-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(server-utils): Emit low cardinality graphql span names #23542
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
Changes from 2 commits
5dcec12
ca3bd08
d95710e
09b2018
b748f87
47731ab
232c4d1
426abf5
393a67b
0398116
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| traceLifecycle: 'stream', | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| integrations: [Sentry.graphqlIntegration({ ignoreResolveSpans: false })], | ||
| transport: loggingTransport, | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
|
|
||
| async function run() { | ||
| const { createApolloServer } = await import('../../apollo-server.mjs'); | ||
| const server = createApolloServer(); | ||
|
|
||
| await Sentry.startSpan({ name: 'Test Transaction', op: 'transaction' }, async span => { | ||
| // Ref: https://www.apollographql.com/docs/apollo-server/testing/testing/#testing-using-executeoperation | ||
| await server.executeOperation({ query: 'query GetHello {hello}' }); | ||
| await server.executeOperation({ | ||
| query: 'mutation TestMutation($email: String) { login(email: $email) }', | ||
| variables: { email: 'test@email.com' }, | ||
| }); | ||
|
|
||
| setTimeout(() => { | ||
| span.end(); | ||
| server.stop(); | ||
| }, 500); | ||
| }); | ||
| } | ||
|
|
||
| run(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import type { SerializedStreamedSpanContainer } from '@sentry/core'; | ||
| import { afterAll, describe, expect } from 'vitest'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../../utils/runner'; | ||
|
|
||
| type StreamedSpan = SerializedStreamedSpanContainer['items'][number]; | ||
|
|
||
| // Scoped to the `Test Transaction` segment: creating the server parses the schema's typeDefs, which | ||
| // emits a parse span under `Test Server Start`. | ||
| function graphqlSpans(container: SerializedStreamedSpanContainer): StreamedSpan[] { | ||
| return container.items.filter( | ||
| item => | ||
| item.attributes['sentry.op']?.value === 'graphql' && | ||
| item.attributes['sentry.segment.name']?.value === 'Test Transaction', | ||
| ); | ||
| } | ||
|
|
||
| describe('GraphQL/Apollo Tests > span streaming', () => { | ||
| afterAll(() => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createTestRunner, test) => { | ||
| test('names graphql spans after the operation type, never the operation name or field path', async () => { | ||
| await createTestRunner() | ||
| .expect({ | ||
| span: container => { | ||
| const spans = graphqlSpans(container); | ||
|
|
||
| const executeSpans = spans.filter(span => span.attributes['graphql.operation.type']); | ||
| expect(executeSpans.map(span => span.name)).toEqual(['GraphQL query', 'GraphQL mutation']); | ||
|
|
||
| // Resolver spans keep the field path as an attribute, but it is unbounded, so it must not | ||
| // reach the span name. | ||
| const resolveSpans = spans.filter(span => span.attributes['graphql.field.path']); | ||
| expect(resolveSpans.map(span => span.attributes['graphql.field.path']?.value)).toEqual(['hello', 'login']); | ||
|
|
||
| // Parse, validate and resolve spans have no operation type to name them after. | ||
| const fallbackSpans = spans.filter(span => !executeSpans.includes(span)); | ||
| expect(fallbackSpans.every(span => span.name === 'GraphQL Operation')).toBe(true); | ||
|
|
||
| expect(spans.some(span => span.name.includes('GetHello') || span.name.includes('TestMutation'))).toBe( | ||
| false, | ||
| ); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
|
|
||
| test('marks every graphql span with its processing type', async () => { | ||
| await createTestRunner() | ||
| .expect({ | ||
| span: container => { | ||
| const processingTypes = graphqlSpans(container).map( | ||
| span => span.attributes['graphql.processing.type']?.value, | ||
| ); | ||
|
|
||
| expect(processingTypes.sort()).toEqual([ | ||
| 'execute', | ||
| 'execute', | ||
| 'parse', | ||
| 'parse', | ||
| 'resolve', | ||
| 'resolve', | ||
| 'validate', | ||
| 'validate', | ||
| ]); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
|
|
||
| test('records the operations on the segment span without renaming it', async () => { | ||
| await createTestRunner() | ||
| .expect({ | ||
| span: container => { | ||
| // `Test Server Start` is a segment too, so pick the one the operations ran under. | ||
| const segmentSpan = container.items.find(item => item.is_segment && item.name === 'Test Transaction'); | ||
|
|
||
| expect(segmentSpan).toBeDefined(); | ||
| expect(segmentSpan?.attributes['sentry.graphql.operation']).toBeDefined(); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
|
|
@@ -9,6 +9,9 @@ | |||
| import { GRAPHQL } from '@sentry/conventions/op'; | ||||
| import type { Span, SpanAttributes } from '@sentry/core'; | ||||
| import { | ||||
| getClient, | ||||
| GRAPHQL_SPAN_NAME_FALLBACK, | ||||
| hasSpanStreamingEnabled, | ||||
| isObjectLike, | ||||
| SEMANTIC_ATTRIBUTE_SENTRY_OP, | ||||
| SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, | ||||
|
|
@@ -23,7 +26,9 @@ import { | |||
| GRAPHQL_FIELD_TYPE, | ||||
| GRAPHQL_PARENT_NAME, | ||||
| GRAPHQL_PATCHED_SYMBOL, | ||||
| GRAPHQL_PROCESSING_TYPE, | ||||
| ORIGIN, | ||||
| PROCESSING_TYPE_RESOLVE, | ||||
| SPAN_NAME_RESOLVE, | ||||
| } from './constants'; | ||||
| import type { | ||||
|
|
@@ -180,13 +185,22 @@ function createResolverSpan(info: GraphQLResolveInfo, path: string[], parentSpan | |||
| const attributes: SpanAttributes = { | ||||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN, | ||||
| [SEMANTIC_ATTRIBUTE_SENTRY_OP]: GRAPHQL, | ||||
| [GRAPHQL_PROCESSING_TYPE]: PROCESSING_TYPE_RESOLVE, | ||||
| [GRAPHQL_FIELD_NAME]: info.fieldName, | ||||
| [GRAPHQL_FIELD_PATH]: path.join('.'), | ||||
| [GRAPHQL_FIELD_TYPE]: info.returnType.toString(), | ||||
| [GRAPHQL_PARENT_NAME]: info.parentType.name, | ||||
| }; | ||||
|
|
||||
| return startInactiveSpan({ name: `${SPAN_NAME_RESOLVE} ${path.join('.')}`, attributes, parentSpan }); | ||||
| const client = getClient(); | ||||
|
|
||||
| return startInactiveSpan({ | ||||
| // The field path is unbounded, so with span streaming it stays on `graphql.field.path` only. | ||||
| name: | ||||
| client && hasSpanStreamingEnabled(client) ? GRAPHQL_SPAN_NAME_FALLBACK : `${SPAN_NAME_RESOLVE} ${path.join('.')}`, | ||||
|
Member
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. suuuper-l: You think this could be a helper function? Something like
Member
Author
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. I think that goes against:
|
||||
| attributes, | ||||
| parentSpan, | ||||
| }); | ||||
| } | ||||
|
|
||||
| function addField(contextValue: ObjectWithGraphQLData, path: string[], field: { span: Span }): void { | ||||
|
|
||||
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.
superduper-l: Theoretically we don't need to add this line as it is on by default