Skip to content

Commit

Permalink
chore(instrumentation-graphql): remove operation name from execute span
Browse files Browse the repository at this point in the history
  • Loading branch information
mentos1386 committed Nov 17, 2021
1 parent e18d416 commit 80fd552
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 39 deletions.
17 changes: 0 additions & 17 deletions plugins/node/opentelemetry-instrumentation-graphql/src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,3 @@ export enum SpanNames {
SCHEMA_VALIDATE = 'graphql.validateSchema',
SCHEMA_PARSE = 'graphql.parseSchema',
}

export const createResolveSpanName = (fieldName: string): string => {
return `${SpanNames.RESOLVE}.${fieldName}`;
};

export const createExecuteSpanName = (
operationType?: string,
operationName?: string
): string => {
if (operationType) {
return `${SpanNames.EXECUTE}.${operationType}`;
}
if (operationType && operationName) {
return `${SpanNames.EXECUTE}.${operationType}.${operationName}`;
}
return `${SpanNames.EXECUTE}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
} from '@opentelemetry/instrumentation';
import type * as graphqlTypes from 'graphql';
import { GraphQLFieldResolver } from 'graphql/type/definition';
import { createExecuteSpanName, SpanNames } from './enum';
import { SpanNames } from './enum';
import { AttributeNames } from './enums/AttributeNames';
import { OTEL_GRAPHQL_DATA_SYMBOL } from './symbols';

Expand All @@ -44,6 +44,7 @@ import {
import {
addInputVariableAttributes,
addSpanSource,
createExecuteSpanName,
endSpan,
getOperation,
wrapFieldResolver,
Expand Down Expand Up @@ -391,18 +392,18 @@ export class GraphQLInstrumentation extends InstrumentationBase {

private getOperationType(
operation: graphqlTypes.DefinitionNode | undefined
): string | null {
): string | undefined {
if (operation && operation.kind == 'OperationDefinition') {
return operation.operation;
}

return null;
return undefined;
}

private getOperationName(
operation: graphqlTypes.DefinitionNode | undefined,
processedArgs: graphqlTypes.ExecutionArgs
): string | null {
): string | undefined {
if (operation && operation.kind == 'OperationDefinition') {
if (operation.name) {
return operation.name.value;
Expand All @@ -411,7 +412,7 @@ export class GraphQLInstrumentation extends InstrumentationBase {
if (processedArgs.operationName) {
return processedArgs.operationName;
}
return null;
return undefined;
}

private _createExecuteSpan(
Expand All @@ -423,18 +424,14 @@ export class GraphQLInstrumentation extends InstrumentationBase {
const operationType = this.getOperationType(operation);
const operationName = this.getOperationName(operation, processedArgs);

const span = this.tracer.startSpan(createExecuteSpanName());
const span = this.tracer.startSpan(createExecuteSpanName(operationType));

if (operationType) {
span.setAttribute(AttributeNames.OPERATION_TYPE, operationType);
span.updateName(createExecuteSpanName(operationType));
}

if (operationName) {
span.setAttribute(AttributeNames.OPERATION_NAME, operationName);
if (operationType) {
span.updateName(createExecuteSpanName(operationType, operationName));
}
}

if (processedArgs.document?.loc) {
Expand Down
20 changes: 14 additions & 6 deletions plugins/node/opentelemetry-instrumentation-graphql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@
import type * as graphqlTypes from 'graphql';
import * as api from '@opentelemetry/api';
import { GraphQLObjectType } from 'graphql/type/definition';
import {
AllowedOperationTypes,
createResolveSpanName,
SpanNames,
TokenKind,
} from './enum';
import { AllowedOperationTypes, SpanNames, TokenKind } from './enum';
import { AttributeNames } from './enums/AttributeNames';
import { OTEL_GRAPHQL_DATA_SYMBOL, OTEL_PATCHED_SYMBOL } from './symbols';
import {
Expand Down Expand Up @@ -438,3 +433,16 @@ async function safeExecuteInTheMiddleAsync<T>(
return result as T;
}
}

export const createResolveSpanName = (fieldName: string): string => {
return `${SpanNames.RESOLVE}.${fieldName}`;
};

export const createExecuteSpanName = (
operationType: string | undefined
): string => {
if (operationType) {
return `${SpanNames.EXECUTE}.${operationType}`;
}
return `${SpanNames.EXECUTE}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { Span } from '@opentelemetry/api';
import * as assert from 'assert';
import type * as graphqlTypes from 'graphql';
import { GraphQLInstrumentation } from '../src';
import { createExecuteSpanName, SpanNames } from '../src/enum';
import { SpanNames } from '../src/enum';
import { AttributeNames } from '../src/enums/AttributeNames';
import {
GraphQLInstrumentationConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
import * as assert from 'assert';
import { createExecuteSpanName, createResolveSpanName } from '../src/enum';
import { createExecuteSpanName, createResolveSpanName } from '../src/utils';
import { AttributeNames } from '../src/enums/AttributeNames';

export function assertResolveSpan(
Expand Down Expand Up @@ -46,10 +46,7 @@ export function assertExecuteSpan(
parentSpanId?: string
) {
const attrs = span.attributes;
assert.deepStrictEqual(
span.name,
createExecuteSpanName(operationType, operationName)
);
assert.deepStrictEqual(span.name, createExecuteSpanName(operationType));
assert.deepStrictEqual(attrs[AttributeNames.SOURCE], source);
assert.deepStrictEqual(attrs[AttributeNames.OPERATION_NAME], operationName);
assert.deepStrictEqual(attrs[AttributeNames.OPERATION_TYPE], operationType);
Expand Down

0 comments on commit 80fd552

Please sign in to comment.