diff --git a/dev-packages/deno-integration-tests/suites/orchestrion-aws/test.ts b/dev-packages/deno-integration-tests/suites/orchestrion-aws/test.ts new file mode 100644 index 000000000000..ee9d529806e9 --- /dev/null +++ b/dev-packages/deno-integration-tests/suites/orchestrion-aws/test.ts @@ -0,0 +1,127 @@ +// + +import { tracingChannel } from 'node:diagnostics_channel'; +import type { Span, TransactionEvent } from '@sentry/core'; +import type { DenoClient } from '@sentry/deno'; +import { getCurrentScope, getGlobalScope, getIsolationScope, init, spanToJSON, 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('aws-sdk 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('Aws'), `Aws should be in defaults, got ${names.join(', ')}`); +}); + +// Drives the `orchestrion:@smithy/smithy-client:send` channel — the same events +// the orchestrion transform publishes around the AWS SDK v3 `Client.prototype.send` +// — so no live AWS client is needed. The subscriber reads the command and client +// config off the channel context and opens an `rpc` span. The span end is deferred +// until the client's async `region()` backfill settles (see the integration), so +// the parent span is held open until the `spanEnd` hook reports the child ended. +Deno.test('aws-sdk instrumentation: orchestrion @smithy/smithy-client:send channel produces a nested rpc span', async () => { + resetGlobals(); + const sink = transactionSink(); + const client = init({ + dsn: 'https://username@domain/123', + tracesSampleRate: 1, + beforeSendTransaction: sink.beforeSendTransaction, + }) as DenoClient; + + // The rpc span ends only after the deferred region backfill settles. Wait on the + // concrete `spanEnd` signal rather than a timer, so the parent stays open until the + // child has actually ended and can be captured on the transaction. + const rpcSpanEnded = new Promise(resolve => { + client.on('spanEnd', (span: Span) => { + if (spanToJSON(span).op === 'rpc') { + resolve(); + } + }); + }); + + const channel = tracingChannel('orchestrion:@smithy/smithy-client:send'); + + // The shape the transform attaches: `arguments[0]` is the v3 command, `self` the + // client. `serviceId` names the service; the command constructor name (minus the + // `Command` suffix) names the operation. `region()` resolves the client region. + const command = { input: {}, constructor: { name: 'DescribeAlarmsCommand' } }; + const ctx: Record = { + arguments: [command], + self: { config: { serviceId: 'CloudWatch', region: () => 'us-east-1' }, constructor: { name: 'CloudWatchClient' } }, + }; + + await startSpan({ name: 'parent', op: 'test' }, async () => { + channel.start.runStores(ctx, () => undefined); + // `send` returns a promise, so `tracePromise` publishes `end` before it settles + // (no `result` yet — the `end` subscriber is a no-op then), then `asyncEnd` once + // the promise resolves with the result. Mirror that order so the span closes on + // `asyncEnd` alone, as it does in production. + channel.end.publish(ctx); + ctx.result = { $metadata: { requestId: 'req-123', httpStatusCode: 200 } }; + channel.asyncEnd.publish(ctx); + await rpcSpanEnded; + }); + + const parent = await withTimeout( + sink.waitFor(t => t.transaction === 'parent'), + 5000, + "'parent' transaction", + ); + + const awsSpan = parent.spans?.find(s => s.op === 'rpc'); + assertExists(awsSpan, `expected an rpc child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`); + assertEquals(awsSpan!.description, 'CloudWatch.DescribeAlarms'); + assertEquals(awsSpan!.data?.['rpc.system'], 'aws-api'); + assertEquals(awsSpan!.data?.['rpc.service'], 'CloudWatch'); + assertEquals(awsSpan!.data?.['rpc.method'], 'DescribeAlarms'); + assertEquals(awsSpan!.data?.['cloud.region'], 'us-east-1'); + assertEquals(awsSpan!.data?.['sentry.origin'], 'auto.aws.orchestrion.aws_sdk'); +}); diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index cc9f64607158..de262530f6da 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -117,6 +117,7 @@ export type { DenoRedisIntegrationOptions } from './integrations/redis'; // adds to the defaults, so users who customize `defaultIntegrations` can re-add it. export { amqplibChannelIntegration, + awsChannelIntegration, dataloaderChannelIntegration, expressChannelIntegration, genericPoolChannelIntegration, diff --git a/packages/deno/src/sdk.ts b/packages/deno/src/sdk.ts index d4dd9ab50b7a..9f2bcdd77c38 100644 --- a/packages/deno/src/sdk.ts +++ b/packages/deno/src/sdk.ts @@ -13,6 +13,7 @@ import { } from '@sentry/core'; import { amqplibChannelIntegration, + awsChannelIntegration, expressChannelIntegration, genericPoolChannelIntegration, graphqlDiagnosticsChannelIntegration, @@ -86,6 +87,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] { ...(MODULE_REGISTER_HOOKS_SUPPORTED ? [ amqplibChannelIntegration(), + awsChannelIntegration(), expressChannelIntegration(), genericPoolChannelIntegration(), hapiChannelIntegration(), diff --git a/packages/deno/test/__snapshots__/mod.test.ts.snap b/packages/deno/test/__snapshots__/mod.test.ts.snap index e42efbeef8d3..d878daee8677 100644 --- a/packages/deno/test/__snapshots__/mod.test.ts.snap +++ b/packages/deno/test/__snapshots__/mod.test.ts.snap @@ -117,6 +117,7 @@ snapshot[`captureException 1`] = ` "DenoRedis", "Graphql", "Amqplib", + "Aws", "Express", "GenericPool", "Hapi", @@ -207,6 +208,7 @@ snapshot[`captureMessage 1`] = ` "DenoRedis", "Graphql", "Amqplib", + "Aws", "Express", "GenericPool", "Hapi", @@ -304,6 +306,7 @@ snapshot[`captureMessage twice 1`] = ` "DenoRedis", "Graphql", "Amqplib", + "Aws", "Express", "GenericPool", "Hapi", @@ -408,6 +411,7 @@ snapshot[`captureMessage twice 2`] = ` "DenoRedis", "Graphql", "Amqplib", + "Aws", "Express", "GenericPool", "Hapi",