Skip to content

perf(instrumentation-graphql): use named imports from @opentelemetry/api - #3941

Open
daniellockyer wants to merge 1 commit into
open-telemetry:mainfrom
daniellockyer:perf/graphql-named-imports
Open

daniellockyer wants to merge 1 commit into
open-telemetry:mainfrom
daniellockyer:perf/graphql-named-imports

Conversation

@daniellockyer

Copy link
Copy Markdown

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.ts and instrumentation.ts use import * 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 reads api.context and api.trace several times per resolved field (api.context.with, api.context.active, api.trace.setSpan), and createResolverSpan and 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-trace and sdk-logs.

Short description of the changes

Import context, trace and SpanStatusCode by name, and Span, SpanAttributes and Tracer with import 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 runs graphql() 5,000 times on a query returning 20 users with 5 scalar fields and a nested object each, inside an active span, with GraphQLInstrumentation at default options (165 spans per query) and a BatchSpanProcessor with a no-op exporter. Instructions and cycles come from perf stat (3 repeats per build), with an N=0 run 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.mjs loader.

Per query CommonJS before → after ESM before → after
Instructions 10.80M → 9.76M (−10%) 8.74M → 8.76M (no change)
Cycles 6.88M → 6.37M (−7%) 5.60M → 5.86M (no change)

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: true the 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-by trailer, following the OpenTelemetry Generative AI policy.

Checklist:

  • Ran npm test and lint for @opentelemetry/instrumentation-graphql
  • Unit tests have been added (not applicable: no behaviour change)
  • Documentation has been updated (not applicable)

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>
@daniellockyer
daniellockyer requested a review from a team as a code owner November 27, 2026 20:06
@github-actions

Copy link
Copy Markdown
Contributor

Welcome, contributor! Thank you for your contribution to opentelemetry-js-contrib.

Important reminders:

  • Read our Contributing Guidelines.
  • Sign the CLA if you haven't already.
  • Follow the OpenTelemetry Generative AI policy: disclose any AI use in your contribution, and communicate (PR descriptions, review replies) in your own words rather than AI-generated text.
  • Give reviewers at least a few days before pinging them for feedback.
  • If you need help with general setup, development process, or contributor etiquette, ask in #opentelemetry-new-contributors.

@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Nov 27, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on maintainers · refreshed 2026-12-01 10:51 UTC

Merge when ready.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with the author? Comment /dashboard route:author to route it back.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants