Skip to content

Commit 519f13a

Browse files
committed
feat(deno): add hapi integration
1 parent 4c157ad commit 519f13a

4 files changed

Lines changed: 103 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// <reference lib="deno.ns" />
2+
3+
import { tracingChannel } from 'node:diagnostics_channel';
4+
import type { TransactionEvent } from '@sentry/core';
5+
import type { DenoClient } from '@sentry/deno';
6+
import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '@sentry/deno';
7+
import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts';
8+
import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts';
9+
import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts';
10+
11+
function resetGlobals(): void {
12+
getCurrentScope().clear();
13+
getCurrentScope().setClient(undefined);
14+
getIsolationScope().clear();
15+
getGlobalScope().clear();
16+
}
17+
18+
/** See deno-redis.test.ts — same sink shape, deduped for clarity. */
19+
function transactionSink(): {
20+
beforeSendTransaction: (event: TransactionEvent) => null;
21+
waitFor: (predicate: (event: TransactionEvent) => boolean) => Promise<TransactionEvent>;
22+
} {
23+
const transactions: TransactionEvent[] = [];
24+
const waiters: { predicate: (e: TransactionEvent) => boolean; resolve: (e: TransactionEvent) => void }[] = [];
25+
return {
26+
beforeSendTransaction(event) {
27+
transactions.push(event);
28+
for (let i = waiters.length - 1; i >= 0; i--) {
29+
const w = waiters[i]!;
30+
if (w.predicate(event)) {
31+
waiters.splice(i, 1);
32+
w.resolve(event);
33+
}
34+
}
35+
return null;
36+
},
37+
waitFor(predicate) {
38+
const already = transactions.find(predicate);
39+
if (already) return Promise.resolve(already);
40+
return new Promise<TransactionEvent>(resolve => {
41+
waiters.push({ predicate, resolve });
42+
});
43+
},
44+
};
45+
}
46+
47+
function withTimeout<T>(p: Promise<T>, ms: number, what: string): Promise<T> {
48+
let timer: ReturnType<typeof setTimeout> | undefined;
49+
const timeout = new Promise<T>((_, reject) => {
50+
timer = setTimeout(() => reject(new Error(`Timed out waiting for ${what} after ${ms}ms`)), ms);
51+
});
52+
return Promise.race([p, timeout]).finally(() => {
53+
if (timer !== undefined) clearTimeout(timer);
54+
});
55+
}
56+
57+
Deno.test('hapi instrumentation: included in default integrations (Deno 2.8.0+)', () => {
58+
resetGlobals();
59+
const client = init({ dsn: 'https://username@domain/123' }) as DenoClient;
60+
const names = client.getOptions().integrations.map(i => i.name);
61+
assert(names.includes('Hapi'), `Hapi should be in defaults, got ${names.join(', ')}`);
62+
});
63+
64+
Deno.test('hapi instrumentation: orchestrion:@hapi/hapi:route channel wraps the route handler into a span', async () => {
65+
resetGlobals();
66+
const sink = transactionSink();
67+
init({
68+
dsn: 'https://username@domain/123',
69+
tracesSampleRate: 1,
70+
beforeSendTransaction: sink.beforeSendTransaction,
71+
});
72+
73+
// `start` wraps the route's `handler` in place; the span opens when that
74+
// handler runs under an active span (as it does per request).
75+
const route = { method: 'get', path: '/hello', handler: (_req: unknown, _h: unknown) => 'ok' };
76+
const ctx = { arguments: [route] as unknown[], self: {} };
77+
tracingChannel('orchestrion:@hapi/hapi:route').start.publish(ctx);
78+
const wrappedRoute = ctx.arguments[0] as typeof route;
79+
80+
startSpan({ name: 'parent', op: 'test' }, () => {
81+
wrappedRoute.handler({}, {});
82+
});
83+
84+
const parent = await withTimeout(
85+
sink.waitFor(t => t.transaction === 'parent'),
86+
5000,
87+
"'parent' transaction",
88+
);
89+
90+
const hapiSpan = parent.spans?.find(s => s.op === 'router.hapi');
91+
assertExists(hapiSpan, `expected a router.hapi span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`);
92+
assertEquals(hapiSpan!.description, 'route - /hello');
93+
assertEquals(hapiSpan!.data?.['hapi.type'], 'router');
94+
assertEquals(hapiSpan!.data?.['http.route'], '/hello');
95+
assertEquals(hapiSpan!.data?.['sentry.origin'], 'auto.http.orchestrion.hapi');
96+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export {
120120
dataloaderChannelIntegration,
121121
expressChannelIntegration,
122122
genericPoolChannelIntegration,
123+
hapiChannelIntegration,
123124
knexChannelIntegration,
124125
koaChannelIntegration,
125126
lruMemoizerChannelIntegration,

packages/deno/src/sdk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
amqplibChannelIntegration,
1616
expressChannelIntegration,
1717
genericPoolChannelIntegration,
18+
hapiChannelIntegration,
1819
koaChannelIntegration,
1920
lruMemoizerChannelIntegration,
2021
mongodbChannelIntegration,
@@ -80,6 +81,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
8081
amqplibChannelIntegration(),
8182
expressChannelIntegration(),
8283
genericPoolChannelIntegration(),
84+
hapiChannelIntegration(),
8385
koaChannelIntegration(),
8486
lruMemoizerChannelIntegration(),
8587
mongodbChannelIntegration(),

packages/deno/test/__snapshots__/mod.test.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ snapshot[`captureException 1`] = `
118118
"Amqplib",
119119
"Express",
120120
"GenericPool",
121+
"Hapi",
121122
"Koa",
122123
"LruMemoizer",
123124
"Mongo",
@@ -205,6 +206,7 @@ snapshot[`captureMessage 1`] = `
205206
"Amqplib",
206207
"Express",
207208
"GenericPool",
209+
"Hapi",
208210
"Koa",
209211
"LruMemoizer",
210212
"Mongo",
@@ -299,6 +301,7 @@ snapshot[`captureMessage twice 1`] = `
299301
"Amqplib",
300302
"Express",
301303
"GenericPool",
304+
"Hapi",
302305
"Koa",
303306
"LruMemoizer",
304307
"Mongo",
@@ -400,6 +403,7 @@ snapshot[`captureMessage twice 2`] = `
400403
"Amqplib",
401404
"Express",
402405
"GenericPool",
406+
"Hapi",
403407
"Koa",
404408
"LruMemoizer",
405409
"Mongo",

0 commit comments

Comments
 (0)