|
| 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 | +}); |
0 commit comments