Skip to content

Commit 2fedd2e

Browse files
committed
feat(deno): add kafkajs integration
1 parent 8767f9b commit 2fedd2e

4 files changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts';
9+
import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.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('kafkajs 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('Kafka'), `Kafka should be in defaults, got ${names.join(', ')}`);
62+
});
63+
64+
Deno.test('kafkajs instrumentation: orchestrion:kafkajs:send_batch channel produces a nested producer 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:kafkajs:send_batch');
74+
75+
// `arguments[0]` is the `{ topicMessages }` batch; a producer span is opened per message.
76+
const ctx = { arguments: [{ topicMessages: [{ topic: 'my-topic', messages: [{ value: 'hi' }] }] }] };
77+
78+
startSpan({ name: 'parent', op: 'test' }, () => {
79+
channel.start.publish(ctx);
80+
channel.asyncEnd.publish(ctx);
81+
});
82+
83+
const parent = await withTimeout(
84+
sink.waitFor(t => t.transaction === 'parent'),
85+
5000,
86+
"'parent' transaction",
87+
);
88+
89+
const kafkaSpan = parent.spans?.find(s => s.op === 'message');
90+
assertExists(kafkaSpan, `expected a message child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`);
91+
assertEquals(kafkaSpan!.description, 'send my-topic');
92+
assertEquals(kafkaSpan!.data?.['messaging.system'], 'kafka');
93+
assertEquals(kafkaSpan!.data?.['messaging.destination.name'], 'my-topic');
94+
assertEquals(kafkaSpan!.data?.['sentry.origin'], 'auto.kafkajs.orchestrion.producer');
95+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export {
121121
expressChannelIntegration,
122122
genericPoolChannelIntegration,
123123
hapiChannelIntegration,
124+
kafkajsChannelIntegration,
124125
knexChannelIntegration,
125126
koaChannelIntegration,
126127
lruMemoizerChannelIntegration,

packages/deno/src/sdk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
expressChannelIntegration,
1717
genericPoolChannelIntegration,
1818
hapiChannelIntegration,
19+
kafkajsChannelIntegration,
1920
koaChannelIntegration,
2021
lruMemoizerChannelIntegration,
2122
mongodbChannelIntegration,
@@ -82,6 +83,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
8283
expressChannelIntegration(),
8384
genericPoolChannelIntegration(),
8485
hapiChannelIntegration(),
86+
kafkajsChannelIntegration(),
8587
koaChannelIntegration(),
8688
lruMemoizerChannelIntegration(),
8789
mongodbChannelIntegration(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ snapshot[`captureException 1`] = `
119119
"Express",
120120
"GenericPool",
121121
"Hapi",
122+
"Kafka",
122123
"Koa",
123124
"LruMemoizer",
124125
"Mongo",
@@ -207,6 +208,7 @@ snapshot[`captureMessage 1`] = `
207208
"Express",
208209
"GenericPool",
209210
"Hapi",
211+
"Kafka",
210212
"Koa",
211213
"LruMemoizer",
212214
"Mongo",
@@ -302,6 +304,7 @@ snapshot[`captureMessage twice 1`] = `
302304
"Express",
303305
"GenericPool",
304306
"Hapi",
307+
"Kafka",
305308
"Koa",
306309
"LruMemoizer",
307310
"Mongo",
@@ -404,6 +407,7 @@ snapshot[`captureMessage twice 2`] = `
404407
"Express",
405408
"GenericPool",
406409
"Hapi",
410+
"Kafka",
407411
"Koa",
408412
"LruMemoizer",
409413
"Mongo",

0 commit comments

Comments
 (0)