-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTracePlugin.ts
129 lines (112 loc) · 3.75 KB
/
TracePlugin.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
import { SchemaTransform, mapSchema, MapperKind } from "@graphql-tools/utils";
import { defaultFieldResolver, DocumentNode, GraphQLSchema } from "graphql";
import { Path } from "graphql/jsutils/Path";
import { Middleware, ParameterizedContext } from "koa";
import { GraphQLPlugin } from "./GraphQLPlugin";
import { ServiceContext } from "./GraphQLServer";
/**
* Plugin implementing Apollo Tracing.
*/
export class TracePlugin implements GraphQLPlugin {
directives(): (string | DocumentNode)[] {
return [];
}
transforms(): SchemaTransform[] {
return [
(schema: GraphQLSchema) => mapSchema(schema, {
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
const { resolve = defaultFieldResolver } = fieldConfig;
fieldConfig.resolve = async (source, args, context: TracePluginContext, info) => {
const start = process.hrtime.bigint();
const path = buildPath(info.path);
const result = await resolve(source, args, context, info);
const end = process.hrtime.bigint();
const pluginContext = context.ctx.tracePlugin;
const trace: ResolverTrace = {
path,
parentType: info.parentType.name,
fieldName: info.fieldName,
returnType: info.returnType.toString(),
startOffset: Number(start - pluginContext.requestStart),
duration: Number(end - start),
};
const traces: ResolverTrace[] = pluginContext.traces;
traces.push(trace);
return result;
};
return fieldConfig;
}
}),
];
}
middleware: Middleware = async (ctx, next) => {
const context = ctx as unknown as TraceContext;
const traceContext = {
requestStart: process.hrtime.bigint(),
traces: [],
};
context.tracePlugin = traceContext;
const startTime = new Date();
await next();
const requestEnd = process.hrtime.bigint();
const endTime = new Date();
const body = ctx.response.body;
if (!body.extensions) {
body.extensions = {};
}
const tracing: GraphQLTrace = {
version: 1,
startTime: startTime.toISOString(),
endTime: endTime.toISOString(),
duration: Number(requestEnd - traceContext.requestStart),
execution: {
resolvers: traceContext.traces,
}
};
body.extensions.tracing = tracing;
}
}
function buildPath(path: Path): string[] {
const result: string[] = [];
let here: Path | undefined = path;
while (here) {
result.unshift(path.key.toString());
here = here.prev;
}
return result;
}
type TraceContext = ParameterizedContext & {
tracePlugin: {
requestStart: bigint;
traces: ResolverTrace[];
}
}
interface TracePluginContext extends ServiceContext {
ctx: TraceContext;
}
interface ResolverTrace {
path?: string[];
parentType?: string;
fieldName?: string;
returnType?: string;
startOffset: number;
duration: number;
}
interface GraphQLTrace {
version: number;
startTime: string;
endTime: string;
duration: number;
parsing?: {
startOffset: number;
duration: number;
}
validation?: {
startOffset: number;
duration: number;
}
execution?: {
resolvers: ResolverTrace[];
}
}
export default TracePlugin;