-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(node): Add
kafkajs
integration (#13528)
- Loading branch information
1 parent
cef6986
commit 1285e4b
Showing
16 changed files
with
196 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
dev-packages/node-integration-tests/suites/tracing/kafkajs/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
services: | ||
db: | ||
image: apache/kafka:latest | ||
restart: always | ||
container_name: integration-tests-kafka | ||
ports: | ||
- '9092:9092' |
63 changes: 63 additions & 0 deletions
63
dev-packages/node-integration-tests/suites/tracing/kafkajs/scenario.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { loggingTransport } = require('@sentry-internal/node-integration-tests'); | ||
const Sentry = require('@sentry/node'); | ||
|
||
Sentry.init({ | ||
dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
release: '1.0', | ||
tracesSampleRate: 1.0, | ||
transport: loggingTransport, | ||
}); | ||
|
||
// Stop the process from exiting before the transaction is sent | ||
setInterval(() => {}, 1000); | ||
|
||
const { Kafka } = require('kafkajs'); | ||
|
||
async function run() { | ||
const kafka = new Kafka({ | ||
clientId: 'my-app', | ||
brokers: ['localhost:9092'], | ||
}); | ||
|
||
const admin = kafka.admin(); | ||
await admin.connect(); | ||
|
||
const producer = kafka.producer(); | ||
await producer.connect(); | ||
|
||
await admin.createTopics({ | ||
topics: [{ topic: 'test-topic' }], | ||
}); | ||
|
||
const consumer = kafka.consumer({ | ||
groupId: 'test-group', | ||
}); | ||
|
||
await consumer.connect(); | ||
await consumer.subscribe({ topic: 'test-topic', fromBeginning: true }); | ||
|
||
consumer.run({ | ||
eachMessage: async ({ message }) => { | ||
// eslint-disable-next-line no-console | ||
console.debug('Received message', message.value.toString()); | ||
}, | ||
}); | ||
|
||
// Wait for the consumer to be ready | ||
await new Promise(resolve => setTimeout(resolve, 4000)); | ||
|
||
await producer.send({ | ||
topic: 'test-topic', | ||
messages: [ | ||
{ | ||
value: 'TEST_MESSAGE', | ||
}, | ||
], | ||
}); | ||
|
||
// Wait for the message to be received | ||
await new Promise(resolve => setTimeout(resolve, 5000)); | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-floating-promises | ||
run(); |
55 changes: 55 additions & 0 deletions
55
dev-packages/node-integration-tests/suites/tracing/kafkajs/test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { cleanupChildProcesses, createRunner } from '../../../utils/runner'; | ||
|
||
// When running docker compose, we need a larger timeout, as this takes some time... | ||
jest.setTimeout(60_000); | ||
|
||
describe('kafkajs', () => { | ||
afterAll(() => { | ||
cleanupChildProcesses(); | ||
}); | ||
|
||
test('traces producers and consumers', done => { | ||
createRunner(__dirname, 'scenario.js') | ||
.withDockerCompose({ | ||
workingDirectory: [__dirname], | ||
readyMatches: ['9092'], | ||
}) | ||
.expect({ | ||
transaction: { | ||
transaction: 'test-topic', | ||
contexts: { | ||
trace: expect.objectContaining({ | ||
op: 'message', | ||
status: 'ok', | ||
data: expect.objectContaining({ | ||
'messaging.system': 'kafka', | ||
'messaging.destination': 'test-topic', | ||
'otel.kind': 'PRODUCER', | ||
'sentry.op': 'message', | ||
'sentry.origin': 'auto.kafkajs.otel.producer', | ||
}), | ||
}), | ||
}, | ||
}, | ||
}) | ||
.expect({ | ||
transaction: { | ||
transaction: 'test-topic', | ||
contexts: { | ||
trace: expect.objectContaining({ | ||
op: 'message', | ||
status: 'ok', | ||
data: expect.objectContaining({ | ||
'messaging.system': 'kafka', | ||
'messaging.destination': 'test-topic', | ||
'otel.kind': 'CONSUMER', | ||
'sentry.op': 'message', | ||
'sentry.origin': 'auto.kafkajs.otel.consumer', | ||
}), | ||
}), | ||
}, | ||
}, | ||
}) | ||
.start(done); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { KafkaJsInstrumentation } from '@opentelemetry/instrumentation-kafkajs'; | ||
|
||
import { defineIntegration } from '@sentry/core'; | ||
import type { IntegrationFn } from '@sentry/types'; | ||
import { generateInstrumentOnce } from '../../otel/instrument'; | ||
import { addOriginToSpan } from '../../utils/addOriginToSpan'; | ||
|
||
const INTEGRATION_NAME = 'Kafka'; | ||
|
||
export const instrumentKafka = generateInstrumentOnce( | ||
INTEGRATION_NAME, | ||
() => | ||
new KafkaJsInstrumentation({ | ||
consumerHook(span) { | ||
addOriginToSpan(span, 'auto.kafkajs.otel.consumer'); | ||
}, | ||
producerHook(span) { | ||
addOriginToSpan(span, 'auto.kafkajs.otel.producer'); | ||
}, | ||
}), | ||
); | ||
|
||
const _kafkaIntegration = (() => { | ||
return { | ||
name: INTEGRATION_NAME, | ||
setupOnce() { | ||
instrumentKafka(); | ||
}, | ||
}; | ||
}) satisfies IntegrationFn; | ||
|
||
/** | ||
* KafkaJs integration | ||
* | ||
* Capture tracing data for KafkaJs. | ||
*/ | ||
export const kafkaIntegration = defineIntegration(_kafkaIntegration); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.