Skip to content

Commit

Permalink
test: Migrated test/versioned/amqplib to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
bizob2828 committed Sep 24, 2024
1 parent 935ac14 commit 7306d19
Show file tree
Hide file tree
Showing 5 changed files with 587 additions and 781 deletions.
202 changes: 91 additions & 111 deletions test/versioned/amqplib/amqp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
*/

'use strict'

const semver = require('semver')

const assert = require('node:assert')
const DESTINATIONS = require('../../../lib/config/attribute-filter').DESTINATIONS
const params = require('../../lib/params')
const metrics = require('../../lib/metrics_helper')
const { assertMetrics, assertSegments } = require('./../../lib/custom-assertions')

const CON_STRING = 'amqp://' + params.rabbitmq_host + ':' + params.rabbitmq_port
const { version: pkgVersion } = require('amqplib/package')
const NATIVE_PROMISES = semver.gte(pkgVersion, '0.10.0')

exports.CON_STRING = CON_STRING
exports.DIRECT_EXCHANGE = 'test-direct-exchange'
exports.FANOUT_EXCHANGE = 'test-fanout-exchange'
Expand All @@ -30,99 +26,95 @@ exports.verifySendToQueue = verifySendToQueue
exports.verifyTransaction = verifyTransaction
exports.getChannel = getChannel

