|
| 1 | +// <reference lib="deno.ns" /> |
| 2 | + |
| 3 | +import * as http from 'node:http'; |
| 4 | +import type { TransactionEvent } from '@sentry/core'; |
| 5 | +import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts'; |
| 6 | +import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts'; |
| 7 | +import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts'; |
| 8 | +import type { DenoClient } from '../build/esm/index.js'; |
| 9 | +import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '../build/esm/index.js'; |
| 10 | +import { |
| 11 | + DENO_VERSION, |
| 12 | + HTTP_CLIENT_DIAGNOSTICS_CHANNEL_SUPPORTED, |
| 13 | + HTTP_SERVER_DIAGNOSTICS_CHANNEL_SUPPORTED, |
| 14 | +} from '../build/esm/denoVersion.js'; |
| 15 | + |
| 16 | +function resetGlobals(): void { |
| 17 | + getCurrentScope().clear(); |
| 18 | + getCurrentScope().setClient(undefined); |
| 19 | + getIsolationScope().clear(); |
| 20 | + getGlobalScope().clear(); |
| 21 | +} |
| 22 | + |
| 23 | +function delay(ms: number): Promise<void> { |
| 24 | + return new Promise(resolve => setTimeout(resolve, ms)); |
| 25 | +} |
| 26 | + |
| 27 | +// Activation test — runs unconditionally so the default-inclusion gate itself |
| 28 | +// is verified on every Deno version. The skip conditions on the behavioral |
| 29 | +// tests below cover the runtime-feature absence; this one covers the gate. |
| 30 | +Deno.test('denoHttpIntegration: included in default integrations when client diagnostics channel is supported', () => { |
| 31 | + resetGlobals(); |
| 32 | + const client = init({ dsn: 'https://username@domain/123' }) as DenoClient; |
| 33 | + const names = client.getOptions().integrations.map(i => i.name); |
| 34 | + |
| 35 | + if (HTTP_CLIENT_DIAGNOSTICS_CHANNEL_SUPPORTED) { |
| 36 | + assert( |
| 37 | + names.includes('DenoHttp'), |
| 38 | + `DenoHttp should be a default integration on Deno ${DENO_VERSION.major}.${DENO_VERSION.minor}.${DENO_VERSION.patch}, got ${names.join(', ')}`, |
| 39 | + ); |
| 40 | + } else { |
| 41 | + assert(!names.includes('DenoHttp'), `DenoHttp should NOT be in defaults on Deno < 2.7.13, got ${names.join(', ')}`); |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +Deno.test({ |
| 46 | + name: 'denoHttpIntegration: node:http incoming request creates an http.server transaction', |
| 47 | + ignore: !HTTP_SERVER_DIAGNOSTICS_CHANNEL_SUPPORTED, |
| 48 | + async fn() { |
| 49 | + resetGlobals(); |
| 50 | + const transactions: TransactionEvent[] = []; |
| 51 | + init({ |
| 52 | + dsn: 'https://username@domain/123', |
| 53 | + tracesSampleRate: 1, |
| 54 | + beforeSendTransaction: (event: TransactionEvent) => { |
| 55 | + transactions.push(event); |
| 56 | + return null; |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + const server = http.createServer((_req, res) => { |
| 61 | + res.end('ok'); |
| 62 | + }); |
| 63 | + const port: number = await new Promise(resolve => { |
| 64 | + server.listen(0, '127.0.0.1', () => { |
| 65 | + resolve((server.address() as { port: number }).port); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + const response = await fetch(`http://127.0.0.1:${port}/users/42?x=1`); |
| 70 | + assertEquals(await response.text(), 'ok'); |
| 71 | + |
| 72 | + // Server-side `response.once('close')` fires shortly after the response |
| 73 | + // is sent. Give it a tick so the span ends before we tear down. |
| 74 | + await delay(50); |
| 75 | + await new Promise<void>(resolve => server.close(() => resolve())); |
| 76 | + |
| 77 | + assertEquals(transactions.length, 1, `expected 1 transaction, got ${transactions.length}`); |
| 78 | + const txn = transactions[0]!; |
| 79 | + assertEquals(txn.contexts?.trace?.op, 'http.server'); |
| 80 | + assertEquals(txn.transaction, 'GET /users/42'); |
| 81 | + assertEquals(txn.contexts?.trace?.data?.['http.method'], 'GET'); |
| 82 | + assertEquals(txn.contexts?.trace?.data?.['http.response.status_code'], 200); |
| 83 | + }, |
| 84 | +}); |
| 85 | + |
| 86 | +Deno.test({ |
| 87 | + name: 'denoHttpIntegration: node:http outgoing request creates a child http.client span', |
| 88 | + ignore: !HTTP_CLIENT_DIAGNOSTICS_CHANNEL_SUPPORTED, |
| 89 | + async fn() { |
| 90 | + resetGlobals(); |
| 91 | + const transactions: TransactionEvent[] = []; |
| 92 | + init({ |
| 93 | + dsn: 'https://username@domain/123', |
| 94 | + tracesSampleRate: 1, |
| 95 | + beforeSendTransaction: (event: TransactionEvent) => { |
| 96 | + transactions.push(event); |
| 97 | + return null; |
| 98 | + }, |
| 99 | + }); |
| 100 | + |
| 101 | + // Use Deno.serve for the target so the test does not depend on the |
| 102 | + // node:http server side (which only works on Deno 2.8.0+). |
| 103 | + const abortController = new AbortController(); |
| 104 | + let onListen: ((_: unknown) => void) | undefined; |
| 105 | + const listening = new Promise(resolve => (onListen = resolve)); |
| 106 | + const target = Deno.serve( |
| 107 | + { port: 0, signal: abortController.signal, onListen, hostname: '127.0.0.1' }, |
| 108 | + () => new Response('pong'), |
| 109 | + ); |
| 110 | + await listening; |
| 111 | + const targetPort = target.addr.port; |
| 112 | + |
| 113 | + // Make the outgoing node:http request inside an explicit parent span so |
| 114 | + // the http.client child span has somewhere to attach and the resulting |
| 115 | + // transaction is captured. |
| 116 | + await startSpan({ name: 'parent', op: 'test' }, async () => { |
| 117 | + await new Promise<void>((resolve, reject) => { |
| 118 | + const req = http.request({ host: '127.0.0.1', port: targetPort, path: '/ping', method: 'GET' }, res => { |
| 119 | + res.on('data', () => {}); |
| 120 | + res.on('end', () => resolve()); |
| 121 | + res.on('error', reject); |
| 122 | + }); |
| 123 | + req.on('error', reject); |
| 124 | + req.end(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + abortController.abort(); |
| 129 | + await target.finished; |
| 130 | + |
| 131 | + // Two transactions are possible: one for the parent span we created, and |
| 132 | + // one http.server transaction from Deno.serve. Find the parent. |
| 133 | + const parent = transactions.find(t => t.transaction === 'parent'); |
| 134 | + assertExists(parent, `expected a 'parent' transaction, got: ${transactions.map(t => t.transaction).join(', ')}`); |
| 135 | + const httpClientSpan = parent!.spans?.find(s => s.op === 'http.client'); |
| 136 | + assertExists( |
| 137 | + httpClientSpan, |
| 138 | + `expected an http.client child span, got ops: ${parent!.spans?.map(s => s.op).join(', ')}`, |
| 139 | + ); |
| 140 | + assertEquals(httpClientSpan!.data?.['http.method'], 'GET'); |
| 141 | + assertEquals(httpClientSpan!.data?.['http.response.status_code'], 200); |
| 142 | + }, |
| 143 | +}); |
0 commit comments