Skip to content

Commit ea2a90a

Browse files
authored
feat(instrumentation-graphql): add parent name in attributes of resolver span (open-telemetry#3287)
1 parent 2316f00 commit ea2a90a

File tree

4 files changed

+43
-2
lines changed

4 files changed

+43
-2
lines changed

packages/instrumentation-graphql/src/enums/AttributeNames.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export enum AttributeNames {
1818
FIELD_NAME = 'graphql.field.name',
1919
FIELD_PATH = 'graphql.field.path',
2020
FIELD_TYPE = 'graphql.field.type',
21+
PARENT_NAME = 'graphql.parent.name',
2122
OPERATION_TYPE = 'graphql.operation.type',
2223
OPERATION_NAME = 'graphql.operation.name',
2324
VARIABLES = 'graphql.variables.',

packages/instrumentation-graphql/src/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ function createResolverSpan(
123123
[AttributeNames.FIELD_NAME]: info.fieldName,
124124
[AttributeNames.FIELD_PATH]: path.join('.'),
125125
[AttributeNames.FIELD_TYPE]: info.returnType.toString(),
126+
[AttributeNames.PARENT_NAME]: info.parentType.name,
126127
};
127128

128129
const span = tracer.startSpan(

packages/instrumentation-graphql/test/graphql.test.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,7 @@ describe('graphql', () => {
208208
'books',
209209
'[Book]',
210210
'books {\n' + ' name\n' + ' }',
211+
'Query',
211212
executeSpan.spanContext().spanId
212213
);
213214
const parentId = resolveParentSpan.spanContext().spanId;
@@ -217,6 +218,7 @@ describe('graphql', () => {
217218
'books.0.name',
218219
'String',
219220
'name',
221+
'Book',
220222
parentId
221223
);
222224
assertResolveSpan(
@@ -225,6 +227,7 @@ describe('graphql', () => {
225227
'books.1.name',
226228
'String',
227229
'name',
230+
'Book',
228231
parentId
229232
);
230233
assertResolveSpan(
@@ -233,6 +236,7 @@ describe('graphql', () => {
233236
'books.2.name',
234237
'String',
235238
'name',
239+
'Book',
236240
parentId
237241
);
238242
});
@@ -341,6 +345,7 @@ describe('graphql', () => {
341345
'book',
342346
'Book',
343347
'book(id: *) {\n' + ' name\n' + ' }',
348+
'Query',
344349
executeSpan.spanContext().spanId
345350
);
346351
const parentId = resolveParentSpan.spanContext().spanId;
@@ -350,6 +355,7 @@ describe('graphql', () => {
350355
'book.name',
351356
'String',
352357
'name',
358+
'Book',
353359
parentId
354360
);
355361
});
@@ -446,6 +452,7 @@ describe('graphql', () => {
446452
'book',
447453
'Book',
448454
'book(id: $id) {\n' + ' name\n' + ' }',
455+
'Query',
449456
executeSpan.spanContext().spanId
450457
);
451458
const parentId = resolveParentSpan.spanContext().spanId;
@@ -455,6 +462,7 @@ describe('graphql', () => {
455462
'book.name',
456463
'String',
457464
'name',
465+
'Book',
458466
parentId
459467
);
460468
});
@@ -541,6 +549,7 @@ describe('graphql', () => {
541549
' name\n' +
542550
' }\n' +
543551
' }',
552+
'Query',
544553
executeSpan.spanContext().spanId
545554
);
546555

@@ -551,6 +560,7 @@ describe('graphql', () => {
551560
'search.0.name',
552561
'String',
553562
'name',
563+
'Book',
554564
parentId
555565
);
556566
assertResolveSpan(
@@ -559,6 +569,7 @@ describe('graphql', () => {
559569
'search.1.name',
560570
'String',
561571
'name',
572+
'EBook',
562573
parentId
563574
);
564575
});
@@ -900,6 +911,7 @@ describe('graphql', () => {
900911
'books',
901912
'[Book]',
902913
'books {\n name\n }',
914+
'Query',
903915
executeSpanId
904916
);
905917

@@ -909,6 +921,7 @@ describe('graphql', () => {
909921
'books.0.name',
910922
'String',
911923
'name',
924+
'Book',
912925
executeSpanId
913926
);
914927

@@ -918,6 +931,7 @@ describe('graphql', () => {
918931
'books.1.name',
919932
'String',
920933
'name',
934+
'Book',
921935
executeSpanId
922936
);
923937

@@ -927,6 +941,7 @@ describe('graphql', () => {
927941
'books.2.name',
928942
'String',
929943
'name',
944+
'Book',
930945
executeSpanId
931946
);
932947
});
@@ -1016,6 +1031,7 @@ describe('graphql', () => {
10161031
'book',
10171032
'Book',
10181033
'book(id: 0) {\n' + ' name\n' + ' }',
1034+
'Query',
10191035
executeSpan.spanContext().spanId
10201036
);
10211037
const parentId = resolveParentSpan.spanContext().spanId;
@@ -1025,6 +1041,7 @@ describe('graphql', () => {
10251041
'book.name',
10261042
'String',
10271043
'name',
1044+
'Book',
10281045
parentId
10291046
);
10301047
});
@@ -1123,10 +1140,19 @@ describe('graphql', () => {
11231140
' ) {\n' +
11241141
' id\n' +
11251142
' }',
1143+
'Mutation',
11261144
executeSpan.spanContext().spanId
11271145
);
11281146
const parentId = resolveParentSpan.spanContext().spanId;
1129-
assertResolveSpan(span1, 'id', 'addBook.id', 'Int', 'id', parentId);
1147+
assertResolveSpan(
1148+
span1,
1149+
'id',
1150+
'addBook.id',
1151+
'Int',
1152+
'id',
1153+
'Book',
1154+
parentId
1155+
);
11301156
});
11311157
});
11321158
describe('AND source is query with param and variables', () => {
@@ -1222,6 +1248,7 @@ describe('graphql', () => {
12221248
'book',
12231249
'Book',
12241250
'book(id: $id) {\n' + ' name\n' + ' }',
1251+
'Query',
12251252
executeSpan.spanContext().spanId
12261253
);
12271254
const parentId = resolveParentSpan.spanContext().spanId;
@@ -1231,6 +1258,7 @@ describe('graphql', () => {
12311258
'book.name',
12321259
'String',
12331260
'name',
1261+
'Book',
12341262
parentId
12351263
);
12361264
});
@@ -1325,10 +1353,19 @@ describe('graphql', () => {
13251353
' ) {\n' +
13261354
' id\n' +
13271355
' }',
1356+
'Mutation',
13281357
executeSpan.spanContext().spanId
13291358
);
13301359
const parentId = resolveParentSpan.spanContext().spanId;
1331-
assertResolveSpan(span1, 'id', 'addBook.id', 'Int', 'id', parentId);
1360+
assertResolveSpan(
1361+
span1,
1362+
'id',
1363+
'addBook.id',
1364+
'Int',
1365+
'id',
1366+
'Book',
1367+
parentId
1368+
);
13321369
});
13331370
});
13341371

packages/instrumentation-graphql/test/helper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function assertResolveSpan(
2525
fieldPath: string,
2626
fieldType: string,
2727
source: string,
28+
parentName: string,
2829
parentSpanId?: string
2930
) {
3031
const attrs = span.attributes;
@@ -36,6 +37,7 @@ export function assertResolveSpan(
3637
assert.deepStrictEqual(attrs[AttributeNames.FIELD_PATH], fieldPath);
3738
assert.deepStrictEqual(attrs[AttributeNames.FIELD_TYPE], fieldType);
3839
assert.deepStrictEqual(attrs[AttributeNames.SOURCE], source);
40+
assert.deepStrictEqual(attrs[AttributeNames.PARENT_NAME], parentName);
3941
if (parentSpanId) {
4042
assert.deepStrictEqual(span.parentSpanContext?.spanId, parentSpanId);
4143
}

0 commit comments

Comments
 (0)