-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathapollo.ts
192 lines (162 loc) · 5.51 KB
/
apollo.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import type { Hub } from '@sentry/core';
import type { EventProcessor, Integration } from '@sentry/types';
import { arrayify, fill, isThenable, loadModule, logger } from '@sentry/utils';
import { shouldDisableAutoInstrumentation } from './utils/node-utils';
interface ApolloOptions {
useNestjs?: boolean;
}
type ApolloResolverGroup = {
[key: string]: () => unknown;
};
type ApolloModelResolvers = {
[key: string]: ApolloResolverGroup;
};
/** Tracing integration for Apollo */
export class Apollo implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'Apollo';
/**
* @inheritDoc
*/
public name: string = Apollo.id;
private readonly _useNest: boolean;
/**
* @inheritDoc
*/
public constructor(
options: ApolloOptions = {
useNestjs: false,
},
) {
this._useNest = !!options.useNestjs;
}
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('Apollo Integration is skipped because of instrumenter configuration.');
return;
}
if (this._useNest) {
const pkg = loadModule<{
GraphQLFactory: {
prototype: {
create: (resolvers: ApolloModelResolvers[]) => unknown;
};
};
}>('@nestjs/graphql');
if (!pkg) {
__DEBUG_BUILD__ && logger.error('Apollo-NestJS Integration was unable to require @nestjs/graphql package.');
return;
}
/**
* Iterate over resolvers of NestJS ResolversExplorerService before schemas are constructed.
*/
fill(
pkg.GraphQLFactory.prototype,
'mergeWithSchema',
function (orig: (this: unknown, ...args: unknown[]) => unknown) {
return function (
this: { resolversExplorerService: { explore: () => ApolloModelResolvers[] } },
...args: unknown[]
) {
fill(this.resolversExplorerService, 'explore', function (orig: () => ApolloModelResolvers[]) {
return function (this: unknown) {
const resolvers = arrayify(orig.call(this));
const instrumentedResolvers = instrumentResolvers(resolvers, getCurrentHub);
return instrumentedResolvers;
};
});
return orig.call(this, ...args);
};
},
);
} else {
const pkg = loadModule<{
ApolloServerBase: {
prototype: {
constructSchema: (config: unknown) => unknown;
};
};
}>('apollo-server-core');
if (!pkg) {
__DEBUG_BUILD__ && logger.error('Apollo Integration was unable to require apollo-server-core package.');
return;
}
/**
* Iterate over resolvers of the ApolloServer instance before schemas are constructed.
*/
fill(pkg.ApolloServerBase.prototype, 'constructSchema', function (orig: (config: unknown) => unknown) {
return function (this: {
config: { resolvers?: ApolloModelResolvers[]; schema?: unknown; modules?: unknown };
}) {
if (!this.config.resolvers) {
if (__DEBUG_BUILD__) {
if (this.config.schema) {
logger.warn(
'Apollo integration is not able to trace `ApolloServer` instances constructed via `schema` property.' +
'If you are using NestJS with Apollo, please use `Sentry.Integrations.Apollo({ useNestjs: true })` instead.',
);
logger.warn();
} else if (this.config.modules) {
logger.warn(
'Apollo integration is not able to trace `ApolloServer` instances constructed via `modules` property.',
);
}
logger.error('Skipping tracing as no resolvers found on the `ApolloServer` instance.');
}
return orig.call(this);
}
const resolvers = arrayify(this.config.resolvers);
this.config.resolvers = instrumentResolvers(resolvers, getCurrentHub);
return orig.call(this);
};
});
}
}
}
function instrumentResolvers(resolvers: ApolloModelResolvers[], getCurrentHub: () => Hub): ApolloModelResolvers[] {
return resolvers.map(model => {
Object.keys(model).forEach(resolverGroupName => {
Object.keys(model[resolverGroupName]).forEach(resolverName => {
if (typeof model[resolverGroupName][resolverName] !== 'function') {
return;
}
wrapResolver(model, resolverGroupName, resolverName, getCurrentHub);
});
});
return model;
});
}
/**
* Wrap a single resolver which can be a parent of other resolvers and/or db operations.
*/
function wrapResolver(
model: ApolloModelResolvers,
resolverGroupName: string,
resolverName: string,
getCurrentHub: () => Hub,
): void {
fill(model[resolverGroupName], resolverName, function (orig: () => unknown | Promise<unknown>) {
return function (this: unknown, ...args: unknown[]) {
const scope = getCurrentHub().getScope();
const parentSpan = scope?.getSpan();
const span = parentSpan?.startChild({
description: `${resolverGroupName}.${resolverName}`,
op: 'graphql.resolve',
});
const rv = orig.call(this, ...args);
if (isThenable(rv)) {
return rv.then((res: unknown) => {
span?.finish();
return res;
});
}
span?.finish();
return rv;
};
});
}