Skip to content

Commit 38210fe

Browse files
committed
feat(deno): add aws integration
1 parent 093f205 commit 38210fe

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ export type { DenoRedisIntegrationOptions } from './integrations/redis';
117117
// adds to the defaults, so users who customize `defaultIntegrations` can re-add it.
118118
export {
119119
amqplibChannelIntegration,
120+
awsChannelIntegration,
120121
dataloaderChannelIntegration,
121122
expressChannelIntegration,
122123
genericPoolChannelIntegration,

packages/deno/src/sdk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '@sentry/core';
1414
import {
1515
amqplibChannelIntegration,
16+
awsChannelIntegration,
1617
expressChannelIntegration,
1718
genericPoolChannelIntegration,
1819
graphqlDiagnosticsChannelIntegration,
@@ -86,6 +87,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
8687
...(MODULE_REGISTER_HOOKS_SUPPORTED
8788
? [
8889
amqplibChannelIntegration(),
90+
awsChannelIntegration(),
8991
expressChannelIntegration(),
9092
genericPoolChannelIntegration(),
9193
hapiChannelIntegration(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ snapshot[`captureException 1`] = `
117117
"DenoRedis",
118118
"Graphql",
119119
"Amqplib",
120+
"Aws",
120121
"Express",
121122
"GenericPool",
122123
"Hapi",
@@ -207,6 +208,7 @@ snapshot[`captureMessage 1`] = `
207208
"DenoRedis",
208209
"Graphql",
209210
"Amqplib",
211+
"Aws",
210212
"Express",
211213
"GenericPool",
212214
"Hapi",
@@ -304,6 +306,7 @@ snapshot[`captureMessage twice 1`] = `
304306
"DenoRedis",
305307
"Graphql",
306308
"Amqplib",
309+
"Aws",
307310
"Express",
308311
"GenericPool",
309312
"Hapi",
@@ -408,6 +411,7 @@ snapshot[`captureMessage twice 2`] = `
408411
"DenoRedis",
409412
"Graphql",
410413
"Amqplib",
414+
"Aws",
411415
"Express",
412416
"GenericPool",
413417
"Hapi",

0 commit comments

Comments
 (0)