Skip to content

feat: add GraphQL operation attributes to instrumented spans#644

Open
ccschmitz-launchdarkly wants to merge 6 commits into
mainfrom
feat/graphql-otel-span-names
Open

feat: add GraphQL operation attributes to instrumented spans#644
ccschmitz-launchdarkly wants to merge 6 commits into
mainfrom
feat/graphql-otel-span-names

Conversation

@ccschmitz-launchdarkly

@ccschmitz-launchdarkly ccschmitz-launchdarkly commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Auto-instrumented GraphQL requests all hit the same endpoint, so their spans show up as a generic POST /graphql / HTTP POST in the trace list with no indication of which operation ran. This was raised as feedback on the React Native SDK's auto-instrumentation.

This adds a shared, dependency-free parseGraphQLOperation helper in observability-shared and uses it in both client SDKs to tag GraphQL request spans with the OTel semconv attributes graphql.operation.name and graphql.operation.type:

  • React Native (observability-react-native): the fetch hook parses the request body and sets the attributes.
  • Browser (highlight.run): enhanceSpanWithHttpRequestAttributes (which already runs for both fetch and XHR) now also sets graphql.operation.type alongside the existing graphql.operation.name. @launchdarkly/observability re-exports highlight.run, so it's covered too.

Span names are deliberately left as the low-cardinality, OTel-generated names (method-based, e.g. HTTP POST). Per the OTel HTTP and GraphQL semantic conventions, the high-cardinality operation name belongs in an attribute, not the span name. The friendly display name is formatted in the gonfalon trace UI from these attributes (companion PR: launchdarkly/gonfalon#65434), where it renders as POST /graphql - query GetUser.

The parser is intentionally regex-based (no graphql dependency) so it tree-shakes into the browser bundle; a stale unused graphql import was removed from observability-shared to keep it out.

Known limitation (follow-up): React Native XHR request bodies aren't captured, so XHR-based GraphQL clients won't be tagged yet; the fetch path covers Apollo / urql / graphql-request.

How did you test this change?

Unit tests (vitest):

  • observability-shared parseGraphQLOperation — 13/13 (named query/mutation/subscription, operationName-only, name-from-document, anonymous shorthand, batched array, persisted query, non-GraphQL, malformed JSON)
  • observability-react-native network-listener — 3/3 (attributes set; span name left unchanged)
  • highlight.run instrumentation — 129/129 (incl. GraphQL attribute cases)

tsc --noEmit and Prettier are clean on the changed files. Full build / enforce-size (256 KB brotli budget) / ESLint run in CI — the change adds only ~40 lines of pure code to the browser bundle, so no size impact is expected.

Are there any deployment considerations?

No migrations or backfills. Pure client-SDK change; release-please will version-bump observability-react-native and highlight.run on merge. The new graphql.operation.* attributes are purely additive and span names are unchanged, so existing saved views / alerts / aggregations keyed on span name are unaffected.


Note

Low Risk
Additive client-side span attributes only; span names and existing alerts keyed on names stay the same, with no server or auth changes.

Overview
GraphQL HTTP spans can now be distinguished in traces via OTel semconv graphql.operation.name and graphql.operation.type, without changing low-cardinality span names.

A shared, regex-based parseGraphQLOperation helper is added in observability-shared (exported from the package) and wired into React Native fetch instrumentation and highlight.run’s enhanceSpanWithHttpRequestAttributes (fetch and XHR). Tagging runs even when request body recording is off on React Native; only operation metadata is read, not stored as body content.

The browser path replaces ad-hoc JSON parsing of operationName with the shared parser (also sets operation type). An unused graphql import is removed from observability-shared utils.ts. highlight.run takes a workspace dev dependency on observability-shared. Node SDK package.json / lockfile also pick up @opentelemetry/instrumentation-pg as a dev dependency.

Reviewed by Cursor Bugbot for commit 633ee26. Bugbot is set up for automated code reviews on this repo. Configure here.

Auto-instrumented GraphQL calls all hit one endpoint, so spans showed as
"POST /graphql" with no operation. Add a shared, dependency-free
parseGraphQLOperation helper in observability-shared and use it from the
React Native fetch hook and highlight-run's fetch/XHR enrichment to set
the OTel semconv attributes graphql.operation.name/type and rename spans
to "<type> <name>" (e.g. "query GetUser").

Also drops a stale unused `graphql` import from observability-shared so
the parser can be tree-shaken into highlight-run without pulling the
heavy graphql package into its bundle.
Per the OTel HTTP and GraphQL semantic conventions, client HTTP span
names should stay low-cardinality (the method), and the high-cardinality
graphql.operation.name belongs in an attribute, not the span name. Drop
the span.updateName() calls from the React Native and highlight.run
hooks and keep only the graphql.operation.name/type attributes;
gonfalon's formatSpanName already builds the display name from them.
@ccschmitz-launchdarkly ccschmitz-launchdarkly changed the title feat: name GraphQL spans by their operation feat: add GraphQL operation attributes to instrumented spans Jun 24, 2026
@ccschmitz-launchdarkly ccschmitz-launchdarkly marked this pull request as ready for review June 25, 2026 14:03
@ccschmitz-launchdarkly ccschmitz-launchdarkly requested a review from a team as a code owner June 25, 2026 14:03
@ccschmitz-launchdarkly ccschmitz-launchdarkly enabled auto-merge (squash) June 25, 2026 14:04

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit cb4447c. Configure here.

In a multi-operation document, graphql.operation.type was always read from
the first query/mutation/subscription keyword, so a request selecting a
later operation via operationName could get the right name but the wrong
type (e.g. query instead of mutation). Scan for the operation matching
operationName and read its type, falling back to the first operation when
the name isn't found in the document.
@ccschmitz-launchdarkly ccschmitz-launchdarkly enabled auto-merge (squash) June 25, 2026 14:17
…pendency

ObservabilityClient.ts imports the PgInstrumentationConfig type from
@opentelemetry/instrumentation-pg, but only ever resolved it transitively
via @opentelemetry/auto-instrumentations-node. With hoistingLimits set to
"dependencies" for this workspace, the lockfile re-resolution from the
recent devDependency bumps moved instrumentation-pg out of a path tsc
could resolve, breaking typegen (TS2307, plus cascading TS7006 on the
responseHook params).

Declare it explicitly as a devDependency, pinned to the same ^0.69.0 the
transitive resolution already used so dedupe stays clean.
…ency

Same fix as the observability-node package: client.ts imports the
PgInstrumentationConfig type from @opentelemetry/instrumentation-pg but
only resolved it transitively via auto-instrumentations-node. The lockfile
re-resolution broke typegen (TS2307 + cascading TS7006). Declare it
explicitly, matching the ^0.69.0 the transitive resolution already used.
"@opentelemetry/exporter-metrics-otlp-http": "^0.203.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.203.0",
"@opentelemetry/instrumentation": "^0.203.0",
"@opentelemetry/instrumentation-pg": "^0.69.0",

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@abelonogov-ld @Vadman97 heads up that I think this fixes the issue we have been seeing with the monorepo build in CI.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants