Skip to content

Commit

Permalink
fix: Fixed amqplib instrumentation via ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners-nr committed Oct 21, 2024
1 parent b917b3e commit 70887b0
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/instrumentation/amqplib/nr-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = [
onRequire: amqplib.instrumentCallbackAPI
},
{
moduleName: 'amqplib/channel_api',
moduleName: 'amqplib',
type: InstrumentationDescriptor.TYPE_MESSAGE,
onRequire: amqplib.instrumentPromiseAPI
}
Expand Down
64 changes: 64 additions & 0 deletions test/versioned/amqplib-esm/issue-2663.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2024 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

import test from 'node:test'
import assert from 'node:assert'
import helper from '../../lib/agent_helper.js'
import promiseResolvers from '../../lib/promise-resolvers.js'

const agent = helper.instrumentMockedAgent({
attributes: {
enabled: true
}
})
const params = {
encoding_key: 'this is an encoding key',
cross_process_id: '1234#4321'
}
agent.config._fromServer(params, 'encoding_key')
agent.config._fromServer(params, 'cross_process_id')
agent.config.trusted_account_ids = [1234]

import amqpUtils from '../amqplib/amqp-utils.js'
// const amqplib = await import('amqplib')
import amqplib from 'amqplib'

test('esm import does instrumentation', async () => {
const { promise, resolve } = promiseResolvers()
const { connection: conn, channel } = await amqpUtils.getChannel(amqplib)
await channel.assertQueue('testQueue')

test.after(async () => {
helper.unloadAgent(agent)
await conn.close()
})

const exchange = amqpUtils.DIRECT_EXCHANGE

await channel.assertExchange(exchange, 'direct')
const { queue } = await channel.assertQueue('', { exclusive: true })
await channel.bindQueue(queue, exchange, 'consume-tx-key')
await helper.runInTransaction(agent, async function (tx) {
channel.publish(exchange, 'consume-tx-key', Buffer.from('hello'))
const msg = await channel.get(queue)
assert.ok(msg, 'should receive a message')
const body = msg.content.toString('utf8')
assert.equal(body, 'hello', 'should receive expected body')

amqpUtils.verifyTransaction(tx, 'get')
channel.ack(msg)
tx.end()
amqpUtils.verifyGet({
tx,
exchangeName: exchange,
routingKey: 'consume-tx-key',
queue,
assertAttr: true
})
resolve()
})

await promise
})
25 changes: 25 additions & 0 deletions test/versioned/amqplib-esm/newrelic.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2020 New Relic Corporation. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/

'use strict'

exports.config = {
app_name: ['My Application'],
license_key: 'license key here',
logging: {
level: 'trace',
filepath: '../../../newrelic_agent.log'
},
utilization: {
detect_aws: false,
detect_pcf: false,
detect_azure: false,
detect_gcp: false,
detect_docker: false
},
transaction_tracer: {
enabled: true
}
}
20 changes: 20 additions & 0 deletions test/versioned/amqplib-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "amqplib-esm-tests",
"version": "0.0.0",
"type": "module",
"private": true,
"tests": [
{
"engines": {
"node": ">=18"
},
"dependencies": {
"amqplib": ">=0.5.0"
},
"files": [
"issue-2663.test.mjs"
]
}
],
"dependencies": {}
}

0 comments on commit 70887b0

Please sign in to comment.