Skip to content

Commit

Permalink
fix(graphql): fix graphql.operation.name field (#903)
Browse files Browse the repository at this point in the history
Co-authored-by: Valentin Marchaud <contact@vmarchaud.fr>
Co-authored-by: Daniel Dyla <dyladan@users.noreply.github.com>
  • Loading branch information
3 people committed Feb 24, 2022
1 parent bf39b90 commit 5529261
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 22 deletions.
Expand Up @@ -19,7 +19,8 @@ export enum AttributeNames {
FIELD_NAME = 'graphql.field.name',
FIELD_PATH = 'graphql.field.path',
FIELD_TYPE = 'graphql.field.type',
OPERATION = 'graphql.operation.name',
OPERATION_TYPE = 'graphql.operation.type',
OPERATION_NAME = 'graphql.operation.name',
VARIABLES = 'graphql.variables.',
ERROR_VALIDATION_NAME = 'graphql.validation.error',
}
Expand Up @@ -401,10 +401,18 @@ export class GraphQLInstrumentation extends InstrumentationBase {

const span = this.tracer.startSpan(SpanNames.EXECUTE, {});
if (operation) {
const name = (operation as graphqlTypes.OperationDefinitionNode)
.operation;
if (name) {
span.setAttribute(AttributeNames.OPERATION, name);
const operationDefinition =
operation as graphqlTypes.OperationDefinitionNode;
span.setAttribute(
AttributeNames.OPERATION_TYPE,
operationDefinition.operation
);

if (operationDefinition.name) {
span.setAttribute(
AttributeNames.OPERATION_NAME,
operationDefinition.name.value
);
}
} else {
let operationName = ' ';
Expand All @@ -415,7 +423,7 @@ export class GraphQLInstrumentation extends InstrumentationBase {
'$operationName$',
operationName
);
span.setAttribute(AttributeNames.OPERATION, operationName);
span.setAttribute(AttributeNames.OPERATION_NAME, operationName);
}

if (processedArgs.document?.loc) {
Expand Down
Expand Up @@ -61,7 +61,7 @@ const sourceBookById = `
`;

const sourceAddBook = `
mutation {
mutation AddBook {
addBook(
name: "Fifth Book"
authorIds: "0,2"
Expand Down Expand Up @@ -155,9 +155,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'query'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
Expand Down Expand Up @@ -278,9 +282,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'query'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
Expand Down Expand Up @@ -364,9 +372,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'query'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
'Query1'
);
assert.deepStrictEqual(
executeSpan.attributes[`${AttributeNames.VARIABLES}id`],
undefined
Expand Down Expand Up @@ -456,9 +468,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'query'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
Expand Down Expand Up @@ -520,9 +536,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'query'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
Expand Down Expand Up @@ -607,9 +627,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'query'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
Expand Down Expand Up @@ -664,7 +688,7 @@ describe('graphql', () => {
assert.deepStrictEqual(
parseSpan.attributes[AttributeNames.SOURCE],
'\n' +
' mutation {\n' +
' mutation AddBook {\n' +
' addBook(\n' +
' name: "Fifth Book"\n' +
' authorIds: "0,2"\n' +
Expand All @@ -689,7 +713,7 @@ describe('graphql', () => {
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.SOURCE],
'\n' +
' mutation {\n' +
' mutation AddBook {\n' +
' addBook(\n' +
' name: "Fifth Book"\n' +
' authorIds: "0,2"\n' +
Expand All @@ -699,9 +723,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'mutation'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
'AddBook'
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
Expand Down Expand Up @@ -785,9 +813,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'query'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
'Query1'
);
assert.deepStrictEqual(
executeSpan.attributes[`${AttributeNames.VARIABLES}id`],
2
Expand Down Expand Up @@ -848,7 +880,7 @@ describe('graphql', () => {
assert.deepStrictEqual(
parseSpan.attributes[AttributeNames.SOURCE],
'\n' +
' mutation {\n' +
' mutation AddBook {\n' +
' addBook(\n' +
' name: "*"\n' +
' authorIds: "*"\n' +
Expand All @@ -873,7 +905,7 @@ describe('graphql', () => {
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.SOURCE],
'\n' +
' mutation {\n' +
' mutation AddBook {\n' +
' addBook(\n' +
' name: "*"\n' +
' authorIds: "*"\n' +
Expand All @@ -883,9 +915,13 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_TYPE],
'mutation'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION_NAME],
'AddBook'
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
Expand Down Expand Up @@ -1021,7 +1057,7 @@ describe('graphql', () => {

it('should attach response hook data to the resulting spans', () => {
const querySpan = spans.find(
span => span.attributes['graphql.operation.name'] == 'query'
span => span.attributes[AttributeNames.OPERATION_TYPE] == 'query'
);
const instrumentationResult = querySpan?.attributes[dataAttributeName];
assert.deepStrictEqual(
Expand Down Expand Up @@ -1125,7 +1161,7 @@ describe('graphql', () => {
' }\n'
);
assert.deepStrictEqual(
executeSpan.attributes[AttributeNames.OPERATION],
executeSpan.attributes[AttributeNames.OPERATION_NAME],
'Operation "foo" not supported'
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
Expand Down

0 comments on commit 5529261

Please sign in to comment.