Skip to content

Commit

Permalink
refactor: metrics_helper to use assert
Browse files Browse the repository at this point in the history
  • Loading branch information
amychisholm03 committed Sep 10, 2024
1 parent f694410 commit f82ece5
Show file tree
Hide file tree
Showing 21 changed files with 101 additions and 108 deletions.
25 changes: 12 additions & 13 deletions test/lib/metrics_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

'use strict'

const tap = require('tap')
const assert = require('node:assert')
const urltils = require('../../lib/util/urltils')
const { isSimpleObject } = require('../../lib/util/objects')
Expand All @@ -14,7 +13,7 @@ exports.findSegment = findSegment
exports.getMetricHostName = getMetricHostName
exports.assertMetrics = assertMetrics
exports.assertMetricValues = assertMetricValues
tap.Test.prototype.addAssert('assertSegments', 3, assertSegments)
exports.assertSegments = assertSegments

/**
* @param {Metrics} metrics metrics under test
Expand Down Expand Up @@ -148,33 +147,33 @@ function assertSegments(parent, expected, options) {

if (typeof sequenceItem === 'string') {
child = children[childCount++]
this.equal(
assert.equal(
child ? child.name : undefined,
sequenceItem,
'segment "' +
parent.name +
'" should have child "' +
sequenceItem +
'" in position ' +
childCount
parent.name +

Check failure on line 154 in test/lib/metrics_helper.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

Insert `··`
'" should have child "' +

Check failure on line 155 in test/lib/metrics_helper.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

Insert `··`
sequenceItem +

Check failure on line 156 in test/lib/metrics_helper.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

Replace `··········` with `············`
'" in position ' +

Check failure on line 157 in test/lib/metrics_helper.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

Insert `··`
childCount

Check failure on line 158 in test/lib/metrics_helper.js

View workflow job for this annotation

GitHub Actions / lint (lts/*)

Insert `··`
)

// If the next expected item is not array, then check that the current
// child has no children
if (!Array.isArray(expected[i + 1])) {
// var children = child.children
this.ok(
assert.ok(
getChildren(child).length === 0,
'segment "' + child.name + '" should not have any children'
)
}
} else if (typeof sequenceItem === 'object') {
this.assertSegments(child, sequenceItem, options)
assertSegments(child, sequenceItem, options)
}
}

// check if correct number of children was found
this.equal(children.length, childCount)
assert.equal(children.length, childCount)
} else {
for (let i = 0; i < expected.length; i++) {
const sequenceItem = expected[i]
Expand All @@ -186,9 +185,9 @@ function assertSegments(parent, expected, options) {
child = parent.children[j]
}
}
this.ok(child, 'segment "' + parent.name + '" should have child "' + sequenceItem + '"')
assert.ok(child, 'segment "' + parent.name + '" should have child "' + sequenceItem + '"')
if (typeof expected[i + 1] === 'object') {
this.assertSegments(child, expected[i + 1], exact)
assertSegments(child, expected[i + 1], exact)
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions test/versioned/aws-sdk-v3/bedrock-chat-completions.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const tap = require('tap')
require('./common')
const helper = require('../../lib/agent_helper')
require('../../lib/metrics_helper')
const { assertSegments } = require('../../lib/metrics_helper')
const createAiResponseServer = require('../../lib/aws-server-stubs/ai-server')
const { FAKE_CREDENTIALS } = require('../../lib/aws-server-stubs')
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
Expand Down Expand Up @@ -114,7 +114,7 @@ tap.afterEach(async (t) => {
const body = JSON.parse(response.body.transformToString('utf8'))
t.equal(response.$metadata.requestId, expected.headers['x-amzn-requestid'])
t.same(body, expected.body)
t.assertSegments(
assertSegments(
tx.trace.root,
['Llm/completion/Bedrock/InvokeModelCommand', [expectedExternalPath(modelId)]],
{ exact: false }
Expand Down Expand Up @@ -316,7 +316,7 @@ tap.afterEach(async (t) => {
}
})

t.assertSegments(
assertSegments(
tx.trace.root,
['Llm/completion/Bedrock/InvokeModelCommand', [expectedExternalPath(modelId)]],
{ exact: false }
Expand Down Expand Up @@ -457,7 +457,7 @@ tap.test(`ai21: should properly create errors on create completion (streamed)`,
}
})

t.assertSegments(
assertSegments(
tx.trace.root,
[
'Llm/completion/Bedrock/InvokeModelWithResponseStreamCommand',
Expand Down Expand Up @@ -521,7 +521,7 @@ tap.test(`models that do not support streaming should be handled`, (t) => {
}
})

t.assertSegments(
assertSegments(
tx.trace.root,
[
'Llm/embedding/Bedrock/InvokeModelWithResponseStreamCommand',
Expand Down
6 changes: 3 additions & 3 deletions test/versioned/aws-sdk-v3/bedrock-embeddings.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const tap = require('tap')
require('./common')
const helper = require('../../lib/agent_helper')
require('../../lib/metrics_helper')
const { assertSegments } = require('../../lib/metrics_helper')
const createAiResponseServer = require('../../lib/aws-server-stubs/ai-server')
const { FAKE_CREDENTIALS } = require('../../lib/aws-server-stubs')
const { DESTINATIONS } = require('../../../lib/config/attribute-filter')
Expand Down Expand Up @@ -77,7 +77,7 @@ tap.afterEach(async (t) => {
const body = JSON.parse(response.body.transformToString('utf8'))
t.equal(response.$metadata.requestId, expected.headers['x-amzn-requestid'])
t.same(body, expected.body)
t.assertSegments(
assertSegments(
tx.trace.root,
['Llm/embedding/Bedrock/InvokeModelCommand', [expectedExternalPath(modelId)]],
{ exact: false }
Expand Down Expand Up @@ -168,7 +168,7 @@ tap.afterEach(async (t) => {
}
})

t.assertSegments(
assertSegments(
tx.trace.root,
['Llm/embedding/Bedrock/InvokeModelCommand', [expectedExternalPath(modelId)]],
{ exact: false }
Expand Down
1 change: 0 additions & 1 deletion test/versioned/aws-sdk-v3/bedrock-negative-tests.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
const tap = require('tap')
require('./common')
const helper = require('../../lib/agent_helper')
require('../../lib/metrics_helper')
const createAiResponseServer = require('../../lib/aws-server-stubs/ai-server')
const { FAKE_CREDENTIALS } = require('../../lib/aws-server-stubs')
const sinon = require('sinon')
Expand Down
4 changes: 2 additions & 2 deletions test/versioned/express/client-disconnect.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
require('../../lib/metrics_helper')
const { assertSegments } = require('../../lib/metrics_helper')
const http = require('http')

function generateApp(t) {
Expand Down Expand Up @@ -46,7 +46,7 @@ tap.test('Client Premature Disconnection', (t) => {
})

agent.on('transactionFinished', (transaction) => {
t.assertSegments(
assertSegments(
transaction.trace.root,
[
'WebTransaction/Expressjs/POST//test',
Expand Down
6 changes: 3 additions & 3 deletions test/versioned/fastify/add-hook.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
require('../../lib/metrics_helper')
const { assertSegments } = require('../../lib/metrics_helper')
const common = require('./common')

// all of these events fire before the route handler
Expand Down Expand Up @@ -105,7 +105,7 @@ tap.test('fastify hook instrumentation', (t) => {
]
]
}
t.assertSegments(transaction.trace.root, expectedSegments)
assertSegments(transaction.trace.root, expectedSegments)
})

await fastify.listen(0)
Expand Down Expand Up @@ -161,7 +161,7 @@ tap.test('fastify hook instrumentation', (t) => {
]
}

t.assertSegments(transaction.trace.root, expectedSegments)
assertSegments(transaction.trace.root, expectedSegments)
})

await fastify.listen(0)
Expand Down
4 changes: 2 additions & 2 deletions test/versioned/fastify/naming-common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

'use strict'
const { routesToTest, makeRequest } = require('./common')
require('../../lib/metrics_helper')
const { assertSegments } = require('../../lib/metrics_helper')
const helper = require('../../lib/agent_helper')

module.exports = function createTests(t, getExpectedSegments) {
Expand Down Expand Up @@ -36,7 +36,7 @@ module.exports = function createTests(t, getExpectedSegments) {
]
}

t.assertSegments(transaction.trace.root, expectedSegments)
assertSegments(transaction.trace.root, expectedSegments)
})

await fastify.listen(0)
Expand Down
12 changes: 6 additions & 6 deletions test/versioned/hapi/segments.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const tap = require('tap')
const helper = require('../../lib/agent_helper')
const http = require('http')
require('../../lib/metrics_helper')
const { assertSegments, assertMetrics } = require('../../lib/metrics_helper')
const NAMES = require('../../../lib/metrics/names')
const utils = require('./hapi-utils')

Expand Down Expand Up @@ -41,7 +41,7 @@ tap.test('Hapi segments', function (t) {

runTest(t, function (segments, transaction) {
checkMetrics(t, transaction.metrics, [NAMES.HAPI.MIDDLEWARE + 'myHandler//test'])
t.assertSegments(transaction.trace.root.children[0], [
assertSegments(transaction.trace.root.children[0], [
NAMES.HAPI.MIDDLEWARE + 'myHandler//test'
])
t.end()
Expand All @@ -63,7 +63,7 @@ tap.test('Hapi segments', function (t) {

runTest(t, function (segments, transaction) {
checkMetrics(t, transaction.metrics, [NAMES.HAPI.MIDDLEWARE + 'customHandler//test'])
t.assertSegments(transaction.trace.root.children[0], [
assertSegments(transaction.trace.root.children[0], [
NAMES.HAPI.MIDDLEWARE + 'customHandler//test'
])
t.end()
Expand All @@ -88,7 +88,7 @@ tap.test('Hapi segments', function (t) {
NAMES.HAPI.MIDDLEWARE + '<anonymous>//onRequest',
NAMES.HAPI.MIDDLEWARE + 'myHandler//test'
])
t.assertSegments(transaction.trace.root.children[0], [
assertSegments(transaction.trace.root.children[0], [
NAMES.HAPI.MIDDLEWARE + '<anonymous>//onRequest',
NAMES.HAPI.MIDDLEWARE + 'myHandler//test'
])
Expand Down Expand Up @@ -118,7 +118,7 @@ tap.test('Hapi segments', function (t) {
NAMES.HAPI.MIDDLEWARE + '<anonymous>//onRequest',
NAMES.HAPI.MIDDLEWARE + 'customHandler//test'
])
t.assertSegments(transaction.trace.root.children[0], [
assertSegments(transaction.trace.root.children[0], [
NAMES.HAPI.MIDDLEWARE + '<anonymous>//onRequest',
NAMES.HAPI.MIDDLEWARE + 'customHandler//test'
])
Expand Down Expand Up @@ -284,5 +284,5 @@ function checkMetrics(t, metrics, expected, path) {
expectedAll.push([{ name: metric, scope: 'WebTransaction/Hapi/GET/' + path }])
}

t.assertMetrics(metrics, expectedAll, true, false)
assertMetrics(metrics, expectedAll, true, false)
}
4 changes: 2 additions & 2 deletions test/versioned/ioredis/ioredis.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const tap = require('tap')
const helper = require('../../lib/agent_helper')
require('../../lib/metrics_helper')
const { assertMetrics } = require('../../lib/metrics_helper')
const params = require('../../lib/params')
const urltils = require('../../../lib/util/urltils')

Expand Down Expand Up @@ -57,7 +57,7 @@ tap.test('ioredis instrumentation', (t) => {
]
expected['Datastore/instance/Redis/' + HOST_ID] = 2

t.assertMetrics(tx.metrics, expected, false, false)
assertMetrics(tx.metrics, expected, false, false)
t.end()
})

Expand Down
8 changes: 4 additions & 4 deletions test/versioned/koa/koa.tap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
const tap = require('tap')
const http = require('http')
const helper = require('../../lib/agent_helper')
require('../../lib/metrics_helper')
const { assertSegments } = require('../../lib/metrics_helper')

tap.test('Koa instrumentation', (t) => {
t.autoend()
Expand Down Expand Up @@ -227,7 +227,7 @@ tap.test('Koa instrumentation', (t) => {
})

agent.on('transactionFinished', (tx) => {
t.assertSegments(tx.trace.root, [
assertSegments(tx.trace.root, [
'WebTransaction/WebFrameworkUri/Koa/GET//',
[
'Nodejs/Middleware/Koa/one',
Expand Down Expand Up @@ -283,7 +283,7 @@ tap.test('Koa instrumentation', (t) => {
})

agent.on('transactionFinished', function (txn) {
t.assertSegments(tx.trace.root, [
assertSegments(tx.trace.root, [
txn.name,
[
'Nodejs/Middleware/Koa/one',
Expand Down Expand Up @@ -399,7 +399,7 @@ tap.test('Koa instrumentation', (t) => {
})

function checkSegments(t, tx) {
t.assertSegments(tx.trace.root, [
assertSegments(tx.trace.root, [
// Until koa-router is instrumented and transaction naming is addressed,
// names will be inconsistent depending on whether there is an error.
tx.name,
Expand Down
Loading

0 comments on commit f82ece5

Please sign in to comment.