-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtracing.ts
More file actions
35 lines (32 loc) · 1.01 KB
/
tracing.ts
File metadata and controls
35 lines (32 loc) · 1.01 KB
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
import { trace as OpenTelemetryTrace } from "@opentelemetry/api";
import { AttributeNames, SpanNames } from "@pothos/tracing-opentelemetry";
import { print } from "graphql";
import { Plugin } from "graphql-yoga";
export const trace = OpenTelemetryTrace;
export const tracer = OpenTelemetryTrace.getTracer("graphql-api");
export const tracingPlugin: Plugin = {
onExecute: ({ setExecuteFn, executeFn }) => {
setExecuteFn((options) =>
tracer.startActiveSpan(
SpanNames.EXECUTE,
{
attributes: {
[AttributeNames.OPERATION_NAME]: options.operationName ?? undefined,
[AttributeNames.SOURCE]: print(options.document),
},
},
async (span) => {
try {
const result = (await executeFn(options)) as unknown;
return result;
} catch (error) {
span.recordException(error as Error);
throw error;
} finally {
span.end();
}
},
),
);
},
};