Skip to content

Commit c365375

Browse files
seemkblumamir
andauthored
feat(instrumentation-graphql): add option to ignore resolver spans (#1858)
Co-authored-by: Amir Blum <[email protected]>
1 parent c54e9b6 commit c365375

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

plugins/node/opentelemetry-instrumentation-graphql/src/instrumentation.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ const DEFAULT_CONFIG: GraphQLInstrumentationConfig = {
5959
mergeItems: false,
6060
depth: -1,
6161
allowValues: false,
62+
ignoreResolveSpans: false,
6263
};
6364

6465
const supportedVersions = ['>=14'];
@@ -474,7 +475,11 @@ export class GraphQLInstrumentation extends InstrumentationBase {
474475
if (!contextValue) {
475476
contextValue = {};
476477
}
477-
if (contextValue[OTEL_GRAPHQL_DATA_SYMBOL]) {
478+
479+
if (
480+
contextValue[OTEL_GRAPHQL_DATA_SYMBOL] ||
481+
this._getConfig().ignoreResolveSpans
482+
) {
478483
return {
479484
schema,
480485
document,

plugins/node/opentelemetry-instrumentation-graphql/src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ export interface GraphQLInstrumentationConfig extends InstrumentationConfig {
3939
*/
4040
depth?: number;
4141

42+
/**
43+
* Do not create spans for resolvers.
44+
*
45+
* @default false
46+
*/
47+
ignoreResolveSpans?: boolean;
48+
4249
/**
4350
* Don't create spans for the execution of the default resolver on object properties.
4451
*

plugins/node/opentelemetry-instrumentation-graphql/test/graphql.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,37 @@ describe('graphql', () => {
689689
});
690690
});
691691

692+
describe('when ignoreResolveSpans is true', () => {
693+
beforeEach(() => {
694+
create({
695+
ignoreResolveSpans: true,
696+
});
697+
});
698+
699+
afterEach(() => {
700+
exporter.reset();
701+
graphQLInstrumentation.disable();
702+
});
703+
704+
it('should not create a span for a defined resolver', async () => {
705+
const schema = buildSchema(`
706+
type Query {
707+
hello: String
708+
}
709+
`);
710+
711+
const rootValue = {
712+
hello: () => 'world',
713+
};
714+
715+
await graphql({ schema, source: '{ hello }', rootValue });
716+
const resolveSpans = exporter
717+
.getFinishedSpans()
718+
.filter(span => span.name === `${SpanNames.RESOLVE} hello`);
719+
assert.deepStrictEqual(resolveSpans.length, 0);
720+
});
721+
});
722+
692723
describe('when allowValues is set to true', () => {
693724
describe('AND source is query with param', () => {
694725
let spans: ReadableSpan[];

0 commit comments

Comments
 (0)