Skip to content

Commit 06a6843

Browse files
committed
test(deno): integration test for mongoose
1 parent 6bf219d commit 06a6843

1 file changed

Lines changed: 119 additions & 0 deletions

File tree

  • dev-packages/deno-integration-tests/suites/orchestrion-mongoose
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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('mongoose 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('Mongoose'), `Mongoose should be in defaults, got ${names.join(', ')}`);
62+
});
63+
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.
71+
Deno.test('mongoose instrumentation: orchestrion:mongoose:model_save channel produces a nested db span', async () => {
72+
resetGlobals();
73+
const sink = transactionSink();
74+
init({
75+
dsn: 'https://username@domain/123',
76+
tracesSampleRate: 1,
77+
beforeSendTransaction: sink.beforeSendTransaction,
78+
});
79+
80+
const channel = tracingChannel('orchestrion:mongoose:model_save');
81+
82+
// `self` is the mongoose document; its `constructor` carries the collection
83+
// (name + connection info) and the model name.
84+
const ctx = {
85+
self: {
86+
constructor: {
87+
collection: { name: 'blogposts', conn: { name: 'mydb', user: 'root', host: '127.0.0.1', port: 27017 } },
88+
modelName: 'BlogPost',
89+
},
90+
},
91+
};
92+
93+
startSpan({ name: 'parent', op: 'test' }, () => {
94+
channel.start.runStores(ctx, () => {
95+
channel.end.publish(ctx);
96+
});
97+
channel.asyncStart.runStores(ctx, () => {
98+
channel.asyncEnd.publish(ctx);
99+
});
100+
});
101+
102+
const parent = await withTimeout(
103+
sink.waitFor(t => t.transaction === 'parent'),
104+
5000,
105+
"'parent' transaction",
106+
);
107+
108+
const mongooseSpan = parent.spans?.find(s => s.op === 'db');
109+
assertExists(mongooseSpan, `expected a db child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`);
110+
assertEquals(mongooseSpan!.description, 'mongoose.BlogPost.save');
111+
assertEquals(mongooseSpan!.data?.['db.system'], 'mongoose');
112+
assertEquals(mongooseSpan!.data?.['db.name'], 'mydb');
113+
assertEquals(mongooseSpan!.data?.['db.mongodb.collection'], 'blogposts');
114+
assertEquals(mongooseSpan!.data?.['db.operation'], 'save');
115+
assertEquals(mongooseSpan!.data?.['db.user'], 'root');
116+
assertEquals(mongooseSpan!.data?.['net.peer.name'], '127.0.0.1');
117+
assertEquals(mongooseSpan!.data?.['net.peer.port'], 27017);
118+
assertEquals(mongooseSpan!.data?.['sentry.origin'], 'auto.db.orchestrion.mongoose');
119+
});

0 commit comments

Comments
 (0)