Repository navigation
fix(server-utils): Record amqplib consumer info before the first delivery and instrument the callback API #25047
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5a5dece
fix(server-utils): Record amqplib consumer info before the first deli…
s1gr1d 4fdc0fa
update comments and address AI review
s1gr1d 359a071
fix: drop entry when consume fails
s1gr1d 2b5a531
hook the whole file, instrument callback API
s1gr1d 2953336
Merge branch 'develop' into sig/amqplib-fix-consumer-info
s1gr1d b10ddb3
fix AI pr comments
s1gr1d 37d1b44
Merge branch 'develop' into sig/amqplib-fix-consumer-info
s1gr1d c546f20
fix test
s1gr1d 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
51 changes: 51 additions & 0 deletions
51
dev-packages/node-integration-tests/suites/tracing/amqplib/scenario-callback-api.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,51 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import amqp from 'amqplib/callback_api.js'; | ||
|
|
||
| const queueName = 'callback-queue'; | ||
| const amqpUsername = 'sentry'; | ||
| const amqpPassword = 'sentry'; | ||
|
|
||
| const AMQP_URL = `amqp://${amqpUsername}:${amqpPassword}@localhost:5672/`; | ||
|
|
||
| (async () => { | ||
| const connection = await connectToRabbitMQ(); | ||
| const channel = await toPromise(cb => connection.createChannel(cb)); | ||
| await toPromise(cb => channel.assertQueue(queueName, { durable: false, exclusive: true }, cb)); | ||
|
|
||
| // Without the info from `consume`, a `noAck` span stays open until the channel closes and ends as an error. | ||
| const received = new Promise((resolve, reject) => { | ||
| channel.consume( | ||
| queueName, | ||
| message => (message ? resolve() : reject(new Error('No message received'))), | ||
| { noAck: true }, | ||
| err => err && reject(err), | ||
| ); | ||
| }); | ||
|
|
||
| await Sentry.startSpan({ name: 'root span' }, async () => { | ||
| channel.sendToQueue(queueName, Buffer.from(JSON.stringify({ foo: 'bar01' }))); | ||
| }); | ||
|
|
||
| await received; | ||
|
|
||
| await toPromise(cb => channel.close(cb)); | ||
| await toPromise(cb => connection.close(cb)); | ||
| })(); | ||
|
|
||
| async function connectToRabbitMQ() { | ||
| // The broker accepts TCP before AMQP handshakes succeed, so early connects can fail while it boots. | ||
| let lastError; | ||
| for (let attempt = 0; attempt < 5; attempt++) { | ||
| try { | ||
| return await toPromise(cb => amqp.connect(AMQP_URL, cb)); | ||
| } catch (err) { | ||
| lastError = err; | ||
| await new Promise(resolve => setTimeout(resolve, 1000)); | ||
| } | ||
| } | ||
| throw lastError; | ||
| } | ||
|
|
||
| function toPromise(fn) { | ||
| return new Promise((resolve, reject) => fn((err, result) => (err ? reject(err) : resolve(result)))); | ||
| } |
63 changes: 63 additions & 0 deletions
63
dev-packages/node-integration-tests/suites/tracing/amqplib/scenario-topic-noack.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,63 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import amqp from 'amqplib'; | ||
|
|
||
| // A topic exchange, so the routing key and the queue name differ. | ||
| const exchangeName = 'orders'; | ||
| const queueName = 'orders-worker'; | ||
| const routingKey = 'order.created.12345'; | ||
| const amqpUsername = 'sentry'; | ||
| const amqpPassword = 'sentry'; | ||
|
|
||
| const AMQP_URL = `amqp://${amqpUsername}:${amqpPassword}@localhost:5672/`; | ||
|
|
||
| // A `noAck` consumer never settles a message, so its span has to end when dispatch returns. | ||
| const NO_ACKNOWLEDGEMENT = { noAck: true }; | ||
|
|
||
| (async () => { | ||
| const { connection, channel } = await connectToRabbitMQ(); | ||
|
|
||
| await channel.assertExchange(exchangeName, 'topic', { durable: false, autoDelete: true }); | ||
| await channel.assertQueue(queueName, { durable: false, exclusive: true }); | ||
| await channel.bindQueue(queueName, exchangeName, 'order.created.#'); | ||
|
|
||
| // Publish before consuming, so the broker sends `BasicConsumeOk` and the delivery back to back. | ||
| await Sentry.startSpan({ name: 'root span' }, async () => { | ||
| channel.publish(exchangeName, routingKey, Buffer.from(JSON.stringify({ foo: 'bar01' }))); | ||
| }); | ||
|
|
||
| await consumeMessageFromQueue(queueName, channel); | ||
|
|
||
| await channel.close(); | ||
| await connection.close(); | ||
| })(); | ||
|
|
||
| async function connectToRabbitMQ() { | ||
| // The broker accepts TCP before AMQP handshakes succeed, so early connects can reject while it boots. | ||
| let lastError; | ||
| for (let attempt = 0; attempt < 5; attempt++) { | ||
| try { | ||
| const connection = await amqp.connect(AMQP_URL); | ||
| const channel = await connection.createChannel(); | ||
| return { connection, channel }; | ||
| } catch (err) { | ||
| lastError = err; | ||
| await new Promise(resolve => setTimeout(resolve, 1000)); | ||
| } | ||
| } | ||
| throw lastError; | ||
| } | ||
|
|
||
| async function consumeMessageFromQueue(queueName, channel) { | ||
| return new Promise((resolve, reject) => { | ||
| const onMessage = message => (message ? resolve() : reject(new Error('No message received'))); | ||
|
|
||
| // Invalid `arguments` throw before the RPC, so the channel stays open for the next `consume`. | ||
| try { | ||
| channel.consume('orders-archive', onMessage, { arguments: 'invalid' }); | ||
| } catch { | ||
| // Expected. | ||
| } | ||
|
|
||
| channel.consume(queueName, onMessage, NO_ACKNOWLEDGEMENT).catch(reject); | ||
| }); | ||
| } |
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.