|
| 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 | +}); |
0 commit comments