Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions packages/instrumentation-graphql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,7 @@ export function wrapFields(
tracer: api.Tracer,
getConfig: () => GraphQLInstrumentationParsedConfig
): void {
if (
!type ||
typeof type.getFields !== 'function' ||
type[OTEL_PATCHED_SYMBOL]
) {
if (!type || type[OTEL_PATCHED_SYMBOL]) {
return;
}
const fields = type.getFields();
Expand All @@ -328,16 +324,47 @@ export function wrapFields(
}

if (field.type) {
let unwrappedType: any = field.type;

while (unwrappedType.ofType) {
unwrappedType = unwrappedType.ofType;
const unwrappedTypes = unwrapType(field.type);
for (const unwrappedType of unwrappedTypes) {
wrapFields(unwrappedType, tracer, getConfig);
}
wrapFields(unwrappedType, tracer, getConfig);
}
});
}

function unwrapType(
type: graphqlTypes.GraphQLOutputType
): readonly graphqlTypes.GraphQLObjectType[] {
// unwrap wrapping types (non-nullable and list types)
if ('ofType' in type) {
return unwrapType(type.ofType);
}

// unwrap union types
if (isGraphQLUnionType(type)) {
return type.getTypes();
}

// return object types
if (isGraphQLObjectType(type)) {
return [type];
}

return [];
}

function isGraphQLUnionType(
type: graphqlTypes.GraphQLType
): type is graphqlTypes.GraphQLUnionType {
return 'getTypes' in type && typeof type.getTypes === 'function';
}

function isGraphQLObjectType(
type: graphqlTypes.GraphQLType
): type is graphqlTypes.GraphQLObjectType {
return 'getFields' in type && typeof type.getFields === 'function';
}

const handleResolveSpanError = (
resolveSpan: api.Span,
err: any,
Expand Down
121 changes: 121 additions & 0 deletions packages/instrumentation-graphql/test/graphql.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,21 @@ const sourceFindUsingVariable = `
}
`;

const sourceSearch = `
query Search ($name: String!) {
search(name: $name) {
... on Book {
__typename
name
}
... on EBook {
__typename
name
}
}
}
`;

const badQuery = `
query foo bar
`;
Expand Down Expand Up @@ -244,6 +259,7 @@ describe('graphql', () => {
assert.ok(times[RESOLVE].end <= times[EXECUTE].end);
});
});

describe('AND source is query with param', () => {
let spans: ReadableSpan[];

Expand Down Expand Up @@ -338,6 +354,7 @@ describe('graphql', () => {
);
});
});

describe('AND source is query with param and variables', () => {
let spans: ReadableSpan[];

Expand Down Expand Up @@ -442,6 +459,110 @@ describe('graphql', () => {
);
});
});

describe('AND source is query to get a list of union type', () => {
let spans: ReadableSpan[];
beforeEach(async () => {
create({});
await graphql({
schema,
source: sourceSearch,
variableValues: { name: 'first' },
});
spans = exporter.getFinishedSpans();
});

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

it('should have 6 spans', () => {
assert.deepStrictEqual(spans.length, 6);
});

it('should instrument parse', () => {
const parseSpan = spans[0];
assert.deepStrictEqual(
parseSpan.attributes[AttributeNames.SOURCE],
sourceSearch
);
assert.deepStrictEqual(parseSpan.name, SpanNames.PARSE);
});

it('should instrument validate', () => {
const validateSpan = spans[1];

assert.deepStrictEqual(validateSpan.name, SpanNames.VALIDATE);
assert.deepStrictEqual(
validateSpan.parentSpanContext?.spanId,
undefined
);
});

it('should instrument execute', () => {
const executeSpan = spans[5];

assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.SOURCE],
sourceSearch
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'query'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
'Search'
);
assert.deepStrictEqual(executeSpan.name, 'query Search');
assert.deepStrictEqual(
executeSpan.parentSpanContext?.spanId,
undefined
);
});

it('should instrument resolvers', () => {
const [, , resolveParentSpan, span1, span2, executeSpan] = spans;

assertResolveSpan(
resolveParentSpan,
'search',
'search',
'[SearchResult]',
'search(name: $name) {\n' +
' ... on Book {\n' +
' __typename\n' +
' name\n' +
' }\n' +
' ... on EBook {\n' +
' __typename\n' +
' name\n' +
' }\n' +
' }',
executeSpan.spanContext().spanId
);

const parentId = resolveParentSpan.spanContext().spanId;
assertResolveSpan(
span1,
'name',
'search.0.name',
'String',
'name',
parentId
);
assertResolveSpan(
span2,
'name',
'search.1.name',
'String',
'name',
parentId
);
});
});
});

describe('when depth is set to 0', () => {
Expand Down
Loading