From 1f07c4253e00472cc7308e16929d15d2b3df5999 Mon Sep 17 00:00:00 2001 From: isaacs Date: Tue, 21 Jul 2026 13:16:47 -0700 Subject: [PATCH] feat(deno): add firebase integration --- .../suites/orchestrion-firebase/test.ts | 111 ++++++++++++++++++ packages/deno/src/index.ts | 1 + packages/deno/src/sdk.ts | 2 + .../deno/test/__snapshots__/mod.test.ts.snap | 4 + 4 files changed, 118 insertions(+) create mode 100644 dev-packages/deno-integration-tests/suites/orchestrion-firebase/test.ts diff --git a/dev-packages/deno-integration-tests/suites/orchestrion-firebase/test.ts b/dev-packages/deno-integration-tests/suites/orchestrion-firebase/test.ts new file mode 100644 index 000000000000..e8625ddcac5d --- /dev/null +++ b/dev-packages/deno-integration-tests/suites/orchestrion-firebase/test.ts @@ -0,0 +1,111 @@ +// + +import { tracingChannel } from 'node:diagnostics_channel'; +import type { TransactionEvent } from '@sentry/core'; +import type { DenoClient } from '@sentry/deno'; +import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '@sentry/deno'; +import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts'; +import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts'; +import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts'; + +function resetGlobals(): void { + getCurrentScope().clear(); + getCurrentScope().setClient(undefined); + getIsolationScope().clear(); + getGlobalScope().clear(); +} + +/** See deno-redis.test.ts — same sink shape, deduped for clarity. */ +function transactionSink(): { + beforeSendTransaction: (event: TransactionEvent) => null; + waitFor: (predicate: (event: TransactionEvent) => boolean) => Promise; +} { + const transactions: TransactionEvent[] = []; + const waiters: { predicate: (e: TransactionEvent) => boolean; resolve: (e: TransactionEvent) => void }[] = []; + return { + beforeSendTransaction(event) { + transactions.push(event); + for (let i = waiters.length - 1; i >= 0; i--) { + const w = waiters[i]!; + if (w.predicate(event)) { + waiters.splice(i, 1); + w.resolve(event); + } + } + return null; + }, + waitFor(predicate) { + const already = transactions.find(predicate); + if (already) return Promise.resolve(already); + return new Promise(resolve => { + waiters.push({ predicate, resolve }); + }); + }, + }; +} + +function withTimeout(p: Promise, ms: number, what: string): Promise { + let timer: ReturnType | undefined; + const timeout = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(`Timed out waiting for ${what} after ${ms}ms`)), ms); + }); + return Promise.race([p, timeout]).finally(() => { + if (timer !== undefined) clearTimeout(timer); + }); +} + +Deno.test('firebase instrumentation: included in default integrations (Deno 2.8.0+)', () => { + resetGlobals(); + const client = init({ dsn: 'https://username@domain/123' }) as DenoClient; + const names = client.getOptions().integrations.map(i => i.name); + assert(names.includes('Firebase'), `Firebase should be in defaults, got ${names.join(', ')}`); +}); + +Deno.test('firebase instrumentation: orchestrion @firebase/firestore:add-doc channel produces a nested db span', async () => { + resetGlobals(); + const sink = transactionSink(); + init({ + dsn: 'https://username@domain/123', + tracesSampleRate: 1, + beforeSendTransaction: sink.beforeSendTransaction, + }); + + const channel = tracingChannel('orchestrion:@firebase/firestore:add-doc'); + + // The subscriber reads these off the reference: `path` names the span/collection, + // `firestore.app` supplies the namespace and project options, `toJSON().settings` + // the server host (omitted here, so no server.address/port attributes). + const reference = { + path: 'users', + type: 'collection', + firestore: { + app: { name: '[DEFAULT]', options: { projectId: 'demo-project', appId: 'demo-app' } }, + toJSON: () => ({ settings: {} }), + }, + }; + const ctx: Record = { arguments: [reference] }; + + startSpan({ name: 'parent', op: 'test' }, () => { + channel.start.runStores(ctx, () => undefined); + channel.end.publish(ctx); + ctx.result = {}; + channel.asyncStart.runStores(ctx, () => undefined); + channel.asyncEnd.publish(ctx); + }); + + const parent = await withTimeout( + sink.waitFor(t => t.transaction === 'parent'), + 5000, + "'parent' transaction", + ); + + const fsSpan = parent.spans?.find(s => s.op === 'db.query'); + assertExists(fsSpan, `expected a db.query child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`); + assertEquals(fsSpan!.description, 'addDoc users'); + assertEquals(fsSpan!.data?.['db.operation.name'], 'addDoc'); + assertEquals(fsSpan!.data?.['db.collection.name'], 'users'); + assertEquals(fsSpan!.data?.['db.namespace'], '[DEFAULT]'); + assertEquals(fsSpan!.data?.['db.system.name'], 'firebase.firestore'); + assertEquals(fsSpan!.data?.['firebase.firestore.options.projectId'], 'demo-project'); + assertEquals(fsSpan!.data?.['sentry.origin'], 'auto.firebase.orchestrion.firestore'); +}); diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index de262530f6da..28ef625c3cf5 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -120,6 +120,7 @@ export { awsChannelIntegration, dataloaderChannelIntegration, expressChannelIntegration, + firebaseChannelIntegration, genericPoolChannelIntegration, graphqlDiagnosticsChannelIntegration, hapiChannelIntegration, diff --git a/packages/deno/src/sdk.ts b/packages/deno/src/sdk.ts index 9f2bcdd77c38..0165e49b9c45 100644 --- a/packages/deno/src/sdk.ts +++ b/packages/deno/src/sdk.ts @@ -15,6 +15,7 @@ import { amqplibChannelIntegration, awsChannelIntegration, expressChannelIntegration, + firebaseChannelIntegration, genericPoolChannelIntegration, graphqlDiagnosticsChannelIntegration, hapiChannelIntegration, @@ -89,6 +90,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] { amqplibChannelIntegration(), awsChannelIntegration(), expressChannelIntegration(), + firebaseChannelIntegration(), genericPoolChannelIntegration(), hapiChannelIntegration(), kafkajsChannelIntegration(), diff --git a/packages/deno/test/__snapshots__/mod.test.ts.snap b/packages/deno/test/__snapshots__/mod.test.ts.snap index d878daee8677..c8c91ca9317c 100644 --- a/packages/deno/test/__snapshots__/mod.test.ts.snap +++ b/packages/deno/test/__snapshots__/mod.test.ts.snap @@ -119,6 +119,7 @@ snapshot[`captureException 1`] = ` "Amqplib", "Aws", "Express", + "Firebase", "GenericPool", "Hapi", "Kafka", @@ -210,6 +211,7 @@ snapshot[`captureMessage 1`] = ` "Amqplib", "Aws", "Express", + "Firebase", "GenericPool", "Hapi", "Kafka", @@ -308,6 +310,7 @@ snapshot[`captureMessage twice 1`] = ` "Amqplib", "Aws", "Express", + "Firebase", "GenericPool", "Hapi", "Kafka", @@ -413,6 +416,7 @@ snapshot[`captureMessage twice 2`] = ` "Amqplib", "Aws", "Express", + "Firebase", "GenericPool", "Hapi", "Kafka",