Skip to content

Commit

Permalink
test: converted metrics-recorder to node:test
Browse files Browse the repository at this point in the history
  • Loading branch information
amychisholm03 committed Sep 9, 2024
1 parent 6d6efc9 commit f694410
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 447 deletions.
23 changes: 12 additions & 11 deletions test/lib/metrics_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
'use strict'

const tap = require('tap')
const assert = require('node:assert')
const urltils = require('../../lib/util/urltils')
const { isSimpleObject } = require('../../lib/util/objects')

exports.findSegment = findSegment
exports.getMetricHostName = getMetricHostName
tap.Test.prototype.addAssert('assertMetrics', 4, assertMetrics)
exports.assertMetrics = assertMetrics
exports.assertMetricValues = assertMetricValues
tap.Test.prototype.addAssert('assertSegments', 3, assertSegments)
tap.Test.prototype.addAssert('assertMetricValues', 3, assertMetricValues)

/**
* @param {Metrics} metrics metrics under test
Expand All @@ -37,9 +38,9 @@ function assertMetrics(metrics, expected, exclusive, assertValues) {
// Assertions about arguments because maybe something returned undefined
// unexpectedly and is passed in, or a return type changed. This will
// hopefully help catch that and make it obvious.
this.ok(isSimpleObject(metrics), 'first argument required to be an Metrics object')
this.ok(Array.isArray(expected), 'second argument required to be an array of metrics')
this.ok(typeof exclusive === 'boolean', 'third argument required to be a boolean if provided')
assert.ok(isSimpleObject(metrics), 'first argument required to be an Metrics object')
assert.ok(Array.isArray(expected), 'second argument required to be an array of metrics')
assert.ok(typeof exclusive === 'boolean', 'third argument required to be a boolean if provided')

if (assertValues === undefined) {
assertValues = true
Expand All @@ -48,15 +49,15 @@ function assertMetrics(metrics, expected, exclusive, assertValues) {
for (let i = 0, len = expected.length; i < len; i++) {
const expectedMetric = expected[i]
const metric = metrics.getMetric(expectedMetric[0].name, expectedMetric[0].scope)
this.ok(metric, `should find ${expectedMetric[0].name}`)
assert.ok(metric, `should find ${expectedMetric[0].name}`)
if (assertValues) {
this.same(metric.toJSON(), expectedMetric[1])
assert.deepEqual(metric.toJSON(), expectedMetric[1])
}
}

if (exclusive) {
const metricsList = metrics.toJSON()
this.equal(metricsList.length, expected.length)
assert.equal(metricsList.length, expected.length)
}
}

Expand Down Expand Up @@ -94,14 +95,14 @@ function assertMetricValues(transaction, expected, exact) {
}

const metric = metrics.getMetric(name, scope)
this.ok(metric, 'should have expected metric name')
assert.ok(metric, 'should have expected metric name')

this.strictSame(metric.toJSON(), expectedMetric[1], 'metric values should match')
assert.deepStrictEqual(metric.toJSON(), expectedMetric[1], 'metric values should match')
}

if (exact) {
const metricsJSON = metrics.toJSON()
this.equal(metricsJSON.length, expected.length, 'metrics length should match')
assert.equal(metricsJSON.length, expected.length, 'metrics length should match')
}
}

Expand Down
45 changes: 20 additions & 25 deletions test/unit/metrics-recorder/distributed-trace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

'use strict'

const tap = require('tap')
const test = require('node:test')
const helper = require('../../lib/agent_helper')
require('../../lib/metrics_helper')
const { assertMetrics } = require('../../lib/metrics_helper')
const recordDistributedTrace = require('../../../lib/metrics/recorders/distributed-trace')
const Transaction = require('../../../lib/transaction')

Expand All @@ -29,7 +29,8 @@ const record = (opts) => {
recordDistributedTrace(tx, opts.type, duration, exclusive)
}

function beforeEach(t) {
function beforeEach(ctx) {
ctx.nr = {}
const agent = helper.loadMockedAgent({
distributed_tracing: {
enabled: true
Expand All @@ -41,22 +42,20 @@ function beforeEach(t) {
;(agent.config.account_id = '1234'),
(agent.config.primary_application_id = '5678'),
(agent.config.trusted_account_key = '1234')
t.context.tx = new Transaction(agent)
t.context.agent = agent
ctx.nr.tx = new Transaction(agent)
ctx.nr.agent = agent
}

function afterEach(t) {
helper.unloadAgent(t.context.agent)
function afterEach(ctx) {
helper.unloadAgent(ctx.nr.agent)
}

tap.test('recordDistributedTrace', (t) => {
t.autoend()
t.test('when a trace payload was received', (t) => {
t.autoend()
test('recordDistributedTrace', async (t) => {
await t.test('when a trace payload was received', async (t) => {
t.beforeEach(beforeEach)
t.afterEach(afterEach)
t.test('records metrics with payload information', (t) => {
const { tx } = t.context
await t.test('records metrics with payload information', (t) => {
const { tx } = t.nr
const payload = tx._createDistributedTracePayload().text()
tx.isDistributedTrace = null
tx._acceptDistributedTracePayload(payload, 'HTTP')
Expand Down Expand Up @@ -87,12 +86,11 @@ tap.test('recordDistributedTrace', (t) => {
]
]

t.assertMetrics(tx.metrics, result, true, true)
t.end()
assertMetrics(tx.metrics, result, true, true)
})

t.test('and transaction errors exist includes error-related metrics', (t) => {
const { tx } = t.context
await t.test('and transaction errors exist includes error-related metrics', (t) => {
const { tx } = t.nr
const payload = tx._createDistributedTracePayload().text()
tx.isDistributedTrace = null
tx._acceptDistributedTracePayload(payload, 'HTTP')
Expand Down Expand Up @@ -133,17 +131,15 @@ tap.test('recordDistributedTrace', (t) => {
]
]

t.assertMetrics(tx.metrics, result, true, true)
t.end()
assertMetrics(tx.metrics, result, true, true)
})
})

t.test('when no trace payload was received', (t) => {
t.autoend()
await t.test('when no trace payload was received', async (t) => {
t.beforeEach(beforeEach)
t.afterEach(afterEach)
t.test('records metrics with Unknown payload information', (t) => {
const { tx } = t.context
await t.test('records metrics with Unknown payload information', (t) => {
const { tx } = t.nr
record({
tx,
duration: 55,
Expand All @@ -162,8 +158,7 @@ tap.test('recordDistributedTrace', (t) => {
]
]

t.assertMetrics(tx.metrics, result, true, true)
t.end()
assertMetrics(tx.metrics, result, true, true)
})
})
})
45 changes: 21 additions & 24 deletions test/unit/metrics-recorder/generic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

'use strict'
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const helper = require('../../lib/agent_helper')
const recordGeneric = require('../../../lib/metrics/recorders/generic')
const Transaction = require('../../../lib/transaction')
Expand All @@ -29,33 +30,32 @@ function record(options) {
recordGeneric(segment, options.transaction.name)
}

tap.test('recordGeneric', function (t) {
t.autoend()
t.beforeEach((t) => {
test('recordGeneric', async function (t) {
t.beforeEach((ctx) => {
ctx.nr = {}
const agent = helper.loadMockedAgent()
t.context.trans = new Transaction(agent)
t.context.agent = agent
ctx.nr.trans = new Transaction(agent)
ctx.nr.agent = agent
})

t.afterEach((t) => {
helper.unloadAgent(t.context.agent)
t.afterEach((ctx) => {
helper.unloadAgent(ctx.nr.agent)
})

t.test("when scoped is undefined it shouldn't crash on recording", function (t) {
const { trans } = t.context
await t.test("when scoped is undefined it shouldn't crash on recording", function (t) {
const { trans } = t.nr
const segment = makeSegment({
transaction: trans,
duration: 0,
exclusive: 0
})
t.doesNotThrow(function () {
assert.doesNotThrow(function () {
recordGeneric(segment, undefined)
})
t.end()
})

t.test('when scoped is undefined it should record no scoped metrics', function (t) {
const { trans } = t.context
await t.test('when scoped is undefined it should record no scoped metrics', function (t) {
const { trans } = t.nr
const segment = makeSegment({
transaction: trans,
duration: 5,
Expand All @@ -65,12 +65,11 @@ tap.test('recordGeneric', function (t) {

const result = [[{ name: 'placeholder' }, [1, 0.005, 0.005, 0.005, 0.005, 0.000025]]]

t.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
t.end()
assert.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
})

t.test('with scope should record scoped metrics', function (t) {
const { trans } = t.context
await t.test('with scope should record scoped metrics', function (t) {
const { trans } = t.nr
record({
transaction: trans,
url: '/test',
Expand All @@ -88,12 +87,11 @@ tap.test('recordGeneric', function (t) {
]
]

t.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
t.end()
assert.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
})

t.test('should report exclusive time correctly', function (t) {
const { trans } = t.context
await t.test('should report exclusive time correctly', function (t) {
const { trans } = t.nr
const root = trans.trace.root
const parent = root.add('Test/Parent', recordGeneric)
const child1 = parent.add('Test/Child/1', recordGeneric)
Expand All @@ -111,7 +109,6 @@ tap.test('recordGeneric', function (t) {
]

trans.end()
t.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
t.end()
assert.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
})
})
45 changes: 21 additions & 24 deletions test/unit/metrics-recorder/http-external.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
*/