function verifySubscribe(t, tx, exchange, routingKey) {
function verifySubscribe(tx, exchange, routingKey) {
const isCallback = !!metrics.findSegment(tx.trace.root, 'Callback: <anonymous>')

let segments = []

if (isCallback) {
segments = [
'amqplib.Channel#consume',
['Callback: <anonymous>', ['MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchange]]
]
} else if (NATIVE_PROMISES) {
segments = [
'amqplib.Channel#consume',
'MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchange
]
} else {
segments = [
'amqplib.Channel#consume',
['MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchange]
]
segments = ['MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchange]
}

t.assertSegments(tx.trace.root, segments)
assertSegments(tx.trace.root, segments)

t.assertMetrics(
assertMetrics(
tx.metrics,
[[{ name: 'MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchange }]],
false,
false
)

t.notMatch(tx.getFullName(), /^OtherTransaction\/Message/, 'should not set transaction name')
assert.equal(tx.getFullName(), null, 'should not set transaction name')

const consume = metrics.findSegment(
tx.trace.root,
'MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchange
)
t.equal(consume.getAttributes().routing_key, routingKey, 'should store routing key')
assert.equal(consume.getAttributes().routing_key, routingKey, 'should store routing key')
}

function verifyCAT(t, produceTransaction, consumeTransaction) {
t.equal(
function verifyCAT(produceTransaction, consumeTransaction) {
assert.equal(
consumeTransaction.incomingCatId,
produceTransaction.agent.config.cross_process_id,
'should have the proper incoming CAT id'
)
t.equal(
assert.equal(
consumeTransaction.referringTransactionGuid,
produceTransaction.id,
'should have the the correct referring transaction guid'
)
t.equal(consumeTransaction.tripId, produceTransaction.id, 'should have the the correct trip id')
t.notOk(
consumeTransaction.invalidIncomingExternalTransaction,
assert.equal(
consumeTransaction.tripId,
produceTransaction.id,
'should have the the correct trip id'
)
assert.ok(
!consumeTransaction.invalidIncomingExternalTransaction,
'invalid incoming external transaction should be false'
)
}

function verifyDistributedTrace(t, produceTransaction, consumeTransaction) {
t.ok(produceTransaction.isDistributedTrace, 'should mark producer as distributed')
t.ok(consumeTransaction.isDistributedTrace, 'should mark consumer as distributed')

t.equal(consumeTransaction.incomingCatId, null, 'should not set old CAT properties')

t.equal(produceTransaction.id, consumeTransaction.parentId, 'should have proper parent id')
t.equal(produceTransaction.traceId, consumeTransaction.traceId, 'should have proper trace id')
// native promises flatten the segment tree, grab the product segment as 2nd child of root
let produceSegment =
NATIVE_PROMISES && produceTransaction.trace.root.children.length > 1
? produceTransaction.trace.root.children[1]
: produceTransaction.trace.root.children[0].children[0]
produceSegment = produceSegment.children[0] || produceSegment
t.equal(produceSegment.id, consumeTransaction.parentSpanId, 'should have proper parentSpanId')
t.equal(consumeTransaction.parentTransportType, 'AMQP', 'should have correct transport type')
function verifyDistributedTrace(produceTransaction, consumeTransaction) {
assert.ok(produceTransaction.isDistributedTrace, 'should mark producer as distributed')
assert.ok(consumeTransaction.isDistributedTrace, 'should mark consumer as distributed')

assert.equal(consumeTransaction.incomingCatId, null, 'should not set old CAT properties')

assert.equal(produceTransaction.id, consumeTransaction.parentId, 'should have proper parent id')
assert.equal(
produceTransaction.traceId,
consumeTransaction.traceId,
'should have proper trace id'
)
const produceSegment = produceTransaction.trace.root.children[0]
assert.equal(
produceSegment.id,
consumeTransaction.parentSpanId,
'should have proper parentSpanId'
)
assert.equal(consumeTransaction.parentTransportType, 'AMQP', 'should have correct transport type')
}

function verifyConsumeTransaction(t, tx, exchange, queue, routingKey) {
t.doesNotThrow(function () {
t.assertMetrics(
tx.metrics,
[
[{ name: 'OtherTransaction/Message/RabbitMQ/Exchange/Named/' + exchange }],
[{ name: 'OtherTransactionTotalTime/Message/RabbitMQ/Exchange/Named/' + exchange }],
[{ name: 'OtherTransaction/Message/all' }],
[{ name: 'OtherTransaction/all' }],
[{ name: 'OtherTransactionTotalTime' }]
],
false,
false
)
}, 'should have expected metrics')

t.equal(
function verifyConsumeTransaction(tx, exchange, queue, routingKey) {
assertMetrics(
tx.metrics,
[
[{ name: 'OtherTransaction/Message/RabbitMQ/Exchange/Named/' + exchange }],
[{ name: 'OtherTransactionTotalTime/Message/RabbitMQ/Exchange/Named/' + exchange }],
[{ name: 'OtherTransaction/Message/all' }],
[{ name: 'OtherTransaction/all' }],
[{ name: 'OtherTransactionTotalTime' }]
],
false,
false
)

assert.equal(
tx.getFullName(),
'OtherTransaction/Message/RabbitMQ/Exchange/Named/' + exchange,
'should not set transaction name'
Expand All @@ -132,24 +124,28 @@ function verifyConsumeTransaction(t, tx, exchange, queue, routingKey) {
tx.trace.root,
'OtherTransaction/Message/RabbitMQ/Exchange/Named/' + exchange
)
t.equal(consume, tx.baseSegment)
assert.equal(consume, tx.baseSegment)
const segmentAttrs = consume.getAttributes()
t.equal(segmentAttrs.host, params.rabbitmq_host, 'should have host on segment')
t.equal(segmentAttrs.port, params.rabbitmq_port, 'should have port on segment')
assert.equal(segmentAttrs.host, params.rabbitmq_host, 'should have host on segment')
assert.equal(segmentAttrs.port, params.rabbitmq_port, 'should have port on segment')

const attributes = tx.trace.attributes.get(DESTINATIONS.TRANS_TRACE)
t.equal(
assert.equal(
attributes['message.routingKey'],
routingKey,
'should have routing key transaction parameter'
)
t.equal(attributes['message.queueName'], queue, 'should have queue name transaction parameter')
assert.equal(
attributes['message.queueName'],
queue,
'should have queue name transaction parameter'
)
}

function verifySendToQueue(t, tx) {
t.assertSegments(tx.trace.root, ['MessageBroker/RabbitMQ/Exchange/Produce/Named/Default'])
function verifySendToQueue(tx) {
assertSegments(tx.trace.root, ['MessageBroker/RabbitMQ/Exchange/Produce/Named/Default'])

t.assertMetrics(
assertMetrics(
tx.metrics,
[[{ name: 'MessageBroker/RabbitMQ/Exchange/Produce/Named/Default' }]],
false,
Expand All @@ -161,14 +157,14 @@ function verifySendToQueue(t, tx) {
'MessageBroker/RabbitMQ/Exchange/Produce/Named/Default'
)
const attributes = segment.getAttributes()
t.equal(attributes.host, params.rabbitmq_host, 'should have host on segment')
t.equal(attributes.port, params.rabbitmq_port, 'should have port on segment')
t.equal(attributes.routing_key, 'testQueue', 'should store routing key')
t.equal(attributes.reply_to, 'my.reply.queue', 'should store reply to')
t.equal(attributes.correlation_id, 'correlation-id', 'should store correlation id')
assert.equal(attributes.host, params.rabbitmq_host, 'should have host on segment')
assert.equal(attributes.port, params.rabbitmq_port, 'should have port on segment')
assert.equal(attributes.routing_key, 'testQueue', 'should store routing key')
assert.equal(attributes.reply_to, 'my.reply.queue', 'should store reply to')
assert.equal(attributes.correlation_id, 'correlation-id', 'should store correlation id')
}

function verifyProduce(t, tx, exchangeName, routingKey) {
function verifyProduce(tx, exchangeName, routingKey) {
const isCallback = !!metrics.findSegment(tx.trace.root, 'Callback: <anonymous>')
let segments = []

Expand All @@ -194,26 +190,18 @@ function verifyProduce(t, tx, exchangeName, routingKey) {
]
// 0.9.0 flattened the segment tree
// See: https://github.com/amqp-node/amqplib/pull/635/files
} else if (semver.gte(pkgVersion, '0.9.0')) {
} else {
segments = [
'Channel#assertExchange',
'Channel#assertQueue',
'Channel#bindQueue',
'MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchangeName
]
} else {
segments = [
'Channel#assertExchange',
[
'Channel#assertQueue',
['Channel#bindQueue', ['MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchangeName]]
]
]
}

t.assertSegments(tx.trace.root, segments, 'should have expected segments')
assertSegments(tx.trace.root, segments, 'should have expected segments')

t.assertMetrics(
assertMetrics(
tx.metrics,
[[{ name: 'MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchangeName }]],
false,
Expand All @@ -226,35 +214,35 @@ function verifyProduce(t, tx, exchangeName, routingKey) {
)
const attributes = segment.getAttributes()
if (routingKey) {
t.equal(attributes.routing_key, routingKey, 'should have routing key')
assert.equal(attributes.routing_key, routingKey, 'should have routing key')
} else {
t.notOk(attributes.routing_key, 'should not have routing key')
assert.ok(!attributes.routing_key, 'should not have routing key')
}

t.equal(attributes.host, params.rabbitmq_host, 'should have host on segment')
t.equal(attributes.port, params.rabbitmq_port, 'should have port on segment')
assert.equal(attributes.host, params.rabbitmq_host, 'should have host on segment')
assert.equal(attributes.port, params.rabbitmq_port, 'should have port on segment')
}

function verifyGet({ t, tx, exchangeName, routingKey, queue, assertAttr }) {
function verifyGet({ tx, exchangeName, routingKey, queue, assertAttr }) {
const isCallback = !!metrics.findSegment(tx.trace.root, 'Callback: <anonymous>')
const produceName = 'MessageBroker/RabbitMQ/Exchange/Produce/Named/' + exchangeName
const consumeName = 'MessageBroker/RabbitMQ/Exchange/Consume/Named/' + queue
if (isCallback) {
t.assertSegments(tx.trace.root, [produceName, consumeName, ['Callback: <anonymous>']])
assertSegments(tx.trace.root, [produceName, consumeName, ['Callback: <anonymous>']])
} else {
t.assertSegments(tx.trace.root, [produceName, consumeName])
assertSegments(tx.trace.root, [produceName, consumeName])
}
t.assertMetrics(tx.metrics, [[{ name: produceName }], [{ name: consumeName }]], false, false)
assertMetrics(tx.metrics, [[{ name: produceName }], [{ name: consumeName }]], false, false)
if (assertAttr) {
const segment = metrics.findSegment(tx.trace.root, consumeName)
const attributes = segment.getAttributes()
t.equal(attributes.host, params.rabbitmq_host, 'should have host on segment')
t.equal(attributes.port, params.rabbitmq_port, 'should have port on segment')
t.equal(attributes.routing_key, routingKey, 'should have routing key on get')
assert.equal(attributes.host, params.rabbitmq_host, 'should have host on segment')
assert.equal(attributes.port, params.rabbitmq_port, 'should have port on segment')
assert.equal(attributes.routing_key, routingKey, 'should have routing key on get')
}
}

function verifyPurge(t, tx) {
function verifyPurge(tx) {
const isCallback = !!metrics.findSegment(tx.trace.root, 'Callback: <anonymous>')
let segments = []

Expand All @@ -278,31 +266,23 @@ function verifyPurge(t, tx) {
]
]
]
// 0.9.0 flattened the segment tree
// See: https://github.com/amqp-node/amqplib/pull/635/files
} else if (semver.gte(pkgVersion, '0.9.0')) {
} else {
segments = [
'Channel#assertExchange',
'Channel#assertQueue',
'Channel#bindQueue',
'MessageBroker/RabbitMQ/Queue/Purge/Temp'
]
} else {
segments = [
'Channel#assertExchange',
['Channel#assertQueue', ['Channel#bindQueue', ['MessageBroker/RabbitMQ/Queue/Purge/Temp']]]
]
}
assertSegments(tx.trace.root, segments, 'should have expected segments')

t.assertSegments(tx.trace.root, segments, 'should have expected segments')

t.assertMetrics(tx.metrics, [[{ name: 'MessageBroker/RabbitMQ/Queue/Purge/Temp' }]], false, false)
assertMetrics(tx.metrics, [[{ name: 'MessageBroker/RabbitMQ/Queue/Purge/Temp' }]], false, false)
}

function verifyTransaction(t, tx, msg) {
function verifyTransaction(tx, msg) {
const seg = tx.agent.tracer.getSegment()
if (t.ok(seg, 'should have transaction state in ' + msg)) {
t.equal(seg.transaction.id, tx.id, 'should have correct transaction in ' + msg)
if (seg) {
assert.equal(seg.transaction.id, tx.id, 'should have correct transaction in ' + msg)
}
}

Expand Down
Loading

0 comments on commit 7306d19

Please sign in to comment.