Skip to content

Commit aabf21e

Browse files
committed
feat(deno): add firebase integration
1 parent 2f8d8b4 commit aabf21e

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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('firebase 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('Firebase'), `Firebase should be in defaults, got ${names.join(', ')}`);
62+
});
63+
64+
Deno.test('firebase instrumentation: orchestrion @firebase/firestore:add-doc 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:@firebase/firestore:add-doc');
74+
75+
// The subscriber reads these off the reference: `path` names the span/collection,
76+
// `firestore.app` supplies the namespace and project options, `toJSON().settings`
77+
// the server host (omitted here, so no server.address/port attributes).
78+
const reference = {
79+
path: 'users',
80+
type: 'collection',
81+
firestore: {
82+
app: { name: '[DEFAULT]', options: { projectId: 'demo-project', appId: 'demo-app' } },
83+
toJSON: () => ({ settings: {} }),
84+
},
85+
};
86+
const ctx: Record<string, unknown> = { arguments: [reference] };
87+
88+
startSpan({ name: 'parent', op: 'test' }, () => {
89+
channel.start.runStores(ctx, () => undefined);
90+
channel.end.publish(ctx);
91+
ctx.result = {};
92+
channel.asyncStart.runStores(ctx, () => undefined);
93+
channel.asyncEnd.publish(ctx);
94+
});
95+
96+
const parent = await withTimeout(
97+
sink.waitFor(t => t.transaction === 'parent'),
98+
5000,
99+
"'parent' transaction",
100+
);
101+
102+
const fsSpan = parent.spans?.find(s => s.op === 'db.query');
103+
assertExists(fsSpan, `expected a db.query child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`);
104+
assertEquals(fsSpan!.description, 'addDoc users');
105+
assertEquals(fsSpan!.data?.['db.operation.name'], 'addDoc');
106+
assertEquals(fsSpan!.data?.['db.collection.name'], 'users');
107+
assertEquals(fsSpan!.data?.['db.namespace'], '[DEFAULT]');
108+
assertEquals(fsSpan!.data?.['db.system.name'], 'firebase.firestore');
109+
assertEquals(fsSpan!.data?.['firebase.firestore.options.projectId'], 'demo-project');
110+
assertEquals(fsSpan!.data?.['sentry.origin'], 'auto.firebase.orchestrion.firestore');
111+
});

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
awsChannelIntegration,
121121
dataloaderChannelIntegration,
122122
expressChannelIntegration,
123+
firebaseChannelIntegration,
123124
genericPoolChannelIntegration,
124125
graphqlDiagnosticsChannelIntegration,
125126
hapiChannelIntegration,

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
awsChannelIntegration,
1717
expressChannelIntegration,
18+
firebaseChannelIntegration,
1819
genericPoolChannelIntegration,
1920
graphqlDiagnosticsChannelIntegration,
2021
hapiChannelIntegration,
@@ -89,6 +90,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
8990
amqplibChannelIntegration(),
9091
awsChannelIntegration(),
9192
expressChannelIntegration(),
93+
firebaseChannelIntegration(),
9294
genericPoolChannelIntegration(),
9395
hapiChannelIntegration(),
9496
kafkajsChannelIntegration(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ snapshot[`captureException 1`] = `
119119
"Amqplib",
120120
"Aws",
121121
"Express",
122+
"Firebase",
122123
"GenericPool",
123124
"Hapi",
124125
"Kafka",
@@ -210,6 +211,7 @@ snapshot[`captureMessage 1`] = `
210211
"Amqplib",
211212
"Aws",
212213
"Express",
214+
"Firebase",
213215
"GenericPool",
214216
"Hapi",
215217
"Kafka",
@@ -308,6 +310,7 @@ snapshot[`captureMessage twice 1`] = `
308310
"Amqplib",
309311
"Aws",
310312
"Express",
313+
"Firebase",
311314
"GenericPool",
312315
"Hapi",
313316
"Kafka",
@@ -413,6 +416,7 @@ snapshot[`captureMessage twice 2`] = `
413416
"Amqplib",
414417
"Aws",
415418
"Express",
419+
"Firebase",
416420
"GenericPool",
417421
"Hapi",
418422
"Kafka",

0 commit comments

Comments
 (0)