|
| 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('aws-sdk 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('Aws'), `Aws should be in defaults, got ${names.join(', ')}`); |
| 62 | +}); |
| 63 | + |
| 64 | +// Drives the `orchestrion:@smithy/smithy-client:send` channel — the same events |
| 65 | +// the orchestrion transform publishes around the AWS SDK v3 `Client.prototype.send` |
| 66 | +// — so no live AWS client is needed. The subscriber reads the command and client |
| 67 | +// config off the channel context and opens an `rpc` span. The span end is deferred |
| 68 | +// until the client's async `region()` backfill settles (see the integration), so |
| 69 | +// the parent span is held open with an awaited macrotask to let the child end first. |
| 70 | +Deno.test('aws-sdk instrumentation: orchestrion @smithy/smithy-client:send channel produces a nested rpc span', async () => { |
| 71 | + resetGlobals(); |
| 72 | + const sink = transactionSink(); |
| 73 | + init({ |
| 74 | + dsn: 'https://username@domain/123', |
| 75 | + tracesSampleRate: 1, |
| 76 | + beforeSendTransaction: sink.beforeSendTransaction, |
| 77 | + }); |
| 78 | + |
| 79 | + const channel = tracingChannel('orchestrion:@smithy/smithy-client:send'); |
| 80 | + |
| 81 | + // The shape the transform attaches: `arguments[0]` is the v3 command, `self` the |
| 82 | + // client. `serviceId` names the service; the command constructor name (minus the |
| 83 | + // `Command` suffix) names the operation. `region()` resolves the client region. |
| 84 | + const command = { input: {}, constructor: { name: 'DescribeAlarmsCommand' } }; |
| 85 | + const ctx: Record<string, unknown> = { |
| 86 | + arguments: [command], |
| 87 | + self: { config: { serviceId: 'CloudWatch', region: () => 'us-east-1' }, constructor: { name: 'CloudWatchClient' } }, |
| 88 | + }; |
| 89 | + |
| 90 | + await startSpan({ name: 'parent', op: 'test' }, async () => { |
| 91 | + channel.start.runStores(ctx, () => undefined); |
| 92 | + ctx.result = { $metadata: { requestId: 'req-123', httpStatusCode: 200 } }; |
| 93 | + channel.end.publish(ctx); |
| 94 | + channel.asyncEnd.publish(ctx); |
| 95 | + // The span ends after the region backfill microtask; let it settle before the |
| 96 | + // parent span closes so the child is captured on the transaction. |
| 97 | + await new Promise(resolve => setTimeout(resolve, 0)); |
| 98 | + }); |
| 99 | + |
| 100 | + const parent = await withTimeout( |
| 101 | + sink.waitFor(t => t.transaction === 'parent'), |
| 102 | + 5000, |
| 103 | + "'parent' transaction", |
| 104 | + ); |
| 105 | + |
| 106 | + const awsSpan = parent.spans?.find(s => s.op === 'rpc'); |
| 107 | + assertExists(awsSpan, `expected an rpc child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`); |
| 108 | + assertEquals(awsSpan!.description, 'CloudWatch.DescribeAlarms'); |
| 109 | + assertEquals(awsSpan!.data?.['rpc.system'], 'aws-api'); |
| 110 | + assertEquals(awsSpan!.data?.['rpc.service'], 'CloudWatch'); |
| 111 | + assertEquals(awsSpan!.data?.['rpc.method'], 'DescribeAlarms'); |
| 112 | + assertEquals(awsSpan!.data?.['cloud.region'], 'us-east-1'); |
| 113 | + assertEquals(awsSpan!.data?.['sentry.origin'], 'auto.aws.orchestrion.aws_sdk'); |
| 114 | +}); |
0 commit comments