Skip to content

Commit 4ad2c35

Browse files
authored
fix(node): Ensure graphql options are correct when preloading (#13769)
Noticed this by chance, due to the way `generateInstrumentOnce` works, we need to pass in the fully formed config.
1 parent 48b3a78 commit 4ad2c35

File tree

3 files changed

+62
-8
lines changed

3 files changed

+62
-8
lines changed

packages/node/src/integrations/tracing/graphql.ts

+14-7
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,7 @@ const INTEGRATION_NAME = 'Graphql';
4040
export const instrumentGraphql = generateInstrumentOnce<GraphqlOptions>(
4141
INTEGRATION_NAME,
4242
(_options: GraphqlOptions = {}) => {
43-
const options = {
44-
ignoreResolveSpans: true,
45-
ignoreTrivialResolveSpans: true,
46-
useOperationNameForRootSpan: true,
47-
..._options,
48-
};
43+
const options = getOptionsWithDefaults(_options);
4944

5045
return new GraphQLInstrumentation({
5146
...options,
@@ -89,7 +84,10 @@ const _graphqlIntegration = ((options: GraphqlOptions = {}) => {
8984
return {
9085
name: INTEGRATION_NAME,
9186
setupOnce() {
92-
instrumentGraphql(options);
87+
// We set defaults here, too, because otherwise we'd update the instrumentation config
88+
// to the config without defaults, as `generateInstrumentOnce` automatically calls `setConfig(options)`
89+
// when being called the second time
90+
instrumentGraphql(getOptionsWithDefaults(options));
9391
},
9492
};
9593
}) satisfies IntegrationFn;
@@ -100,3 +98,12 @@ const _graphqlIntegration = ((options: GraphqlOptions = {}) => {
10098
* Capture tracing data for GraphQL.
10199
*/
102100
export const graphqlIntegration = defineIntegration(_graphqlIntegration);
101+
102+
function getOptionsWithDefaults(options?: GraphqlOptions): GraphqlOptions {
103+
return {
104+
ignoreResolveSpans: true,
105+
ignoreTrivialResolveSpans: true,
106+
useOperationNameForRootSpan: true,
107+
...options,
108+
};
109+
}

packages/node/src/otel/instrument.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { Instrumentation } from '@opentelemetry/instrumentation';
22
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
33

4-
const INSTRUMENTED: Record<string, Instrumentation> = {};
4+
/** Exported only for tests. */
5+
export const INSTRUMENTED: Record<string, Instrumentation> = {};
56

67
/**
78
* Instrument an OpenTelemetry instrumentation once.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { GraphQLInstrumentation } from '@opentelemetry/instrumentation-graphql';
2+
import { graphqlIntegration, instrumentGraphql } from '../../../src/integrations/tracing/graphql';
3+
import { INSTRUMENTED } from '../../../src/otel/instrument';
4+
5+
jest.mock('@opentelemetry/instrumentation-graphql');
6+
7+
describe('GraphQL', () => {
8+
beforeEach(() => {
9+
jest.clearAllMocks();
10+
delete INSTRUMENTED.Graphql;
11+
12+
(GraphQLInstrumentation as unknown as jest.SpyInstance).mockImplementation(() => {
13+
return {
14+
setTracerProvider: () => undefined,
15+
setMeterProvider: () => undefined,
16+
getConfig: () => ({}),
17+
setConfig: () => ({}),
18+
enable: () => undefined,
19+
};
20+
});
21+
});
22+
23+
it('defaults are correct for instrumentGraphql', () => {
24+
instrumentGraphql({ ignoreTrivialResolveSpans: false });
25+
26+
expect(GraphQLInstrumentation).toHaveBeenCalledTimes(1);
27+
expect(GraphQLInstrumentation).toHaveBeenCalledWith({
28+
ignoreResolveSpans: true,
29+
ignoreTrivialResolveSpans: false,
30+
useOperationNameForRootSpan: true,
31+
responseHook: expect.any(Function),
32+
});
33+
});
34+
35+
it('defaults are correct for _graphqlIntegration', () => {
36+
graphqlIntegration({ ignoreTrivialResolveSpans: false }).setupOnce!();
37+
38+
expect(GraphQLInstrumentation).toHaveBeenCalledTimes(1);
39+
expect(GraphQLInstrumentation).toHaveBeenCalledWith({
40+
ignoreResolveSpans: true,
41+
ignoreTrivialResolveSpans: false,
42+
useOperationNameForRootSpan: true,
43+
responseHook: expect.any(Function),
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)