Skip to content

Commit

Permalink
feat(instrumentation-graphql): add option to ignore resolver spans (#…
Browse files Browse the repository at this point in the history
…1858)

Co-authored-by: Amir Blum <[email protected]>
  • Loading branch information
seemk and blumamir authored Jan 15, 2024
1 parent c54e9b6 commit c365375
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const DEFAULT_CONFIG: GraphQLInstrumentationConfig = {
mergeItems: false,
depth: -1,
allowValues: false,
ignoreResolveSpans: false,
};

const supportedVersions = ['>=14'];
Expand Down Expand Up @@ -474,7 +475,11 @@ export class GraphQLInstrumentation extends InstrumentationBase {
if (!contextValue) {
contextValue = {};
}
if (contextValue[OTEL_GRAPHQL_DATA_SYMBOL]) {

if (
contextValue[OTEL_GRAPHQL_DATA_SYMBOL] ||
this._getConfig().ignoreResolveSpans
) {
return {
schema,
document,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export interface GraphQLInstrumentationConfig extends InstrumentationConfig {
*/
depth?: number;

/**
* Do not create spans for resolvers.
*
* @default false
*/
ignoreResolveSpans?: boolean;

/**
* Don't create spans for the execution of the default resolver on object properties.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,37 @@ describe('graphql', () => {
});
});

describe('when ignoreResolveSpans is true', () => {
beforeEach(() => {
create({
ignoreResolveSpans: true,
});
});

afterEach(() => {
exporter.reset();
graphQLInstrumentation.disable();
});

it('should not create a span for a defined resolver', async () => {
const schema = buildSchema(`
type Query {
hello: String
}
`);

const rootValue = {
hello: () => 'world',
};

await graphql({ schema, source: '{ hello }', rootValue });
const resolveSpans = exporter
.getFinishedSpans()
.filter(span => span.name === `${SpanNames.RESOLVE} hello`);
assert.deepStrictEqual(resolveSpans.length, 0);
});
});

describe('when allowValues is set to true', () => {
describe('AND source is query with param', () => {
let spans: ReadableSpan[];
Expand Down

0 comments on commit c365375

Please sign in to comment.