-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
ref(node): Vendor amqplib instrumentation #21003
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
nicohrubec
wants to merge
6
commits into
develop
Choose a base branch
from
nh/vendor-amqplib-instrumentation
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
663b0c0
ref(node): Vendor amqplib instrumentation
nicohrubec e716ad9
yarn fix
nicohrubec 8573651
test(node): Add amqplib v2 integration test
nicohrubec a06a635
test(node): Fix amqplib test to test v1 (not v2)
nicohrubec 1096fe3
test: Use unique container name for amqplib v1 test
nicohrubec 88795af
test: Use different ports for amqplib v1 test to avoid conflicts
nicohrubec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
22 changes: 22 additions & 0 deletions
22
dev-packages/node-integration-tests/suites/tracing/amqplib-v1/docker-compose.yml
This file contains hidden or 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,22 @@ | ||
| version: '3' | ||
|
|
||
| services: | ||
| rabbitmq: | ||
| image: rabbitmq:management | ||
| container_name: rabbitmq-v1 | ||
| environment: | ||
| - RABBITMQ_DEFAULT_USER=sentry | ||
| - RABBITMQ_DEFAULT_PASS=sentry | ||
| ports: | ||
| - '5673:5672' | ||
| - '15673:15672' | ||
| healthcheck: | ||
| test: ['CMD-SHELL', 'rabbitmq-diagnostics -q ping'] | ||
| interval: 2s | ||
| timeout: 10s | ||
| retries: 30 | ||
| start_period: 15s | ||
|
|
||
| networks: | ||
| default: | ||
| driver: bridge |
9 changes: 9 additions & 0 deletions
9
dev-packages/node-integration-tests/suites/tracing/amqplib-v1/instrument.mjs
This file contains hidden or 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,9 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| transport: loggingTransport, | ||
| }); |
7 changes: 7 additions & 0 deletions
7
dev-packages/node-integration-tests/suites/tracing/amqplib-v1/package.json
This file contains hidden or 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 @@ | ||
| { | ||
| "name": "sentry-amqplib-v1-test", | ||
| "version": "1.0.0", | ||
| "dependencies": { | ||
| "amqplib": "^1.0.0" | ||
| } | ||
| } |
72 changes: 72 additions & 0 deletions
72
dev-packages/node-integration-tests/suites/tracing/amqplib-v1/scenario.mjs
This file contains hidden or 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,72 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import amqp from 'amqplib'; | ||
|
|
||
| const queueName = 'queue1'; | ||
| const amqpUsername = 'sentry'; | ||
| const amqpPassword = 'sentry'; | ||
|
|
||
| const AMQP_URL = `amqp://${amqpUsername}:${amqpPassword}@localhost:5673/`; | ||
| const ACKNOWLEDGEMENT = { noAck: false }; | ||
|
|
||
| const QUEUE_OPTIONS = { | ||
| durable: true, // Make the queue durable | ||
| exclusive: false, // Not exclusive | ||
| autoDelete: false, // Don't auto-delete the queue | ||
| arguments: { | ||
| 'x-message-ttl': 30000, // Message TTL of 30 seconds | ||
| 'x-max-length': 1000, // Maximum queue length of 1000 messages | ||
| }, | ||
| }; | ||
|
|
||
| (async () => { | ||
| const { connection, channel } = await connectToRabbitMQ(); | ||
| await createQueue(queueName, channel); | ||
|
|
||
| const consumeMessagePromise = consumeMessageFromQueue(queueName, channel); | ||
|
|
||
| await Sentry.startSpan({ name: 'root span' }, async () => { | ||
| sendMessageToQueue(queueName, channel, JSON.stringify({ foo: 'bar01' })); | ||
| }); | ||
|
|
||
| await consumeMessagePromise; | ||
|
|
||
| await channel.close(); | ||
| await connection.close(); | ||
| })(); | ||
|
|
||
| async function connectToRabbitMQ() { | ||
| const connection = await amqp.connect(AMQP_URL); | ||
| const channel = await connection.createChannel(); | ||
| return { connection, channel }; | ||
| } | ||
|
|
||
| async function createQueue(queueName, channel) { | ||
| await channel.assertQueue(queueName, QUEUE_OPTIONS); | ||
| } | ||
|
|
||
| function sendMessageToQueue(queueName, channel, message) { | ||
| channel.sendToQueue(queueName, Buffer.from(message)); | ||
| } | ||
|
|
||
| async function consumer(queueName, channel) { | ||
| return new Promise((resolve, reject) => { | ||
| channel | ||
| .consume( | ||
| queueName, | ||
| message => { | ||
| if (message) { | ||
| channel.ack(message); | ||
| resolve(); | ||
| } else { | ||
| reject(new Error('No message received')); | ||
| } | ||
| }, | ||
| ACKNOWLEDGEMENT, | ||
| ) | ||
| .catch(reject); | ||
| }); | ||
| } | ||
|
|
||
| async function consumeMessageFromQueue(queueName, channel) { | ||
| await consumer(queueName, channel); | ||
| } |
55 changes: 55 additions & 0 deletions
55
dev-packages/node-integration-tests/suites/tracing/amqplib-v1/test.ts
This file contains hidden or 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 type { TransactionEvent } from '@sentry/core'; | ||
| import { afterAll, describe, expect } from 'vitest'; | ||
| import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; | ||
|
|
||
| const EXPECTED_MESSAGE_SPAN_PRODUCER = expect.objectContaining({ | ||
| op: 'message', | ||
| data: expect.objectContaining({ | ||
| 'messaging.system': 'rabbitmq', | ||
| 'otel.kind': 'PRODUCER', | ||
| 'sentry.op': 'message', | ||
| 'sentry.origin': 'auto.amqplib.otel.publisher', | ||
| }), | ||
| status: 'ok', | ||
| }); | ||
|
|
||
| const EXPECTED_MESSAGE_SPAN_CONSUMER = expect.objectContaining({ | ||
| op: 'message', | ||
| data: expect.objectContaining({ | ||
| 'messaging.system': 'rabbitmq', | ||
| 'otel.kind': 'CONSUMER', | ||
| 'sentry.op': 'message', | ||
| 'sentry.origin': 'auto.amqplib.otel.consumer', | ||
| }), | ||
| status: 'ok', | ||
| }); | ||
|
|
||
| describe('amqplib v1 auto-instrumentation', () => { | ||
| afterAll(async () => { | ||
| cleanupChildProcesses(); | ||
| }); | ||
|
|
||
| createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createTestRunner, test) => { | ||
| test('should be able to send and receive messages with amqplib v1', { timeout: 60_000 }, async () => { | ||
| await createTestRunner() | ||
| .withDockerCompose({ | ||
| workingDirectory: [__dirname], | ||
| }) | ||
| .expect({ | ||
| transaction: (transaction: TransactionEvent) => { | ||
| expect(transaction.transaction).toEqual('root span'); | ||
| expect(transaction.spans?.length).toEqual(1); | ||
| expect(transaction.spans![0]).toMatchObject(EXPECTED_MESSAGE_SPAN_PRODUCER); | ||
| }, | ||
| }) | ||
| .expect({ | ||
| transaction: (transaction: TransactionEvent) => { | ||
| expect(transaction.transaction).toEqual('queue1 process'); | ||
| expect(transaction.contexts?.trace).toMatchObject(EXPECTED_MESSAGE_SPAN_CONSUMER); | ||
| }, | ||
| }) | ||
| .start() | ||
| .completed(); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or 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
3 changes: 2 additions & 1 deletion
3
.../node/src/integrations/tracing/amqplib.ts → ...src/integrations/tracing/amqplib/index.ts
This file contains hidden or 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
47 changes: 47 additions & 0 deletions
47
packages/node/src/integrations/tracing/amqplib/vendored/amqplib-types.ts
This file contains hidden or 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,47 @@ | ||
| /* | ||
| * Simplified types inlined from @types/amqplib (DefinitelyTyped). | ||
| * Only includes members accessed by this instrumentation. | ||
| * Other amqplib types (Message, ConsumeMessage, Options.Publish, etc.) are already | ||
| * vendored in types.ts by the upstream OTel instrumentation. | ||
| */ | ||
|
|
||
| export interface Connection { | ||
| connection: { serverProperties: { product?: string; [key: string]: any } }; | ||
| [key: string]: any; | ||
| } | ||
|
|
||
| export interface Channel { | ||
| connection: Connection; | ||
| [key: string]: any; | ||
| } | ||
|
|
||
| export interface ConfirmChannel extends Channel {} | ||
|
|
||
| export namespace Options { | ||
| export interface Connect { | ||
| protocol?: string; | ||
| hostname?: string; | ||
| port?: number; | ||
| username?: string; | ||
| vhost?: string; | ||
| } | ||
| export interface Consume { | ||
| consumerTag?: string; | ||
| noLocal?: boolean; | ||
| noAck?: boolean; | ||
| exclusive?: boolean; | ||
| priority?: number; | ||
| arguments?: any; | ||
| } | ||
| export interface Publish { | ||
| headers?: any; | ||
| [key: string]: any; | ||
| } | ||
| } | ||
|
|
||
| export namespace Replies { | ||
| export interface Empty {} | ||
| export interface Consume { | ||
| consumerTag: string; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The code accesses
conn.serverPropertiesdirectly, but the actualamqplibconnection object nests this property underconn.connection.serverProperties, causing a TypeError.Severity: CRITICAL
Suggested Fix
In
utils.ts, change the access fromconn.serverProperties.product?.toLowerCase?.()toconn.connection.serverProperties.product?.toLowerCase?.()to match the actual structure of theamqplibConnectionobject.Prompt for AI Agent
Also affects:
packages/node/src/integrations/tracing/amqplib/vendored/utils.ts:105~107Did we get this right? 👍 / 👎 to inform future reviews.