-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
graphql.ts
66 lines (53 loc) · 1.75 KB
/
graphql.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
import type { Hub } from '@sentry/core';
import type { EventProcessor, Integration } from '@sentry/types';
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
import { shouldDisableAutoInstrumentation } from './utils/node-utils';
/** Tracing integration for graphql package */
export class GraphQL implements Integration {
/**
* @inheritDoc
*/
public static id: string = 'GraphQL';
/**
* @inheritDoc
*/
public name: string = GraphQL.id;
/**
* @inheritDoc
*/
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('GraphQL Integration is skipped because of instrumenter configuration.');
return;
}
const pkg = loadModule<{
[method: string]: (...args: unknown[]) => unknown;
}>('graphql/execution/execute.js');
if (!pkg) {
__DEBUG_BUILD__ && logger.error('GraphQL Integration was unable to require graphql/execution package.');
return;
}
fill(pkg, 'execute', function (orig: () => void | Promise<unknown>) {
return function (this: unknown, ...args: unknown[]) {
const scope = getCurrentHub().getScope();
const parentSpan = scope?.getSpan();
const span = parentSpan?.startChild({
description: 'execute',
op: 'graphql.execute',
});
scope?.setSpan(span);
const rv = orig.call(this, ...args);
if (isThenable(rv)) {
return rv.then((res: unknown) => {
span?.finish();
scope?.setSpan(parentSpan);
return res;
});
}
span?.finish();
scope?.setSpan(parentSpan);
return rv;
};
});
}
}