|
| 1 | +// <reference lib="deno.ns" /> |
| 2 | + |
| 3 | +/** |
| 4 | + * Lives in its own file because `setupOnce` runs once per process |
| 5 | + * (`installedIntegrations` guards it) and the diagnostics channel |
| 6 | + * subscription is global. Deno gives each test file a fresh module graph, |
| 7 | + * so this is the only way to install `denoHttpIntegration` with |
| 8 | + * `spans: false` after another file has installed it with the defaults. |
| 9 | + * |
| 10 | + * Both tests below share that single `spans: false` subscription. |
| 11 | + */ |
| 12 | + |
| 13 | +import * as http from 'node:http'; |
| 14 | +import type { TransactionEvent } from '@sentry/core'; |
| 15 | +import { getIsolationScope, getMainCarrier } from '@sentry/core'; |
| 16 | +import { assert } from 'https://deno.land/[email protected]/assert/assert.ts'; |
| 17 | +import { assertEquals } from 'https://deno.land/[email protected]/assert/assert_equals.ts'; |
| 18 | +import type { DenoClient } from '../build/esm/index.js'; |
| 19 | +import { denoHttpIntegration, init, startSpan } from '../build/esm/index.js'; |
| 20 | + |
| 21 | +/** |
| 22 | + * `spans: false` must win over `tracesSampleRate: 1`, so tracing is on |
| 23 | + * everywhere except the HTTP integration. Without it the option would be |
| 24 | + * indistinguishable from tracing being off. |
| 25 | + */ |
| 26 | +function initWithSpansDisabled(transactions: TransactionEvent[]): DenoClient { |
| 27 | + getMainCarrier().__SENTRY__ = undefined; |
| 28 | + return init({ |
| 29 | + dsn: 'https://username@domain/123', |
| 30 | + tracesSampleRate: 1, |
| 31 | + traceLifecycle: 'static', |
| 32 | + integrations: [denoHttpIntegration({ spans: false })], |
| 33 | + beforeSendTransaction: (event: TransactionEvent) => { |
| 34 | + transactions.push(event); |
| 35 | + return null; |
| 36 | + }, |
| 37 | + }) as DenoClient; |
| 38 | +} |
| 39 | + |
| 40 | +Deno.test({ |
| 41 | + name: 'denoHttpIntegration: node:http outgoing request creates no http.client span when spans: false', |
| 42 | + async fn() { |
| 43 | + const transactions: TransactionEvent[] = []; |
| 44 | + const client = initWithSpansDisabled(transactions); |
| 45 | + |
| 46 | + // Deno.serve for the target so this does not depend on the node:http |
| 47 | + // server instrumentation. |
| 48 | + const abortController = new AbortController(); |
| 49 | + let onListen: ((_: unknown) => void) | undefined; |
| 50 | + const listening = new Promise(resolve => (onListen = resolve)); |
| 51 | + // Captured so we can tell "spans were disabled" apart from "the client |
| 52 | + // was never instrumented at all" -- header injection survives spans: false. |
| 53 | + let sentryTraceHeader: string | null = null; |
| 54 | + const target = Deno.serve( |
| 55 | + { port: 0, signal: abortController.signal, onListen, hostname: '127.0.0.1' }, |
| 56 | + (request: Request) => { |
| 57 | + sentryTraceHeader = request.headers.get('sentry-trace'); |
| 58 | + return new Response('pong'); |
| 59 | + }, |
| 60 | + ); |
| 61 | + await listening; |
| 62 | + |
| 63 | + await startSpan({ name: 'parent', op: 'test' }, async () => { |
| 64 | + await new Promise<void>((resolve, reject) => { |
| 65 | + const req = http.request({ host: '127.0.0.1', port: target.addr.port, path: '/ping', method: 'GET' }, res => { |
| 66 | + res.on('data', () => {}); |
| 67 | + res.on('end', () => resolve()); |
| 68 | + res.on('error', reject); |
| 69 | + }); |
| 70 | + req.on('error', reject); |
| 71 | + req.end(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + abortController.abort(); |
| 76 | + await target.finished; |
| 77 | + |
| 78 | + // Event capture runs through the client's async processing queue, so |
| 79 | + // drain it before reading the sink -- otherwise these assertions race. |
| 80 | + await client.flush(5_000); |
| 81 | + |
| 82 | + // The parent span proves tracing itself is live, so an absent |
| 83 | + // http.client span is the option working rather than tracing being off. |
| 84 | + assert(sentryTraceHeader, 'expected an injected sentry-trace header, so the client was instrumented'); |
| 85 | + const parent = transactions.find(t => t.transaction === 'parent'); |
| 86 | + assert(parent, `expected the 'parent' transaction, got: ${transactions.map(t => t.transaction).join(', ')}`); |
| 87 | + const childOps = parent!.spans?.map(s => s.op) ?? []; |
| 88 | + assertEquals( |
| 89 | + childOps.includes('http.client'), |
| 90 | + false, |
| 91 | + `expected no http.client span, got ops: ${childOps.join(', ')}`, |
| 92 | + ); |
| 93 | + }, |
| 94 | +}); |
| 95 | + |
| 96 | +Deno.test({ |
| 97 | + name: 'denoHttpIntegration: node:http incoming request creates no http.server transaction when spans: false', |
| 98 | + async fn() { |
| 99 | + const transactions: TransactionEvent[] = []; |
| 100 | + const client = initWithSpansDisabled(transactions); |
| 101 | + |
| 102 | + // Captured inside the handler so we can tell "spans were disabled" apart |
| 103 | + // from "the request was never instrumented at all". |
| 104 | + let isolatedTransactionName: string | undefined; |
| 105 | + const server = http.createServer((_req, res) => { |
| 106 | + isolatedTransactionName = getIsolationScope().getScopeData().transactionName; |
| 107 | + res.end('ok'); |
| 108 | + }); |
| 109 | + const port: number = await new Promise(resolve => { |
| 110 | + server.listen(0, '127.0.0.1', () => { |
| 111 | + resolve((server.address() as { port: number }).port); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + const response = await fetch(`http://127.0.0.1:${port}/users/42`); |
| 116 | + assertEquals(await response.text(), 'ok'); |
| 117 | + await new Promise<void>(resolve => server.close(() => resolve())); |
| 118 | + |
| 119 | + // Drain the async processing queue first. Without this, "no http.server |
| 120 | + // transaction yet" and "no http.server transaction at all" look alike, |
| 121 | + // so the assertion below could pass while spans were still enabled. |
| 122 | + await client.flush(5_000); |
| 123 | + |
| 124 | + // Request isolation still runs with spans off, so this proves the |
| 125 | + // instrumentation saw the request. |
| 126 | + assertEquals(isolatedTransactionName, 'GET /users/42'); |
| 127 | + const ops = transactions.map(t => t.contexts?.trace?.op); |
| 128 | + assertEquals(ops.includes('http.server'), false, `expected no http.server transaction, got ops: ${ops.join(', ')}`); |
| 129 | + }, |
| 130 | +}); |
0 commit comments