Skip to content

Commit 3c68cda

Browse files
committed
feat(deno): add postgresjs integration
Also, remove a needlessly repetitive copypasta comment from the Deno integration tests. We know that they're integration tests, it helps no one to have a comment stating that it tests the integration.
1 parent a860837 commit 3c68cda

8 files changed

Lines changed: 116 additions & 30 deletions

File tree

dev-packages/deno-integration-tests/suites/orchestrion-amqplib/test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ Deno.test('amqplib instrumentation: included in default integrations (Deno 2.8.0
6161
assert(names.includes('Amqplib'), `Amqplib should be in defaults, got ${names.join(', ')}`);
6262
});
6363

64-
// Exercises the SDK path end-to-end: `init()` installs the AsyncLocalStorage
65-
// context strategy and wires the default `amqplibChannelIntegration` (which
66-
// subscribes to the channel), and we drive the `orchestrion:amqplib:publish`
67-
// channel manually — the same events the orchestrion transform publishes around
68-
// `Channel.prototype.publish` — so no live broker is needed. Asserting a nested
69-
// producer `message` span proves the subscriber, the emitted attributes, AND the
70-
// context-strategy wiring all work.
7164
Deno.test('amqplib instrumentation: orchestrion:amqplib:publish channel produces a nested message span', async () => {
7265
resetGlobals();
7366
const sink = transactionSink();

dev-packages/deno-integration-tests/suites/orchestrion-koa/test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ Deno.test('koa instrumentation: included in default integrations (Deno 2.8.0+)',
6161
assert(names.includes('Koa'), `Koa should be in defaults, got ${names.join(', ')}`);
6262
});
6363

64-
// Exercises the SDK path end-to-end. Unlike the db integrations, koa's channel
65-
// doesn't build a span directly: its `start` handler wraps the registered
66-
// middleware (arg 0) in a span-creating proxy, and the span opens when that
67-
// middleware later runs under an active span. So we publish `orchestrion:koa:use`
68-
// with a middleware, then invoke the wrapped middleware inside a parent span —
69-
// the same shape `app.use(fn)` then a request produces. Asserting a nested
70-
// `middleware.koa` span proves the subscriber and context wiring work.
7164
Deno.test('koa instrumentation: orchestrion:koa:use channel wraps middleware into a span', async () => {
7265
resetGlobals();
7366
const sink = transactionSink();

dev-packages/deno-integration-tests/suites/orchestrion-mongo/test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ Deno.test('mongodb instrumentation: included in default integrations (Deno 2.8.0
6161
assert(names.includes('Mongo'), `Mongo should be in defaults, got ${names.join(', ')}`);
6262
});
6363

64-
// Exercises the SDK path end-to-end: `init()` installs the AsyncLocalStorage
65-
// context strategy and wires the default `mongodbChannelIntegration` (which
66-
// subscribes to the channel), and we drive the `orchestrion:mongodb:command`
67-
// channel manually — the same events the orchestrion transform publishes around
68-
// `Connection.prototype.command` — so no live database is needed. Asserting a
69-
// nested `db` span proves the subscriber, the emitted attributes, AND the
70-
// context-strategy wiring all work.
7164
Deno.test('mongodb instrumentation: orchestrion:mongodb:command channel produces a nested db span', async () => {
7265
resetGlobals();
7366
const sink = transactionSink();

dev-packages/deno-integration-tests/suites/orchestrion-mongoose/test.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,6 @@ Deno.test('mongoose instrumentation: included in default integrations (Deno 2.8.
6161
assert(names.includes('Mongoose'), `Mongoose should be in defaults, got ${names.join(', ')}`);
6262
});
6363

64-
// Exercises the SDK path end-to-end: `init()` installs the AsyncLocalStorage
65-
// context strategy and wires the default `mongooseChannelIntegration` (which
66-
// subscribes to the channel), and we drive the `orchestrion:mongoose:model_save`
67-
// channel manually — the same events the orchestrion transform publishes around
68-
// `Model.prototype.save` — so no live database is needed. Asserting a nested
69-
// `db` span proves the subscriber, the emitted attributes, AND the
70-
// context-strategy wiring all work.
7164
Deno.test('mongoose instrumentation: orchestrion:mongoose:model_save channel produces a nested db span', async () => {
7265
resetGlobals();
7366
const sink = transactionSink();
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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 { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts';
9+
import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.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('postgres.js 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('PostgresJs'), `PostgresJs should be in defaults, got ${names.join(', ')}`);
62+
});
63+
64+
Deno.test('postgres.js instrumentation: orchestrion:postgres:handle channel produces a nested db 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+
const channel = tracingChannel('orchestrion:postgres:handle');
74+
75+
// `self` is the postgres.js `Query`; `strings` is its tagged-template SQL parts.
76+
// The span ends when postgres.js calls `query.resolve`, which the subscriber wraps.
77+
const query = {
78+
strings: ['SELECT name FROM users'],
79+
executed: false,
80+
resolve: (..._args: unknown[]) => undefined,
81+
reject: (..._args: unknown[]) => undefined,
82+
};
83+
const ctx = { self: query };
84+
85+
startSpan({ name: 'parent', op: 'test' }, () => {
86+
// `start` creates the span and wraps `query.resolve`/`query.reject`.
87+
channel.start.runStores(ctx, () => undefined);
88+
// postgres.js signals completion by calling `resolve`; the wrapper ends the span.
89+
query.resolve({ command: 'SELECT' });
90+
});
91+
92+
const parent = await withTimeout(
93+
sink.waitFor(t => t.transaction === 'parent'),
94+
5000,
95+
"'parent' transaction",
96+
);
97+
98+
const pgSpan = parent.spans?.find(s => s.op === 'db');
99+
assertExists(pgSpan, `expected a db child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`);
100+
assertEquals(pgSpan!.description, 'SELECT name FROM users');
101+
assertEquals(pgSpan!.data?.['db.system.name'], 'postgres');
102+
assertEquals(pgSpan!.data?.['db.query.text'], 'SELECT name FROM users');
103+
// Set by the resolve wrapper from the `command` passed to `query.resolve`.
104+
assertEquals(pgSpan!.data?.['db.operation.name'], 'SELECT');
105+
assertEquals(pgSpan!.data?.['sentry.origin'], 'auto.db.orchestrion.postgresjs');
106+
});

packages/deno/src/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,9 @@ export type { DenoHttpIntegrationOptions } from './integrations/http';
112112
export { denoRedisIntegration } from './integrations/redis';
113113
export type { DenoRedisIntegrationOptions } from './integrations/redis';
114114
// The orchestrion channel integrations, re-exported from `@sentry/server-utils`.
115-
// The first six are in the default set; `dataloader` and `knex` are opt-in (add
116-
// them to `integrations` to enable), matching Node.
115+
// Most are in the default set; `dataloader` and `knex` are opt-in (add them to
116+
// `integrations` to enable), matching Node. Re-export every one that `sdk.ts`
117+
// adds to the defaults, so users who customize `defaultIntegrations` can re-add it.
117118
export {
118119
amqplibChannelIntegration,
119120
dataloaderChannelIntegration,
@@ -123,6 +124,7 @@ export {
123124
mongooseChannelIntegration,
124125
mysqlChannelIntegration,
125126
postgresChannelIntegration,
127+
postgresJsChannelIntegration,
126128
} from '@sentry/server-utils/orchestrion';
127129
// Deprecated aliases kept for back-compat. Each forwards to the shared
128130
// 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
@@ -18,6 +18,7 @@ import {
1818
mongooseChannelIntegration,
1919
mysqlChannelIntegration,
2020
postgresChannelIntegration,
21+
postgresJsChannelIntegration,
2122
} from '@sentry/server-utils/orchestrion';
2223
import { DenoClient } from './client';
2324
import { breadcrumbsIntegration } from './integrations/breadcrumbs';
@@ -77,6 +78,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
7778
mongooseChannelIntegration(),
7879
mysqlChannelIntegration(),
7980
postgresChannelIntegration(),
81+
postgresJsChannelIntegration(),
8082
]
8183
: []),
8284
contextLinesIntegration(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ snapshot[`captureException 1`] = `
121121
"Mongoose",
122122
"Mysql",
123123
"Postgres",
124+
"PostgresJs",
124125
"ContextLines",
125126
"NormalizePaths",
126127
"GlobalHandlers",
@@ -202,6 +203,7 @@ snapshot[`captureMessage 1`] = `
202203
"Mongoose",
203204
"Mysql",
204205
"Postgres",
206+
"PostgresJs",
205207
"ContextLines",
206208
"NormalizePaths",
207209
"GlobalHandlers",
@@ -290,6 +292,7 @@ snapshot[`captureMessage twice 1`] = `
290292
"Mongoose",
291293
"Mysql",
292294
"Postgres",
295+
"PostgresJs",
293296
"ContextLines",
294297
"NormalizePaths",
295298
"GlobalHandlers",
@@ -385,6 +388,7 @@ snapshot[`captureMessage twice 2`] = `
385388
"Mongoose",
386389
"Mysql",
387390
"Postgres",
391+
"PostgresJs",
388392
"ContextLines",
389393
"NormalizePaths",
390394
"GlobalHandlers",

0 commit comments

Comments
 (0)