Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gql)!: conform GraphQL span name to spec #1444

Merged
merged 5 commits into from
Apr 2, 2023
Merged
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
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