From 8767f9b48b4ec2470ed55522243d54b88cf11476 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 21 Jul 2026 12:40:17 -0700 Subject: [PATCH] feat(deno): add hapi integration --- .../suites/orchestrion-hapi/test.ts | 96 +++++++++++++++++++ packages/deno/src/index.ts | 1 + packages/deno/src/sdk.ts | 2 + .../deno/test/__snapshots__/mod.test.ts.snap | 4 + 4 files changed, 103 insertions(+) create mode 100644 dev-packages/deno-integration-tests/suites/orchestrion-hapi/test.ts diff --git a/dev-packages/deno-integration-tests/suites/orchestrion-hapi/test.ts b/dev-packages/deno-integration-tests/suites/orchestrion-hapi/test.ts new file mode 100644 index 000000000000..58c9a8d59815 --- /dev/null +++ b/dev-packages/deno-integration-tests/suites/orchestrion-hapi/test.ts @@ -0,0 +1,96 @@ +// + +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('hapi 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('Hapi'), `Hapi should be in defaults, got ${names.join(', ')}`); +}); + +Deno.test('hapi instrumentation: orchestrion:@hapi/hapi:route channel wraps the route handler into a span', async () => { + resetGlobals(); + const sink = transactionSink(); + init({ + dsn: 'https://username@domain/123', + tracesSampleRate: 1, + beforeSendTransaction: sink.beforeSendTransaction, + }); + + // `start` wraps the route's `handler` in place; the span opens when that + // handler runs under an active span (as it does per request). + const route = { method: 'get', path: '/hello', handler: (_req: unknown, _h: unknown) => 'ok' }; + const ctx = { arguments: [route] as unknown[], self: {} }; + tracingChannel('orchestrion:@hapi/hapi:route').start.publish(ctx); + const wrappedRoute = ctx.arguments[0] as typeof route; + + startSpan({ name: 'parent', op: 'test' }, () => { + wrappedRoute.handler({}, {}); + }); + + const parent = await withTimeout( + sink.waitFor(t => t.transaction === 'parent'), + 5000, + "'parent' transaction", + ); + + const hapiSpan = parent.spans?.find(s => s.op === 'router.hapi'); + assertExists(hapiSpan, `expected a router.hapi span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`); + assertEquals(hapiSpan!.description, 'route - /hello'); + assertEquals(hapiSpan!.data?.['hapi.type'], 'router'); + assertEquals(hapiSpan!.data?.['http.route'], '/hello'); + assertEquals(hapiSpan!.data?.['sentry.origin'], 'auto.http.orchestrion.hapi'); +}); diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index 7bfba75dbb2e..dd73b7bd97d4 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -120,6 +120,7 @@ export { dataloaderChannelIntegration, expressChannelIntegration, genericPoolChannelIntegration, + hapiChannelIntegration, knexChannelIntegration, koaChannelIntegration, lruMemoizerChannelIntegration, diff --git a/packages/deno/src/sdk.ts b/packages/deno/src/sdk.ts index 789dac54d8f4..97f990dead6c 100644 --- a/packages/deno/src/sdk.ts +++ b/packages/deno/src/sdk.ts @@ -15,6 +15,7 @@ import { amqplibChannelIntegration, expressChannelIntegration, genericPoolChannelIntegration, + hapiChannelIntegration, koaChannelIntegration, lruMemoizerChannelIntegration, mongodbChannelIntegration, @@ -80,6 +81,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] { amqplibChannelIntegration(), expressChannelIntegration(), genericPoolChannelIntegration(), + hapiChannelIntegration(), koaChannelIntegration(), lruMemoizerChannelIntegration(), mongodbChannelIntegration(), diff --git a/packages/deno/test/__snapshots__/mod.test.ts.snap b/packages/deno/test/__snapshots__/mod.test.ts.snap index cf5455ad9e02..a64cec594012 100644 --- a/packages/deno/test/__snapshots__/mod.test.ts.snap +++ b/packages/deno/test/__snapshots__/mod.test.ts.snap @@ -118,6 +118,7 @@ snapshot[`captureException 1`] = ` "Amqplib", "Express", "GenericPool", + "Hapi", "Koa", "LruMemoizer", "Mongo", @@ -205,6 +206,7 @@ snapshot[`captureMessage 1`] = ` "Amqplib", "Express", "GenericPool", + "Hapi", "Koa", "LruMemoizer", "Mongo", @@ -299,6 +301,7 @@ snapshot[`captureMessage twice 1`] = ` "Amqplib", "Express", "GenericPool", + "Hapi", "Koa", "LruMemoizer", "Mongo", @@ -400,6 +403,7 @@ snapshot[`captureMessage twice 2`] = ` "Amqplib", "Express", "GenericPool", + "Hapi", "Koa", "LruMemoizer", "Mongo",