Repository navigation
perf(instrumentation-graphql): use named imports from @opentelemetry/api - #3941
Open
daniellockyer wants to merge 1 commit into
Open
daniellockyer wants to merge 1 commit into
daniellockyer wants to merge 1 commit into
Conversation
The CommonJS build turns `import * as api` into an interop object whose properties are getters, so every `api.context`, `api.trace` and `api.SpanStatusCode` access went through a getter call. The resolver wrapper does several of these per resolved field. Import the names directly instead. Assisted-by: Claude Opus 5.5 Signed-off-by: Daniel Lockyer <hi@daniellockyer.com>
Contributor
|
Welcome, contributor! Thank you for your contribution to opentelemetry-js-contrib. Important reminders:
|
Pull request dashboard statusWaiting on maintainers · refreshed 2026-12-01 10:51 UTC Merge when ready. Status above doesn't look right?
|
pichlermarc
approved these changes
Dec 1, 2026
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Which problem is this PR solving?
Refs #1739
Refs open-telemetry/opentelemetry-js#6100
With default options, the GraphQL instrumentation wraps every field resolver and creates a span per resolved field, so its per-field cost is multiplied by the size of the response (#1739).
utils.tsandinstrumentation.tsuseimport * as api from '@opentelemetry/api'. In the CommonJS build, the bundler turns that namespace import into an interop object (__toESM) whose properties are getters, each calling a bound function. The resolver wrapper readsapi.contextandapi.traceseveral times per resolved field (api.context.with,api.context.active,api.trace.setSpan), andcreateResolverSpanand the span-ending helpers add more, so these getter calls add up per field.This is the same issue open-telemetry/opentelemetry-js#7355 and open-telemetry/opentelemetry-js#7402 fixed in
sdk-traceandsdk-logs.Short description of the changes
Import
context,traceandSpanStatusCodeby name, andSpan,SpanAttributesandTracerwithimport type. The compiled CommonJS code then reads them straight from the required module. No behaviour change: the existing tests (69) pass unchanged.Benchmarks
Node.js v26.13.1, Linux, pinned to one core with
taskset, graphql 16.12.0. The benchmark runsgraphql()5,000 times on a query returning 20 users with 5 scalar fields and a nested object each, inside an active span, withGraphQLInstrumentationat default options (165 spans per query) and aBatchSpanProcessorwith a no-op exporter. Instructions and cycles come fromperf stat(3 repeats per build), with anN=0run subtracted to remove startup. Before and after builds alternated over three rounds; the table shows medians. The ESM run loads the app, graphql and the instrumentation as ES modules through the@opentelemetry/instrumentation/hook.mjsloader.The CommonJS before and after ranges were separated for both instructions and cycles; the ESM ranges overlapped. The ESM build doesn't use the interop helper, so it isn't expected to change.
For reference, the same query costs about 2.42M instructions with the SDK and no GraphQL instrumentation. Most of what remains is creating 165 spans per query; with
ignoreTrivialResolveSpans: truethe same query creates 5 spans.AI disclosure
This change was developed with an AI coding assistant (Claude Opus 5.5), which helped with profiling, writing the code and drafting this description. The commit carries an
Assisted-bytrailer, following the OpenTelemetry Generative AI policy.Checklist:
npm testand lint for@opentelemetry/instrumentation-graphql