Skip to content

Commit

Permalink
fix(gql)!: conform GraphQL span name to spec (open-telemetry#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
naseemkullah authored Apr 2, 2023
1 parent 9268716 commit 7d070db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -404,18 +404,21 @@ export class GraphQLInstrumentation extends InstrumentationBase {

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

if (operationDefinition.name) {
span.setAttribute(
AttributeNames.OPERATION_NAME,
operationDefinition.name.value
);
span.setAttribute(AttributeNames.OPERATION_TYPE, operationType);

const operationName = nameNode?.value;

// https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/instrumentation/graphql/
// > The span name MUST be of the format <graphql.operation.type> <graphql.operation.name> provided that graphql.operation.type and graphql.operation.name are available.
// > If graphql.operation.name is not available, the span SHOULD be named <graphql.operation.type>.
if (operationName) {
span.setAttribute(AttributeNames.OPERATION_NAME, operationName);
span.updateName(`${operationType} ${operationName}`);
} else {
span.updateName(operationType);
}
} else {
let operationName = ' ';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ describe('graphql', () => {
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'query');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});

Expand Down Expand Up @@ -297,7 +297,7 @@ describe('graphql', () => {
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'query');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});

Expand Down Expand Up @@ -395,7 +395,7 @@ describe('graphql', () => {
executeSpan.attributes[`${AttributeNames.VARIABLES}id`],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'query Query1');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});

Expand Down Expand Up @@ -487,7 +487,7 @@ describe('graphql', () => {
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'query');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
});
Expand Down Expand Up @@ -555,7 +555,7 @@ describe('graphql', () => {
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'query');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});
});
Expand Down Expand Up @@ -752,7 +752,7 @@ describe('graphql', () => {
executeSpan.attributes[AttributeNames.OPERATION_NAME],
undefined
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'query');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});

Expand Down Expand Up @@ -848,7 +848,7 @@ describe('graphql', () => {
executeSpan.attributes[AttributeNames.OPERATION_NAME],
'AddBook'
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'mutation AddBook');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});

Expand Down Expand Up @@ -946,7 +946,7 @@ describe('graphql', () => {
executeSpan.attributes[`${AttributeNames.VARIABLES}id`],
2
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'query Query1');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});

Expand Down Expand Up @@ -1044,7 +1044,7 @@ describe('graphql', () => {
executeSpan.attributes[AttributeNames.OPERATION_NAME],
'AddBook'
);
assert.deepStrictEqual(executeSpan.name, SpanNames.EXECUTE);
assert.deepStrictEqual(executeSpan.name, 'mutation AddBook');
assert.deepStrictEqual(executeSpan.parentSpanId, undefined);
});

Expand Down Expand Up @@ -1317,7 +1317,7 @@ describe('graphql', () => {

// validate execute span is present
const spans = exporter.getFinishedSpans();
const executeSpans = spans.filter(s => s.name === SpanNames.EXECUTE);
const executeSpans = spans.filter(s => s.name === 'query');
assert.deepStrictEqual(executeSpans.length, 1);
const [executeSpan] = executeSpans;
assert.deepStrictEqual(
Expand Down Expand Up @@ -1371,7 +1371,7 @@ describe('graphql', () => {
);

// single execute span
const executeSpans = spans.filter(s => s.name === SpanNames.EXECUTE);
const executeSpans = spans.filter(s => s.name === 'query');
assert.deepStrictEqual(executeSpans.length, 1);
});
});
Expand Down

0 comments on commit 7d070db

Please sign in to comment.