diff --git a/dev-packages/deno-integration-tests/suites/orchestrion-express/test.ts b/dev-packages/deno-integration-tests/suites/orchestrion-express/test.ts new file mode 100644 index 000000000000..1e29b96b5314 --- /dev/null +++ b/dev-packages/deno-integration-tests/suites/orchestrion-express/test.ts @@ -0,0 +1,100 @@ +// + +import { EventEmitter } from 'node:events'; +import { tracingChannel } from 'node:diagnostics_channel'; +import type { TransactionEvent } from '@sentry/core'; +import type { DenoClient } from '@sentry/deno'; +import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '@sentry/deno'; +import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts'; +import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts'; +import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts'; + +function resetGlobals(): void { + getCurrentScope().clear(); + getCurrentScope().setClient(undefined); + getIsolationScope().clear(); + getGlobalScope().clear(); +} + +/** See deno-redis.test.ts — same sink shape, deduped for clarity. */ +function transactionSink(): { + beforeSendTransaction: (event: TransactionEvent) => null; + waitFor: (predicate: (event: TransactionEvent) => boolean) => Promise; +} { + const transactions: TransactionEvent[] = []; + const waiters: { predicate: (e: TransactionEvent) => boolean; resolve: (e: TransactionEvent) => void }[] = []; + return { + beforeSendTransaction(event) { + transactions.push(event); + for (let i = waiters.length - 1; i >= 0; i--) { + const w = waiters[i]!; + if (w.predicate(event)) { + waiters.splice(i, 1); + w.resolve(event); + } + } + return null; + }, + waitFor(predicate) { + const already = transactions.find(predicate); + if (already) return Promise.resolve(already); + return new Promise(resolve => { + waiters.push({ predicate, resolve }); + }); + }, + }; +} + +function withTimeout(p: Promise, ms: number, what: string): Promise { + let timer: ReturnType | undefined; + const timeout = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(`Timed out waiting for ${what} after ${ms}ms`)), ms); + }); + return Promise.race([p, timeout]).finally(() => { + if (timer !== undefined) clearTimeout(timer); + }); +} + +Deno.test('express instrumentation: included in default integrations (Deno 2.8.0+)', () => { + resetGlobals(); + const client = init({ dsn: 'https://username@domain/123' }) as DenoClient; + const names = client.getOptions().integrations.map(i => i.name); + assert(names.includes('Express'), `Express should be in defaults, got ${names.join(', ')}`); +}); + +Deno.test('express instrumentation: orchestrion:express:handle channel produces a nested middleware span', async () => { + resetGlobals(); + const sink = transactionSink(); + init({ + dsn: 'https://username@domain/123', + tracesSampleRate: 1, + beforeSendTransaction: sink.beforeSendTransaction, + }); + + const channel = tracingChannel('orchestrion:express:handle'); + + // `self` is the routing layer; `arguments` are `[req, res, next]`. A 3-arg + // handler that isn't a router or route-dispatch is traced as a middleware. + const layer = { name: 'myMiddleware', handle: (_req: unknown, _res: unknown, _next: unknown) => undefined }; + const res = new EventEmitter(); + const ctx = { self: layer, arguments: [{}, res, () => undefined] }; + + startSpan({ name: 'parent', op: 'test' }, () => { + channel.start.runStores(ctx, () => undefined); + channel.asyncStart.runStores(ctx, () => undefined); + channel.asyncEnd.publish(ctx); + }); + + const parent = await withTimeout( + sink.waitFor(t => t.transaction === 'parent'), + 5000, + "'parent' transaction", + ); + + const expressSpan = parent.spans?.find(s => s.op === 'middleware.express'); + assertExists(expressSpan, `expected a middleware.express span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`); + assertEquals(expressSpan!.description, 'myMiddleware'); + assertEquals(expressSpan!.data?.['express.name'], 'myMiddleware'); + assertEquals(expressSpan!.data?.['express.type'], 'middleware'); + assertEquals(expressSpan!.data?.['sentry.origin'], 'auto.http.express'); +}); diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index 404bdbabb833..7bfba75dbb2e 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -118,6 +118,7 @@ export type { DenoRedisIntegrationOptions } from './integrations/redis'; export { amqplibChannelIntegration, dataloaderChannelIntegration, + expressChannelIntegration, genericPoolChannelIntegration, knexChannelIntegration, koaChannelIntegration, diff --git a/packages/deno/src/sdk.ts b/packages/deno/src/sdk.ts index 78c85bd29a2c..789dac54d8f4 100644 --- a/packages/deno/src/sdk.ts +++ b/packages/deno/src/sdk.ts @@ -13,6 +13,7 @@ import { } from '@sentry/core'; import { amqplibChannelIntegration, + expressChannelIntegration, genericPoolChannelIntegration, koaChannelIntegration, lruMemoizerChannelIntegration, @@ -77,6 +78,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] { ...(MODULE_REGISTER_HOOKS_SUPPORTED ? [ amqplibChannelIntegration(), + expressChannelIntegration(), genericPoolChannelIntegration(), koaChannelIntegration(), lruMemoizerChannelIntegration(), diff --git a/packages/deno/test/__snapshots__/mod.test.ts.snap b/packages/deno/test/__snapshots__/mod.test.ts.snap index a0aa2ff47068..cf5455ad9e02 100644 --- a/packages/deno/test/__snapshots__/mod.test.ts.snap +++ b/packages/deno/test/__snapshots__/mod.test.ts.snap @@ -116,6 +116,7 @@ snapshot[`captureException 1`] = ` "DenoHttp", "DenoRedis", "Amqplib", + "Express", "GenericPool", "Koa", "LruMemoizer", @@ -202,6 +203,7 @@ snapshot[`captureMessage 1`] = ` "DenoHttp", "DenoRedis", "Amqplib", + "Express", "GenericPool", "Koa", "LruMemoizer", @@ -295,6 +297,7 @@ snapshot[`captureMessage twice 1`] = ` "DenoHttp", "DenoRedis", "Amqplib", + "Express", "GenericPool", "Koa", "LruMemoizer", @@ -395,6 +398,7 @@ snapshot[`captureMessage twice 2`] = ` "DenoHttp", "DenoRedis", "Amqplib", + "Express", "GenericPool", "Koa", "LruMemoizer",