Skip to content
Open
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
8 changes: 4 additions & 4 deletions packages/instrumentation-graphql/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {

/** @knipignore */
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
import * as api from '@opentelemetry/api';
import type { Span } from '@opentelemetry/api';
import type { PromiseOrValue } from 'graphql/jsutils/PromiseOrValue';
import {
GraphQLInstrumentationConfig,
Expand Down Expand Up @@ -218,7 +218,7 @@ export class GraphQLInstrumentation extends InstrumentationBase<GraphQLInstrumen
}

private _handleExecutionResult(
span: api.Span,
span: Span,
err?: Error,
result?: PromiseOrValue<graphqlTypes.ExecutionResult>
) {
Expand Down Expand Up @@ -251,7 +251,7 @@ export class GraphQLInstrumentation extends InstrumentationBase<GraphQLInstrumen
}

private _executeResponseHook(
span: api.Span,
span: Span,
result: graphqlTypes.ExecutionResult
) {
const { responseHook } = this.getConfig();
Expand Down Expand Up @@ -384,7 +384,7 @@ export class GraphQLInstrumentation extends InstrumentationBase<GraphQLInstrumen
private _createExecuteSpan(
operation: graphqlTypes.DefinitionNode | undefined,
processedArgs: graphqlTypes.ExecutionArgs
): api.Span {
): Span {
const config = this.getConfig();

const span = this.tracer.startSpan(SpanNames.EXECUTE, {});
Expand Down
88 changes: 40 additions & 48 deletions packages/instrumentation-graphql/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

import type * as graphqlTypes from 'graphql';
import * as api from '@opentelemetry/api';
import type { Span, SpanAttributes, Tracer } from '@opentelemetry/api';
import { SpanStatusCode, context, trace } from '@opentelemetry/api';
import { AllowedOperationTypes, SpanNames, TokenKind } from './enum';
import { AttributeNames } from './enums/AttributeNames';
import { OTEL_GRAPHQL_DATA_SYMBOL, OTEL_PATCHED_SYMBOL } from './symbols';
Expand All @@ -30,7 +31,7 @@ const isObjectLike = (value: unknown): value is { [key: string]: unknown } => {
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function addInputVariableAttribute(span: api.Span, key: string, variable: any) {
function addInputVariableAttribute(span: Span, key: string, variable: any) {
if (Array.isArray(variable)) {
variable.forEach((value, idx) => {
addInputVariableAttribute(span, `${key}.${idx}`, value);
Expand All @@ -45,7 +46,7 @@ function addInputVariableAttribute(span: api.Span, key: string, variable: any) {
}

export function addInputVariableAttributes(
span: api.Span,
span: Span,
variableValues: { [key: string]: any }
) {
Object.entries(variableValues).forEach(([key, value]) => {
Expand All @@ -54,7 +55,7 @@ export function addInputVariableAttributes(
}

export function addSpanSource(
span: api.Span,
span: Span,
loc?: graphqlTypes.Location,
allowValues?: boolean,
start?: number,
Expand All @@ -65,7 +66,7 @@ export function addSpanSource(
}

function createFieldIfNotExists(
tracer: api.Tracer,
tracer: Tracer,
getConfig: () => GraphQLInstrumentationParsedConfig,
contextValue: any,
info: graphqlTypes.GraphQLResolveInfo,
Expand Down Expand Up @@ -101,14 +102,14 @@ function createFieldIfNotExists(
}

function createResolverSpan(
tracer: api.Tracer,
tracer: Tracer,
getConfig: () => GraphQLInstrumentationParsedConfig,
contextValue: any,
info: graphqlTypes.GraphQLResolveInfo,
path: string[],
parentSpan?: api.Span
): api.Span {
const attributes: api.SpanAttributes = {
parentSpan?: Span
): Span {
const attributes: SpanAttributes = {
[AttributeNames.FIELD_NAME]: info.fieldName,
[AttributeNames.FIELD_PATH]: path.join('.'),
[AttributeNames.FIELD_TYPE]: info.returnType.toString(),
Expand All @@ -120,7 +121,7 @@ function createResolverSpan(
{
attributes,
},
parentSpan ? api.trace.setSpan(api.context.active(), parentSpan) : undefined
parentSpan ? trace.setSpan(context.active(), parentSpan) : undefined
);

const document = contextValue[OTEL_GRAPHQL_DATA_SYMBOL].source;
Expand All @@ -141,7 +142,7 @@ function createResolverSpan(
return span;
}

export function endSpan(span: api.Span, error?: Error): void {
export function endSpan(span: Span, error?: Error): void {
if (error) {
span.recordException(error);
}
Expand Down Expand Up @@ -180,7 +181,7 @@ function getField(contextValue: any, path: string[]): GraphQLField {
return contextValue[OTEL_GRAPHQL_DATA_SYMBOL].fields[path.join('.')];
}

function getParentFieldSpan(contextValue: any, path: string[]): api.Span {
function getParentFieldSpan(contextValue: any, path: string[]): Span {
for (let i = path.length - 1; i > 0; i--) {
const field = getField(contextValue, path.slice(0, i));

Expand All @@ -192,7 +193,7 @@ function getParentFieldSpan(contextValue: any, path: string[]): api.Span {
return getRootSpan(contextValue);
}

function getRootSpan(contextValue: any): api.Span {
function getRootSpan(contextValue: any): Span {
return contextValue[OTEL_GRAPHQL_DATA_SYMBOL].span;
}

Expand Down Expand Up @@ -292,7 +293,7 @@ export function getSourceFromLocation(

export function wrapFields(
type: Maybe<graphqlTypes.GraphQLObjectType & OtelPatched>,
tracer: api.Tracer,
tracer: Tracer,
getConfig: () => GraphQLInstrumentationParsedConfig
): void {
if (!type || type[OTEL_PATCHED_SYMBOL]) {
Expand Down Expand Up @@ -356,7 +357,7 @@ function isGraphQLObjectType(
}

const handleResolveSpanError = (
resolveSpan: api.Span,
resolveSpan: Span,
err: any,
shouldEndSpan: boolean
) => {
Expand All @@ -365,14 +366,14 @@ const handleResolveSpanError = (
}
resolveSpan.recordException(err);
resolveSpan.setStatus({
code: api.SpanStatusCode.ERROR,
code: SpanStatusCode.ERROR,
message: err.message,
});
resolveSpan.end();
};

const handleResolveSpanSuccess = (
resolveSpan: api.Span,
resolveSpan: Span,
shouldEndSpan: boolean
) => {
if (!shouldEndSpan) {
Expand All @@ -382,7 +383,7 @@ const handleResolveSpanSuccess = (
};

export function wrapFieldResolver<TSource = any, TContext = any, TArgs = any>(
tracer: api.Tracer,
tracer: Tracer,
getConfig: () => GraphQLInstrumentationParsedConfig,
fieldResolver: Maybe<
graphqlTypes.GraphQLFieldResolver<TSource, TContext, TArgs> & OtelPatched
Expand Down Expand Up @@ -434,7 +435,7 @@ export function wrapFieldResolver<TSource = any, TContext = any, TArgs = any>(
const path = pathToArray(config.mergeItems, info && info.path);
const depth = path.filter((item: any) => typeof item === 'string').length;

let span: api.Span;
let span: Span;
let shouldEndSpan = false;
if (config.depth >= 0 && config.depth < depth) {
span = getParentFieldSpan(contextValue, path);
Expand All @@ -450,38 +451,29 @@ export function wrapFieldResolver<TSource = any, TContext = any, TArgs = any>(
shouldEndSpan = spanAdded;
}

return api.context.with(
api.trace.setSpan(api.context.active(), span),
() => {
try {
const res = fieldResolver.call(
this,
source,
args,
contextValue,
info
return context.with(trace.setSpan(context.active(), span), () => {
try {
const res = fieldResolver.call(this, source, args, contextValue, info);
if (isPromise(res)) {
return res.then(
(r: any) => {
handleResolveSpanSuccess(span, shouldEndSpan);
return r;
},
(err: Error) => {
handleResolveSpanError(span, err, shouldEndSpan);
throw err;
}
);
if (isPromise(res)) {
return res.then(
(r: any) => {
handleResolveSpanSuccess(span, shouldEndSpan);
return r;
},
(err: Error) => {
handleResolveSpanError(span, err, shouldEndSpan);
throw err;
}
);
} else {
handleResolveSpanSuccess(span, shouldEndSpan);
return res;
}
} catch (err: any) {
handleResolveSpanError(span, err, shouldEndSpan);
throw err;
} else {
handleResolveSpanSuccess(span, shouldEndSpan);
return res;
}
} catch (err: any) {
handleResolveSpanError(span, err, shouldEndSpan);
throw err;
}
);
});
}

(wrappedFieldResolver as OtelPatched)[OTEL_PATCHED_SYMBOL] = true;
Expand Down