Skip to content

Commit d6e7431

Browse files
authored
feat(deno): add express integration (#22461)
1 parent b001079 commit d6e7431

4 files changed

Lines changed: 107 additions & 0 deletions

File tree

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

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export type { DenoRedisIntegrationOptions } from './integrations/redis';
118118
export {
119119
amqplibChannelIntegration,
120120
dataloaderChannelIntegration,
121+
expressChannelIntegration,
121122
genericPoolChannelIntegration,
122123
knexChannelIntegration,
123124
koaChannelIntegration,

packages/deno/src/sdk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '@sentry/core';
1414
import {
1515
amqplibChannelIntegration,
16+
expressChannelIntegration,
1617
genericPoolChannelIntegration,
1718
koaChannelIntegration,
1819
lruMemoizerChannelIntegration,
@@ -77,6 +78,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
7778
...(MODULE_REGISTER_HOOKS_SUPPORTED
7879
? [
7980
amqplibChannelIntegration(),
81+
expressChannelIntegration(),
8082
genericPoolChannelIntegration(),
8183
koaChannelIntegration(),
8284
lruMemoizerChannelIntegration(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ snapshot[`captureException 1`] = `
116116
"DenoHttp",
117117
"DenoRedis",
118118
"Amqplib",
119+
"Express",
119120
"GenericPool",
120121
"Koa",
121122
"LruMemoizer",
@@ -202,6 +203,7 @@ snapshot[`captureMessage 1`] = `
202203
"DenoHttp",
203204
"DenoRedis",
204205
"Amqplib",
206+
"Express",
205207
"GenericPool",
206208
"Koa",
207209
"LruMemoizer",
@@ -295,6 +297,7 @@ snapshot[`captureMessage twice 1`] = `
295297
"DenoHttp",
296298
"DenoRedis",
297299
"Amqplib",
300+
"Express",
298301
"GenericPool",
299302
"Koa",
300303
"LruMemoizer",
@@ -395,6 +398,7 @@ snapshot[`captureMessage twice 2`] = `
395398
"DenoHttp",
396399
"DenoRedis",
397400
"Amqplib",
401+
"Express",
398402
"GenericPool",
399403
"Koa",
400404
"LruMemoizer",

0 commit comments

Comments
 (0)