Skip to content

Commit 12b5dc9

Browse files
committed
[WIP] - Add basic GraphQL tracing functionality
1 parent b5fd785 commit 12b5dc9

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

packages/tracing/src/hubextensions.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,12 @@ function _autoloadDatabaseIntegrations(): void {
237237
}
238238

239239
const packageToIntegrationMapping: Record<string, () => Integration> = {
240+
graphql() {
241+
const integration = dynamicRequire(module, './integrations/graphql') as {
242+
GraphQL: IntegrationClass<Integration>;
243+
};
244+
return new integration.GraphQL();
245+
},
240246
mongodb() {
241247
const integration = dynamicRequire(module, './integrations/node/mongo') as {
242248
Mongo: IntegrationClass<Integration>;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/* eslint-disable @typescript-eslint/no-unused-vars */
2+
/* eslint-disable no-console */
3+
/* eslint-disable no-debugger */
4+
import { Hub } from '@sentry/hub';
5+
import { EventProcessor, Integration } from '@sentry/types';
6+
import { fill, isThenable, loadModule, logger } from '@sentry/utils';
7+
8+
/** Tracing integration for graphql package */
9+
export class GraphQL implements Integration {
10+
/**
11+
* @inheritDoc
12+
*/
13+
public static id: string = 'GraphQL';
14+
15+
/**
16+
* @inheritDoc
17+
*/
18+
public name: string = GraphQL.id;
19+
20+
/**
21+
* @inheritDoc
22+
*/
23+
public setupOnce(_: (callback: EventProcessor) => void, getCurrentHub: () => Hub): void {
24+
const pkg = loadModule<{
25+
[method: string]: (...args: unknown[]) => unknown;
26+
}>(`graphql/execution/execute.js`);
27+
28+
debugger;
29+
if (!pkg) {
30+
logger.error(`GraphQL Integration was unable to require @graphql/execution package.`);
31+
return;
32+
}
33+
34+
['execute', 'defaultFieldResolver', 'defaultTypeResolver', 'buildExecutionContext'].forEach(method => {
35+
fill(pkg, method, function(orig: () => void | Promise<unknown>) {
36+
return function(this: unknown, ...args: unknown[]) {
37+
const scope = getCurrentHub().getScope();
38+
const parentSpan = scope?.getSpan();
39+
40+
const span = parentSpan?.startChild({
41+
description: method,
42+
op: 'graphql',
43+
});
44+
45+
const rv = orig.call(this, ...args) as Promise<unknown>;
46+
47+
if (isThenable(rv)) {
48+
return rv.then((res: unknown) => {
49+
span?.finish();
50+
return res;
51+
});
52+
} else {
53+
span?.finish();
54+
return rv;
55+
}
56+
};
57+
});
58+
});
59+
}
60+
}

packages/tracing/src/integrations/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export { Express } from './node/express';
22
export { Postgres } from './node/postgres';
33
export { Mysql } from './node/mysql';
44
export { Mongo } from './node/mongo';
5+
export { GraphQL } from './graphql';
56

67
// TODO(v7): Remove this export
78
// Please see `src/index.ts` for more details.

0 commit comments

Comments
 (0)