Skip to content

Commit cd70a91

Browse files
committed
feat(deno): add graphql integration
1 parent c82daae commit cd70a91

5 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// <reference lib="deno.ns" />
2+
3+
import { tracingChannel } from 'node:diagnostics_channel';
4+
import type { TransactionEvent } from '@sentry/core';
5+
import type { DenoClient } from '@sentry/deno';
6+
import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '@sentry/deno';
7+
import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts';
8+
import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts';
9+
import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts';
10+
11+
function resetGlobals(): void {
12+
getCurrentScope().clear();
13+
getCurrentScope().setClient(undefined);
14+
getIsolationScope().clear();
15+
getGlobalScope().clear();
16+
}
17+
18+
/** See deno-redis.test.ts — same sink shape, deduped for clarity. */
19+
function transactionSink(): {
20+
beforeSendTransaction: (event: TransactionEvent) => null;
21+
waitFor: (predicate: (event: TransactionEvent) => boolean) => Promise<TransactionEvent>;
22+
} {
23+
const transactions: TransactionEvent[] = [];
24+
const waiters: { predicate: (e: TransactionEvent) => boolean; resolve: (e: TransactionEvent) => void }[] = [];
25+
return {
26+
beforeSendTransaction(event) {
27+
transactions.push(event);
28+
for (let i = waiters.length - 1; i >= 0; i--) {
29+
const w = waiters[i]!;
30+
if (w.predicate(event)) {
31+
waiters.splice(i, 1);
32+
w.resolve(event);
33+
}
34+
}
35+
return null;
36+
},
37+
waitFor(predicate) {
38+
const already = transactions.find(predicate);
39+
if (already) return Promise.resolve(already);
40+
return new Promise<TransactionEvent>(resolve => {
41+
waiters.push({ predicate, resolve });
42+
});
43+
},
44+
};
45+
}
46+
47+
function withTimeout<T>(p: Promise<T>, ms: number, what: string): Promise<T> {
48+
let timer: ReturnType<typeof setTimeout> | undefined;
49+
const timeout = new Promise<T>((_, reject) => {
50+
timer = setTimeout(() => reject(new Error(`Timed out waiting for ${what} after ${ms}ms`)), ms);
51+
});
52+
return Promise.race([p, timeout]).finally(() => {
53+
if (timer !== undefined) clearTimeout(timer);
54+
});
55+
}
56+
57+
// Drives one graphql parse channel and asserts the resulting nested span. The
58+
// composed integration subscribes to both the orchestrion channel (graphql
59+
// v14–16) and graphql v17's native `graphql:parse` channel; both emit the same
60+
// `graphql.parse` span, so exercising each channel proves that half is wired.
61+
async function assertParseSpan(channelName: string): Promise<void> {
62+
resetGlobals();
63+
const sink = transactionSink();
64+
init({
65+
dsn: 'https://username@domain/123',
66+
tracesSampleRate: 1,
67+
beforeSendTransaction: sink.beforeSendTransaction,
68+
});
69+
70+
const channel = tracingChannel(channelName);
71+
const ctx = { arguments: [] };
72+
73+
startSpan({ name: 'parent', op: 'test' }, () => {
74+
channel.start.runStores(ctx, () => {
75+
channel.end.publish(ctx);
76+
});
77+
channel.asyncStart.runStores(ctx, () => {
78+
channel.asyncEnd.publish(ctx);
79+
});
80+
});
81+
82+
const parent = await withTimeout(
83+
sink.waitFor(t => t.transaction === 'parent'),
84+
5000,
85+
"'parent' transaction",
86+
);
87+
88+
const parseSpan = parent.spans?.find(s => s.description === 'graphql.parse');
89+
assertExists(parseSpan, `expected a graphql.parse span, got: ${parent.spans?.map(s => s.description).join(', ')}`);
90+
assertEquals(parseSpan!.op, 'graphql');
91+
assertEquals(parseSpan!.data?.['sentry.origin'], 'auto.graphql.diagnostic_channel');
92+
}
93+
94+
Deno.test('graphql instrumentation: included in default integrations (Deno 2.8.0+)', () => {
95+
resetGlobals();
96+
const client = init({ dsn: 'https://username@domain/123' }) as DenoClient;
97+
const names = client.getOptions().integrations.map(i => i.name);
98+
assert(names.includes('Graphql'), `Graphql should be in defaults, got ${names.join(', ')}`);
99+
});
100+
101+
Deno.test('graphql instrumentation: orchestrion:graphql:parse channel produces a nested span (v14–16)', async () => {
102+
await assertParseSpan('orchestrion:graphql:parse');
103+
});
104+
105+
Deno.test('graphql instrumentation: native graphql:parse channel produces a nested span (v17)', async () => {
106+
await assertParseSpan('graphql:parse');
107+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export {
120120
dataloaderChannelIntegration,
121121
expressChannelIntegration,
122122
genericPoolChannelIntegration,
123+
graphqlDiagnosticsChannelIntegration,
123124
hapiChannelIntegration,
124125
kafkajsChannelIntegration,
125126
knexChannelIntegration,

packages/deno/src/sdk.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
amqplibChannelIntegration,
1616
expressChannelIntegration,
1717
genericPoolChannelIntegration,
18+
graphqlDiagnosticsChannelIntegration,
1819
hapiChannelIntegration,
1920
kafkajsChannelIntegration,
2021
koaChannelIntegration,
@@ -70,6 +71,11 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
7071
: []),
7172
// node:diagnostics_channel.tracingChannel exists on Deno 1.44.3+.
7273
...(TRACING_CHANNEL_SUPPORTED ? [denoRedisIntegration()] : []),
74+
// graphql is gated on tracingChannel rather than the module hook: the
75+
// composed integration also subscribes to graphql v17's native diagnostics
76+
// channels, which need only tracingChannel. The orchestrion implementation
77+
// (graphql v14–16) stays inert until the runtime hook injects those channels.
78+
...(TRACING_CHANNEL_SUPPORTED ? [graphqlDiagnosticsChannelIntegration()] : []),
7379
// orchestrion-based instrumentations. We add a deliberate list here rather
7480
// than every channel integration: each one needs a Deno test proving it
7581
// records spans.

packages/deno/test/__snapshots__/mod.test.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ snapshot[`captureException 1`] = `
115115
"DenoServe",
116116
"DenoHttp",
117117
"DenoRedis",
118+
"Graphql",
118119
"Amqplib",
119120
"Express",
120121
"GenericPool",
@@ -204,6 +205,7 @@ snapshot[`captureMessage 1`] = `
204205
"DenoServe",
205206
"DenoHttp",
206207
"DenoRedis",
208+
"Graphql",
207209
"Amqplib",
208210
"Express",
209211
"GenericPool",
@@ -300,6 +302,7 @@ snapshot[`captureMessage twice 1`] = `
300302
"DenoServe",
301303
"DenoHttp",
302304
"DenoRedis",
305+
"Graphql",
303306
"Amqplib",
304307
"Express",
305308
"GenericPool",
@@ -403,6 +406,7 @@ snapshot[`captureMessage twice 2`] = `
403406
"DenoServe",
404407
"DenoHttp",
405408
"DenoRedis",
409+
"Graphql",
406410
"Amqplib",
407411
"Express",
408412
"GenericPool",

packages/server-utils/src/orchestrion/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export {
4747
genericPoolChannelIntegration,
4848
googleGenAIChannelIntegration,
4949
graphqlChannelIntegration,
50+
graphqlDiagnosticsChannelIntegration,
5051
hapiChannelIntegration,
5152
koaChannelIntegration,
5253
ioredisChannelIntegration,

0 commit comments

Comments
 (0)