Skip to content

Commit cb45771

Browse files
committed
feat(deno): add tedious integration
1 parent 51adfcc commit cb45771

4 files changed

Lines changed: 116 additions & 0 deletions

File tree

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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('tedious 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('Tedious'), `Tedious should be in defaults, got ${names.join(', ')}`);
63+
});
64+
65+
Deno.test('tedious instrumentation: orchestrion:tedious:execSql channel produces a nested db 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+
// The connection and request are `EventEmitter`s; the subscriber only traces
75+
// when `arguments[0]` is one, and ends the span from the request's callback.
76+
const connection = Object.assign(new EventEmitter(), {
77+
config: { server: '127.0.0.1', userName: 'sa', options: { database: 'mydb', port: 1433 } },
78+
});
79+
const request = Object.assign(new EventEmitter(), {
80+
sqlTextOrProcedure: 'SELECT 1',
81+
callback: (..._args: unknown[]) => undefined,
82+
});
83+
84+
// `connect` seeds the connection's current database, read into `db.name`.
85+
tracingChannel('orchestrion:tedious:connect').start.publish({ self: connection, arguments: [] });
86+
87+
startSpan({ name: 'parent', op: 'test' }, () => {
88+
tracingChannel('orchestrion:tedious:execSql').start.publish({ self: connection, arguments: [request] });
89+
// tedious signals completion via the request callback; the wrapper ends the span.
90+
request.callback();
91+
});
92+
93+
const parent = await withTimeout(
94+
sink.waitFor(t => t.transaction === 'parent'),
95+
5000,
96+
"'parent' transaction",
97+
);
98+
99+
const tediousSpan = parent.spans?.find(s => s.op === 'db');
100+
assertExists(tediousSpan, `expected a db child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`);
101+
assertEquals(tediousSpan!.description, 'execSql mydb');
102+
assertEquals(tediousSpan!.data?.['db.system'], 'mssql');
103+
assertEquals(tediousSpan!.data?.['db.name'], 'mydb');
104+
assertEquals(tediousSpan!.data?.['db.user'], 'sa');
105+
assertEquals(tediousSpan!.data?.['db.statement'], 'SELECT 1');
106+
assertEquals(tediousSpan!.data?.['net.peer.name'], '127.0.0.1');
107+
assertEquals(tediousSpan!.data?.['net.peer.port'], 1433);
108+
assertEquals(tediousSpan!.data?.['sentry.origin'], 'auto.db.orchestrion.tedious');
109+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export {
126126
mysql2ChannelIntegration,
127127
postgresChannelIntegration,
128128
postgresJsChannelIntegration,
129+
tediousChannelIntegration,
129130
} from '@sentry/server-utils/orchestrion';
130131
// Deprecated aliases kept for back-compat. Each forwards to the shared
131132
// integration above, so its name is the shared name (e.g. `Mysql`), not the old

packages/deno/src/sdk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
mysql2ChannelIntegration,
2121
postgresChannelIntegration,
2222
postgresJsChannelIntegration,
23+
tediousChannelIntegration,
2324
} from '@sentry/server-utils/orchestrion';
2425
import { DenoClient } from './client';
2526
import { breadcrumbsIntegration } from './integrations/breadcrumbs';
@@ -81,6 +82,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
8182
mysql2ChannelIntegration(),
8283
postgresChannelIntegration(),
8384
postgresJsChannelIntegration(),
85+
tediousChannelIntegration(),
8486
]
8587
: []),
8688
contextLinesIntegration(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ snapshot[`captureException 1`] = `
123123
"Mysql2",
124124
"Postgres",
125125
"PostgresJs",
126+
"Tedious",
126127
"ContextLines",
127128
"NormalizePaths",
128129
"GlobalHandlers",
@@ -206,6 +207,7 @@ snapshot[`captureMessage 1`] = `
206207
"Mysql2",
207208
"Postgres",
208209
"PostgresJs",
210+
"Tedious",
209211
"ContextLines",
210212
"NormalizePaths",
211213
"GlobalHandlers",
@@ -296,6 +298,7 @@ snapshot[`captureMessage twice 1`] = `
296298
"Mysql2",
297299
"Postgres",
298300
"PostgresJs",
301+
"Tedious",
299302
"ContextLines",
300303
"NormalizePaths",
301304
"GlobalHandlers",
@@ -393,6 +396,7 @@ snapshot[`captureMessage twice 2`] = `
393396
"Mysql2",
394397
"Postgres",
395398
"PostgresJs",
399+
"Tedious",
396400
"ContextLines",
397401
"NormalizePaths",
398402
"GlobalHandlers",

0 commit comments

Comments
 (0)