'use strict'
const tap = require('tap')
const test = require('node:test')
const assert = require('node:assert')
const helper = require('../../lib/agent_helper')
const generateRecorder = require('../../../lib/metrics/recorders/http_external')
const Transaction = require('../../../lib/transaction')
Expand Down Expand Up @@ -33,35 +34,34 @@ function record(options) {
recordExternal(segment, options.transaction.name)
}

tap.test('recordExternal', function (t) {
t.autoend()
t.beforeEach(function (t) {
test('recordExternal', async function (t) {
t.beforeEach(function (ctx) {
ctx.nr = {}
const agent = helper.loadMockedAgent()
const trans = new Transaction(agent)
trans.type = Transaction.TYPES.BG
t.context.agent = agent
t.context.trans = trans
ctx.nr.agent = agent
ctx.nr.trans = trans
})

t.afterEach(function (t) {
helper.unloadAgent(t.context.agent)
t.afterEach(function (ctx) {
helper.unloadAgent(ctx.nr.agent)
})

t.test("when scoped is undefined it shouldn't crash on recording", function (t) {
const { trans } = t.context
await t.test("when scoped is undefined it shouldn't crash on recording", function (t) {
const { trans } = t.nr
const segment = makeSegment({
transaction: trans,
duration: 0,
exclusive: 0
})
t.doesNotThrow(function () {
assert.doesNotThrow(function () {
recordExternal(segment, undefined)
})
t.end()
})

t.test('when scoped is undefined it should record no scoped metrics', function (t) {
const { trans } = t.context
await t.test('when scoped is undefined it should record no scoped metrics', function (t) {
const { trans } = t.nr
const segment = makeSegment({
transaction: trans,
duration: 0,
Expand All @@ -76,12 +76,11 @@ tap.test('recordExternal', function (t) {
[{ name: 'External/all' }, [1, 0, 0, 0, 0, 0]]
]

t.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
t.end()
assert.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
})

t.test('with scope should record scoped metrics', function (t) {
const { trans } = t.context
await t.test('with scope should record scoped metrics', function (t) {
const { trans } = t.nr
trans.type = Transaction.TYPES.WEB
record({
transaction: trans,
Expand All @@ -103,12 +102,11 @@ tap.test('recordExternal', function (t) {
]
]

t.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
t.end()
assert.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
})

t.test('should report exclusive time correctly', function (t) {
const { trans } = t.context
await t.test('should report exclusive time correctly', function (t) {
const { trans } = t.nr
const root = trans.trace.root
const parent = root.add('/parent', recordExternal)
const child1 = parent.add('/child1', generateRecorder('api.twitter.com', 'https'))
Expand All @@ -131,7 +129,6 @@ tap.test('recordExternal', function (t) {
]

trans.end()
t.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
t.end()
assert.equal(JSON.stringify(trans.metrics), JSON.stringify(result))
})
})
Loading

0 comments on commit f694410

Please sign in to comment.