-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(server-utils): Instrument graphql v17 via native tracing channels #21804
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
logaretm
wants to merge
11
commits into
develop
Choose a base branch
from
awad/graphql-tracing-channels
base: develop
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.
+1,360
−3
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3ab3911
feat(server-utils): Instrument graphql v17 via native diagnostics_cha…
logaretm 98d5390
feat(server-utils): Support graphql resolver spans via ignoreResolveS…
logaretm 55386f0
refactor(graphql): Drop test-only reset and redundant subscriber book…
logaretm a47dcb2
refactor(graphql): Rename graphqlChannelIntegration to graphqlIntegra…
logaretm f27eaa2
style(graphql): Use nullish-coalescing for optional span attributes
logaretm 6e1c439
refactor(graphql): Use waitForTracingChannelBinding to defer subscribe
logaretm 93ff31a
fix: make the gql integrtion name constant
logaretm ae5f992
refactor(graphql): Drop no-op nullish coalescing on span attributes
logaretm 1c4dfb9
ref: remove try/catch and move subscribe to the end
logaretm 3c947ab
feat(graphql): Rename root span with operation name on the diagnostic…
logaretm 199e1b7
docs(graphql): Explain the reverse loop and non-null assertion in red…
logaretm 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
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/tracing/graphql-tracing-channel/instrument.mjs
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,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
10 changes: 10 additions & 0 deletions
10
...es/node-integration-tests/suites/tracing/graphql-tracing-channel/resolvers/instrument.mjs
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,10 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| integrations: [Sentry.graphqlIntegration({ ignoreResolveSpans: false })], | ||
| transport: loggingTransport, | ||
| }); |
40 changes: 40 additions & 0 deletions
40
...ages/node-integration-tests/suites/tracing/graphql-tracing-channel/resolvers/scenario.mjs
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,40 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { graphql, GraphQLInt, GraphQLNonNull, GraphQLObjectType, GraphQLSchema, GraphQLString } from 'graphql'; | ||
|
|
||
| // Built programmatically (not via `buildSchema(sdl)`) so no `graphql:parse` fires at module load. | ||
| const UserType = new GraphQLObjectType({ | ||
| name: 'User', | ||
| // `name` has no resolver, so it uses graphql's default property resolver (a "trivial" resolve). | ||
| fields: { name: { type: GraphQLString } }, | ||
| }); | ||
|
|
||
| const schema = new GraphQLSchema({ | ||
| query: new GraphQLObjectType({ | ||
| name: 'Query', | ||
| fields: { | ||
| hello: { type: GraphQLString, resolve: () => 'world' }, | ||
| user: { | ||
| type: UserType, | ||
| args: { id: { type: new GraphQLNonNull(GraphQLInt) } }, | ||
| resolve: (_, { id }) => ({ name: `user-${id}` }), | ||
| }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| async function run() { | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
|
||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| await graphql({ schema, source: '{ hello }' }); | ||
| await graphql({ schema, source: 'query GetUser { user(id: 1) { name } }' }); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| run(); |
62 changes: 62 additions & 0 deletions
62
dev-packages/node-integration-tests/suites/tracing/graphql-tracing-channel/resolvers/test.ts
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,62 @@ | ||
| import { afterAll, expect } from 'vitest'; | ||
| import { conditionalTest } from '../../../../utils'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../../utils/runner'; | ||
|
|
||
| // With `ignoreResolveSpans: false`, the channel path also subscribes `graphql:resolve` and emits a | ||
| // span per non-trivial field resolver. `ignoreTrivialResolveSpans` defaults to true, so graphql's | ||
| // default property resolver (the `name` field) is skipped. graphql 17 requires Node >= 22. | ||
| conditionalTest({ min: 22 })('GraphQL tracing channel Test > resolve spans', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| const expectedResolveSpan = (path: string, fieldName: string, parentName: string) => | ||
| expect.objectContaining({ | ||
| description: `graphql.resolve ${path}`, | ||
| op: 'graphql', | ||
| origin: 'auto.graphql.diagnostic_channel', | ||
| data: expect.objectContaining({ | ||
| 'graphql.field.name': fieldName, | ||
| 'graphql.field.path': path, | ||
| 'graphql.parent.name': parentName, | ||
| }), | ||
| }); | ||
|
|
||
| const EXPECTED_TRANSACTION = { | ||
| // Root span renamed with the (sorted) operation names. useOperationNameForRootSpan defaults to true. | ||
| transaction: 'Test Transaction (query, query GetUser)', | ||
| spans: expect.arrayContaining([ | ||
| expect.objectContaining({ description: 'query', op: 'graphql' }), | ||
| expect.objectContaining({ description: 'query GetUser', op: 'graphql' }), | ||
| expectedResolveSpan('hello', 'hello', 'Query'), | ||
| expectedResolveSpan('user', 'user', 'Query'), | ||
| ]), | ||
| }; | ||
|
|
||
| createEsmAndCjsTests( | ||
| __dirname, | ||
| 'scenario.mjs', | ||
| 'instrument.mjs', | ||
| (createTestRunner, test) => { | ||
| test('emits resolver spans when ignoreResolveSpans is false', async () => { | ||
| await createTestRunner().expect({ transaction: EXPECTED_TRANSACTION }).start().completed(); | ||
| }); | ||
|
|
||
| test('skips the default property resolver (trivial resolve) by default', async () => { | ||
| await createTestRunner() | ||
| .expect({ | ||
| transaction: event => { | ||
| const spans = event.spans || []; | ||
| // `user.name` uses graphql's default property resolver, so no span is emitted for it. | ||
| expect(spans.find(span => span.description === 'graphql.resolve user.name')).toBeUndefined(); | ||
| // ...but the user-defined resolvers do produce spans. | ||
| expect(spans.find(span => span.description === 'graphql.resolve user')).toBeDefined(); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
| }, | ||
| { additionalDependencies: { graphql: '^17' } }, | ||
| ); | ||
| }); |
71 changes: 71 additions & 0 deletions
71
dev-packages/node-integration-tests/suites/tracing/graphql-tracing-channel/scenario.mjs
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,71 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { | ||
| graphql, | ||
| GraphQLBoolean, | ||
| GraphQLInt, | ||
| GraphQLNonNull, | ||
| GraphQLObjectType, | ||
| GraphQLSchema, | ||
| GraphQLString, | ||
| } from 'graphql'; | ||
|
|
||
| // Build the schema programmatically rather than via `buildSchema(sdl)`: the SDL form parses at module | ||
| // load, which would publish a `graphql:parse` event outside any transaction (an orphan span). | ||
| const UserType = new GraphQLObjectType({ | ||
| name: 'User', | ||
| fields: { name: { type: GraphQLString } }, | ||
| }); | ||
|
|
||
| const schema = new GraphQLSchema({ | ||
| query: new GraphQLObjectType({ | ||
| name: 'Query', | ||
| fields: { | ||
| hello: { type: GraphQLString, resolve: () => 'world' }, | ||
| user: { | ||
| type: UserType, | ||
| args: { id: { type: new GraphQLNonNull(GraphQLInt) } }, | ||
| resolve: (_, { id }) => ({ name: `user-${id}` }), | ||
| }, | ||
| boom: { | ||
| type: GraphQLString, | ||
| resolve: () => { | ||
| throw new Error('resolver failed'); | ||
| }, | ||
| }, | ||
| }, | ||
| }), | ||
| mutation: new GraphQLObjectType({ | ||
| name: 'Mutation', | ||
| fields: { | ||
| login: { | ||
| type: GraphQLBoolean, | ||
| args: { email: { type: new GraphQLNonNull(GraphQLString) } }, | ||
| resolve: (_, { email }) => Boolean(email), | ||
| }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| async function run() { | ||
| // Let the integration's deferred channel subscription and OpenTelemetry's async-context setup | ||
| // finish before issuing operations. In a real server graphql runs per-request, long after init; | ||
| // here it would otherwise race init within the same tick. | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
|
||
| await Sentry.startSpan( | ||
| { | ||
| name: 'Test Transaction', | ||
| op: 'transaction', | ||
| }, | ||
| async () => { | ||
| await graphql({ schema, source: '{ hello }' }); | ||
| await graphql({ schema, source: 'query GetUser { user(id: 42) { name } }' }); | ||
| // Inline literal carries a value, to assert it is redacted out of `graphql.document`. | ||
| await graphql({ schema, source: 'mutation Login { login(email: "secret@example.com") }' }); | ||
| // A resolver throw surfaces as `result.errors`, which must flag the execute span as errored. | ||
| await graphql({ schema, source: 'query Boom { boom }' }); | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| run(); | ||
116 changes: 116 additions & 0 deletions
116
dev-packages/node-integration-tests/suites/tracing/graphql-tracing-channel/test.ts
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,116 @@ | ||
| import { afterAll, expect } from 'vitest'; | ||
| import { conditionalTest } from '../../../utils'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| // graphql >= 17 publishes its operations via `node:diagnostics_channel`, so the SDK subscribes to | ||
| // those channels (`subscribeGraphqlDiagnosticChannels`) instead of the vendored OTel patcher. This | ||
| // suite pins `^17` and asserts the diagnostics-channel path: graphql semconv attributes, redacted | ||
| // document text, span relationships, and that the legacy OTel path does NOT also fire (no double | ||
| // instrumentation). graphql 17 requires Node >= 22, so this suite is skipped on older Node. | ||
| conditionalTest({ min: 22 })('GraphQL tracing channel Test', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| const expectedExecuteSpan = (description: string, extraData: Record<string, unknown> = {}) => | ||
| expect.objectContaining({ | ||
| description, | ||
| op: 'graphql', | ||
| origin: 'auto.graphql.diagnostic_channel', | ||
| data: expect.objectContaining(extraData), | ||
| }); | ||
|
|
||
| const EXPECTED_TRANSACTION = { | ||
| // Root span renamed with the (sorted) operation names. useOperationNameForRootSpan defaults to true. | ||
| transaction: 'Test Transaction (mutation Login, query, query Boom, query GetUser)', | ||
| spans: expect.arrayContaining([ | ||
| expect.objectContaining({ description: 'graphql.parse', op: 'graphql' }), | ||
| expect.objectContaining({ description: 'graphql.validate', op: 'graphql' }), | ||
| // anonymous query -> span named after the operation type only | ||
| expectedExecuteSpan('query', { 'graphql.operation.type': 'query' }), | ||
| expectedExecuteSpan('query GetUser', { | ||
| 'graphql.operation.type': 'query', | ||
| 'graphql.operation.name': 'GetUser', | ||
| // the inline `42` literal is redacted out of the document | ||
| 'graphql.document': 'query GetUser { user(id: *) { name } }', | ||
| }), | ||
| expectedExecuteSpan('mutation Login', { | ||
| 'graphql.operation.type': 'mutation', | ||
| // the inline email literal must be redacted to `"*"`, so the raw value can never leak | ||
| 'graphql.document': 'mutation Login { login(email: "*") }', | ||
| }), | ||
| ]), | ||
| }; | ||
|
|
||
| createEsmAndCjsTests( | ||
| __dirname, | ||
| 'scenario.mjs', | ||
| 'instrument.mjs', | ||
| (createTestRunner, test) => { | ||
| test('subscribes to graphql >= 17 diagnostics channels with graphql semconv attributes', async () => { | ||
| await createTestRunner().expect({ transaction: EXPECTED_TRANSACTION }).start().completed(); | ||
| }); | ||
|
|
||
| test('does not double-instrument: the vendored OTel graphql patcher does not fire on 17', async () => { | ||
| await createTestRunner() | ||
| .expect({ | ||
| transaction: event => { | ||
| const spans = event.spans || []; | ||
| // The vendored OTel path (origin `auto.graphql.otel.graphql`) must be inactive on 17+. | ||
| expect(spans.find(span => span.origin === 'auto.graphql.otel.graphql')).toBeUndefined(); | ||
| // ...while the diagnostics-channel path is active. | ||
| expect(spans.find(span => span.origin === 'auto.graphql.diagnostic_channel')).toBeDefined(); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
|
|
||
| test('never leaks raw inline literal values into graphql.document', async () => { | ||
| await createTestRunner() | ||
| .expect({ | ||
| transaction: event => { | ||
| const spans = event.spans || []; | ||
| for (const span of spans) { | ||
| const document = span.data?.['graphql.document']; | ||
| if (typeof document === 'string') { | ||
| expect(document).not.toContain('secret@example.com'); | ||
| } | ||
| } | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
|
|
||
| test('flags the execute span as errored when a resolver throws', async () => { | ||
| await createTestRunner() | ||
| .expect({ | ||
| transaction: event => { | ||
| const spans = event.spans || []; | ||
| const boomSpan = spans.find(span => span.description === 'query Boom'); | ||
| expect(boomSpan).toBeDefined(); | ||
| expect(boomSpan?.status).toBe('internal_error'); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
|
|
||
| test('parents the execute span to the surrounding transaction', async () => { | ||
| await createTestRunner() | ||
| .expect({ | ||
| transaction: event => { | ||
| const spans = event.spans || []; | ||
| const executeSpan = spans.find(span => span.description === 'query GetUser'); | ||
| expect(executeSpan).toBeDefined(); | ||
| expect(executeSpan?.parent_span_id).toBe(event.contexts?.trace?.span_id); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
| }, | ||
| { additionalDependencies: { graphql: '^17' } }, | ||
| ); | ||
| }); |
10 changes: 10 additions & 0 deletions
10
...uites/tracing/graphql-tracing-channel/useOperationNameForRootSpan/instrument-disabled.mjs
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,10 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| integrations: [Sentry.graphqlIntegration({ useOperationNameForRootSpan: false })], | ||
| transport: loggingTransport, | ||
| }); |
9 changes: 9 additions & 0 deletions
9
...n-tests/suites/tracing/graphql-tracing-channel/useOperationNameForRootSpan/instrument.mjs
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,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
21 changes: 21 additions & 0 deletions
21
.../suites/tracing/graphql-tracing-channel/useOperationNameForRootSpan/scenario-disabled.mjs
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,21 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { GraphQLObjectType, GraphQLSchema, GraphQLString, graphql } from 'graphql'; | ||
|
|
||
| const schema = new GraphQLSchema({ | ||
| query: new GraphQLObjectType({ | ||
| name: 'Query', | ||
| fields: { | ||
| hello: { type: GraphQLString, resolve: () => 'world' }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| async function run() { | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
|
||
| await Sentry.startSpan({ name: 'Test Transaction', op: 'transaction' }, async () => { | ||
| await graphql({ schema, source: 'query GetHello { hello }' }); | ||
| }); | ||
| } | ||
|
|
||
| run(); |
23 changes: 23 additions & 0 deletions
23
.../suites/tracing/graphql-tracing-channel/useOperationNameForRootSpan/scenario-multiple.mjs
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,23 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { GraphQLObjectType, GraphQLSchema, GraphQLString, graphql } from 'graphql'; | ||
|
|
||
| const schema = new GraphQLSchema({ | ||
| query: new GraphQLObjectType({ | ||
| name: 'Query', | ||
| fields: { | ||
| hello: { type: GraphQLString, resolve: () => 'world' }, | ||
| world: { type: GraphQLString, resolve: () => 'hello' }, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| async function run() { | ||
| await new Promise(resolve => setTimeout(resolve, 100)); | ||
|
|
||
| await Sentry.startSpan({ name: 'Test Transaction', op: 'transaction' }, async () => { | ||
| await graphql({ schema, source: 'query GetWorld { world }' }); | ||
| await graphql({ schema, source: 'query GetHello { hello }' }); | ||
| }); | ||
| } | ||
|
|
||
| run(); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.