-
Notifications
You must be signed in to change notification settings - Fork 25
feat(opentelemetry): allow more customization for hive tracing setup #1611
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
EmrysMyrddin
wants to merge
2
commits into
main
Choose a base branch
from
feat/otel/error-based-sampling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@graphql-hive/plugin-opentelemetry': minor | ||
--- | ||
|
||
It is now possible to fully configure the OpenTelemetry setup when using `hiveTracingSetup`. You can now provide a `resource` and a `samplingRate` (among other options). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -120,41 +120,41 @@ type SamplingOptions = | |||||
samplingRate?: number; | ||||||
}; | ||||||
|
||||||
type OpentelemetrySetupOptions = TracingOptions & | ||||||
SamplingOptions & { | ||||||
/** | ||||||
* The Resource that will be used to create the Trace Provider. | ||||||
* Can be either a Resource instance, or an simple object with service name and version | ||||||
*/ | ||||||
resource?: Resource | { serviceName: string; serviceVersion: string }; | ||||||
/** | ||||||
* The Context Manager to be used to track OTEL Context. | ||||||
* If possible, use `AsyncLocalStorageContextManager` from `@opentelemetry/context-async-hooks`. | ||||||
*/ | ||||||
contextManager: ContextManager | null; | ||||||
/** | ||||||
* A custom list of propagators that will replace the default ones (Trace Context and Baggage) | ||||||
*/ | ||||||
propagators?: TextMapPropagator[]; | ||||||
/** | ||||||
* The general limits of OTEL attributes. | ||||||
*/ | ||||||
generalLimits?: GeneralLimits; | ||||||
/** | ||||||
* The Logger to be used by this utility. | ||||||
* A child of this logger will be used for OTEL diag API, unless `configureDiagLogger` is false | ||||||
*/ | ||||||
log?: Logger; | ||||||
/** | ||||||
* Configure Opentelemetry `diag` API to use Gateway's logger. | ||||||
* | ||||||
* @default true | ||||||
* | ||||||
* Note: Logger configuration respects OTEL environment variables standard. | ||||||
* This means that the logger will be enabled only if `OTEL_LOG_LEVEL` variable is set. | ||||||
*/ | ||||||
configureDiagLogger?: boolean; | ||||||
}; | ||||||
type BaseOptions = { | ||||||
/** | ||||||
* The Resource that will be used to create the Trace Provider. | ||||||
* Can be either a Resource instance, or an simple object with service name and version | ||||||
*/ | ||||||
resource?: Resource | { serviceName: string; serviceVersion: string }; | ||||||
/** | ||||||
* The Context Manager to be used to track OTEL Context. | ||||||
* If possible, use `AsyncLocalStorageContextManager` from `@opentelemetry/context-async-hooks`. | ||||||
*/ | ||||||
contextManager: ContextManager | null; | ||||||
/** | ||||||
* A custom list of propagators that will replace the default ones (Trace Context and Baggage) | ||||||
*/ | ||||||
propagators?: TextMapPropagator[]; | ||||||
/** | ||||||
* The general limits of OTEL attributes. | ||||||
*/ | ||||||
generalLimits?: GeneralLimits; | ||||||
/** | ||||||
* The Logger to be used by this utility. | ||||||
* A child of this logger will be used for OTEL diag API, unless `configureDiagLogger` is false | ||||||
*/ | ||||||
log?: Logger; | ||||||
/** | ||||||
* Configure Opentelemetry `diag` API to use Gateway's logger. | ||||||
* | ||||||
* @default true | ||||||
* | ||||||
* Note: Logger configuration respects OTEL environment variables standard. | ||||||
* This means that the logger will be enabled only if `OTEL_LOG_LEVEL` variable is set. | ||||||
*/ | ||||||
configureDiagLogger?: boolean; | ||||||
}; | ||||||
type OpentelemetrySetupOptions = TracingOptions & SamplingOptions & BaseOptions; | ||||||
|
||||||
export function openTelemetrySetup(options: OpentelemetrySetupOptions) { | ||||||
const log = options.log || new Logger(); | ||||||
|
@@ -221,26 +221,21 @@ export function openTelemetrySetup(options: OpentelemetrySetupOptions) { | |||||
logAttributes['console'] = true; | ||||||
} | ||||||
|
||||||
const baseResource = resourceFromAttributes({ | ||||||
[ATTR_SERVICE_NAME]: | ||||||
options.resource && 'serviceName' in options.resource | ||||||
? options.resource?.serviceName | ||||||
: getEnvStr('OTEL_SERVICE_NAME') || | ||||||
'@graphql-hive/plugin-opentelemetry', | ||||||
[ATTR_SERVICE_VERSION]: | ||||||
options.resource && 'serviceVersion' in options.resource | ||||||
? options.resource?.serviceVersion | ||||||
: getEnvStr('OTEL_SERVICE_VERSION') || | ||||||
globalThis.__OTEL_PLUGIN_VERSION__ || | ||||||
'unknown', | ||||||
['hive.gateway.version']: globalThis.__VERSION__, | ||||||
['hive.otel.version']: globalThis.__OTEL_PLUGIN_VERSION__ || 'unknown', | ||||||
}); | ||||||
|
||||||
const resource = | ||||||
options.resource && !('serviceName' in options.resource) | ||||||
? baseResource.merge(options.resource) | ||||||
: baseResource; | ||||||
const resource = createResource( | ||||||
resourceFromAttributes({ | ||||||
[ATTR_SERVICE_NAME]: | ||||||
getEnvStr('OTEL_SERVICE_NAME') || | ||||||
'@graphql-hive/plugin-opentelemetry', | ||||||
[ATTR_SERVICE_VERSION]: | ||||||
getEnvStr('OTEL_SERVICE_VERSION') || | ||||||
globalThis.__OTEL_PLUGIN_VERSION__ || | ||||||
'unknown', | ||||||
['hive.gateway.version']: globalThis.__VERSION__, | ||||||
['hive.otel.version']: | ||||||
globalThis.__OTEL_PLUGIN_VERSION__ || 'unknown', | ||||||
}), | ||||||
options.resource, | ||||||
); | ||||||
|
||||||
logAttributes['resource'] = resource.attributes; | ||||||
logAttributes['sampling'] = options.sampler | ||||||
|
@@ -300,12 +295,9 @@ export type HiveTracingOptions = { target?: string } & ( | |||||
} | ||||||
); | ||||||
|
||||||
export function hiveTracingSetup( | ||||||
config: HiveTracingOptions & { | ||||||
contextManager: ContextManager | null; | ||||||
log?: Logger; | ||||||
}, | ||||||
) { | ||||||
export type HiveTracingSetupOptions = BaseOptions & HiveTracingOptions; | ||||||
|
||||||
export function hiveTracingSetup(config: HiveTracingSetupOptions) { | ||||||
const log = config.log || new Logger(); | ||||||
config.target ??= getEnvStr('HIVE_TARGET'); | ||||||
|
||||||
|
@@ -332,11 +324,19 @@ export function hiveTracingSetup( | |||||
} | ||||||
|
||||||
openTelemetrySetup({ | ||||||
...config, | ||||||
log, | ||||||
contextManager: config.contextManager, | ||||||
resource: resourceFromAttributes({ | ||||||
'hive.target_id': config.target, | ||||||
}), | ||||||
resource: createResource( | ||||||
resourceFromAttributes({ | ||||||
'hive.target_id': config.target, | ||||||
[ATTR_SERVICE_NAME]: getEnvStr('OTEL_SERVICE_NAME') || 'hive-gateway', | ||||||
[ATTR_SERVICE_VERSION]: | ||||||
getEnvStr('OTEL_SERVICE_VERSION') || | ||||||
globalThis.__OTEL_PLUGIN_VERSION__ || | ||||||
'unknown', | ||||||
}), | ||||||
config.resource, | ||||||
), | ||||||
traces: { | ||||||
processors: [ | ||||||
new HiveTracingSpanProcessor(config as HiveTracingSpanProcessorOptions), | ||||||
|
@@ -363,3 +363,23 @@ function resolveBatchingConfig( | |||||
return new BatchSpanProcessor(exporter, value); | ||||||
} | ||||||
} | ||||||
|
||||||
function createResource( | ||||||
baseResource: Resource, | ||||||
userResource?: OpentelemetrySetupOptions['resource'], | ||||||
): Resource { | ||||||
if (!userResource) { | ||||||
return baseResource; | ||||||
} | ||||||
|
||||||
if ('serviceName' in userResource) { | ||||||
return baseResource.merge( | ||||||
resourceFromAttributes({ | ||||||
[ATTR_SERVICE_NAME]: userResource.serviceName, | ||||||
[ATTR_SERVICE_VERSION]: userResource.serviceVersion, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential undefined access:
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
}), | ||||||
); | ||||||
} | ||||||
|
||||||
return baseResource.merge(userResource); | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
hiveTracingSetup
function signature change removes thecontextManager
andlog
parameters that were previously required. This is a breaking change - existing callers that pass these parameters separately will fail. Consider maintaining backward compatibility or documenting this as a breaking change.Copilot uses AI. Check for mistakes.