-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathindex.ts
143 lines (127 loc) · 4.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { print } from 'graphql';
import { isAsyncIterable, OnExecuteHookResult, Plugin } from '@envelop/core';
import { useOnResolve } from '@envelop/on-resolve';
import { SpanAttributes, SpanKind, TracerProvider } from '@opentelemetry/api';
import * as opentelemetry from '@opentelemetry/api';
import {
BasicTracerProvider,
ConsoleSpanExporter,
SimpleSpanProcessor,
} from '@opentelemetry/sdk-trace-base';
export enum AttributeName {
EXECUTION_ERROR = 'graphql.execute.error',
EXECUTION_RESULT = 'graphql.execute.result',
RESOLVER_EXCEPTION = 'graphql.resolver.exception',
RESOLVER_FIELD_NAME = 'graphql.resolver.fieldName',
RESOLVER_TYPE_NAME = 'graphql.resolver.typeName',
RESOLVER_RESULT_TYPE = 'graphql.resolver.resultType',
RESOLVER_ARGS = 'graphql.resolver.args',
EXECUTION_OPERATION_NAME = 'graphql.execute.operationName',
EXECUTION_OPERATION_DOCUMENT = 'graphql.execute.document',
EXECUTION_VARIABLES = 'graphql.execute.variables',
}
const tracingSpanSymbol = Symbol('OPEN_TELEMETRY_GRAPHQL');
export type TracingOptions = {
resolvers: boolean;
variables: boolean;
result: boolean;
};
type PluginContext = {
[tracingSpanSymbol]: opentelemetry.Span;
};
export const useOpenTelemetry = (
options: TracingOptions,
tracingProvider?: TracerProvider,
spanKind: SpanKind = SpanKind.SERVER,
spanAdditionalAttributes: SpanAttributes = {},
serviceName = 'graphql',
): Plugin<PluginContext> => {
if (!tracingProvider) {
const basicTraceProvider = new BasicTracerProvider();
basicTraceProvider.addSpanProcessor(new SimpleSpanProcessor(new ConsoleSpanExporter()));
basicTraceProvider.register();
tracingProvider = basicTraceProvider;
}
const tracer = tracingProvider.getTracer(serviceName);
return {
onPluginInit({ addPlugin }) {
if (options.resolvers) {
addPlugin(
useOnResolve(({ info, context, args }) => {
if (context && typeof context === 'object' && context[tracingSpanSymbol]) {
const ctx = opentelemetry.trace.setSpan(
opentelemetry.context.active(),
context[tracingSpanSymbol],
);
const { fieldName, returnType, parentType } = info;
const resolverSpan = tracer.startSpan(
`${parentType.name}.${fieldName}`,
{
attributes: {
[AttributeName.RESOLVER_FIELD_NAME]: fieldName,
[AttributeName.RESOLVER_TYPE_NAME]: parentType.toString(),
[AttributeName.RESOLVER_RESULT_TYPE]: returnType.toString(),
[AttributeName.RESOLVER_ARGS]: JSON.stringify(args || {}),
},
},
ctx,
);
return ({ result }) => {
if (result instanceof Error) {
resolverSpan.recordException({
name: AttributeName.RESOLVER_EXCEPTION,
message: JSON.stringify(result),
});
} else {
resolverSpan.end();
}
};
}
return () => {};
}),
);
}
},
onExecute({ args, extendContext }) {
const executionSpan = tracer.startSpan(`${args.operationName || 'Anonymous Operation'}`, {
kind: spanKind,
attributes: {
...spanAdditionalAttributes,
[AttributeName.EXECUTION_OPERATION_NAME]: args.operationName ?? undefined,
[AttributeName.EXECUTION_OPERATION_DOCUMENT]: print(args.document),
...(options.variables
? { [AttributeName.EXECUTION_VARIABLES]: JSON.stringify(args.variableValues ?? {}) }
: {}),
},
});
const resultCbs: OnExecuteHookResult<PluginContext> = {
onExecuteDone({ result }) {
if (isAsyncIterable(result)) {
executionSpan.end();
// eslint-disable-next-line no-console
console.warn(
`Plugin "newrelic" encountered a AsyncIterator which is not supported yet, so tracing data is not available for the operation.`,
);
return;
}
if (result.data && options.result) {
executionSpan.setAttribute(AttributeName.EXECUTION_RESULT, JSON.stringify(result));
}
if (result.errors && result.errors.length > 0) {
executionSpan.recordException({
name: AttributeName.EXECUTION_ERROR,
message: JSON.stringify(result.errors),
});
}
executionSpan.end();
},
};
if (options.resolvers) {
extendContext({
[tracingSpanSymbol]: executionSpan,
});
}
return resultCbs;
},
};
};