Skip to content

Commit edbaf16

Browse files
committed
update tests for metrics
1 parent a307c57 commit edbaf16

File tree

19 files changed

+108
-36
lines changed

19 files changed

+108
-36
lines changed

lib/shimmer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ const shimmer = (module.exports = {
306306
registerHooks(agent) {
307307
// add the packages from the subscriber based instrumentation
308308
// this is only added to add tracking metrics
309-
pkgsToHook.push(...Object.keys(subscriptions), ...trackingPkgs)
309+
Array.prototype.push.apply(pkgsToHook, trackingPkgs)
310310
this._ritm = new Hook(pkgsToHook, function onHook(exports, name, basedir) {
311311
return _postLoad(agent, exports, name, basedir)
312312
})

test/lib/custom-assertions/assert-pkg-tracking-metrics.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,18 @@
44
*/
55

66
'use strict'
7-
const NAMES = require('#agentlib/metrics/names.js')
8-
const assertMetrics = require('./assert-metrics')
7+
98
const semver = require('semver')
9+
const assertMetrics = require('./assert-metrics')
10+
11+
const {
12+
FEATURES: {
13+
INSTRUMENTATION: {
14+
SUBSCRIBER_USED,
15+
ON_REQUIRE
16+
}
17+
}
18+
} = require('#agentlib/metrics/names.js')
1019

1120
/**
1221
* assertion to verify tracking metrics for a given
@@ -16,19 +25,29 @@ const semver = require('semver')
1625
* @param {string} params.pkg name of package
1726
* @param {string} params.version version of package
1827
* @param {Agent} params.agent agent instance
28+
* @param {boolean} params.subscriberType When true, the metrics are expected
29+
* to have been generated from a subscriber based instrumentation. Otherwise,
30+
* the metrics are expected to be generated from a shimmer based
31+
* instrumentation.
1932
* @param {object} [deps] Injected dependencies.
2033
* @param {object} [deps.assert] Assertion library to use.
2134
*/
2235
module.exports = function assertPackageMetrics(
23-
{ pkg, version, agent },
36+
{ pkg, version, agent, subscriberType = false },
2437
{ assert = require('node:assert') } = {}
2538
) {
26-
const metrics = [
27-
[{ name: `${NAMES.FEATURES.INSTRUMENTATION.ON_REQUIRE}/${pkg}` }]
28-
]
39+
const metrics = []
40+
const prefix = subscriberType === true
41+
? `${SUBSCRIBER_USED}/${pkg}`
42+
: `${ON_REQUIRE}/${pkg}`
2943

44+
metrics.push([{ name: prefix }])
3045
if (version) {
31-
metrics.push([{ name: `${NAMES.FEATURES.INSTRUMENTATION.ON_REQUIRE}/${pkg}/Version/${semver.major(version)}` }])
46+
const major = semver.major(version)
47+
const suffix = subscriberType === true
48+
? `/${major}`
49+
: `/Version/${major}`
50+
metrics.push([{ name: `${prefix}${suffix}` }])
3251
}
3352

3453
assertMetrics(agent.metrics, metrics, false, false, { assert })

test/versioned/amqplib/callback.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ test('amqplib callback instrumentation', async function (t) {
6262

6363
await t.test('should log tracking metrics', function(t) {
6464
const { agent } = t.nr
65-
assertPackageMetrics({ agent, pkg: 'amqplib', version })
65+
assertPackageMetrics({ agent, pkg: 'amqplib', version, subscriberType: true })
6666
})
6767

6868
await t.test('connect in a transaction', function (t, end) {

test/versioned/amqplib/promises.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ test('amqplib promise instrumentation', async function (t) {
5555

5656
await t.test('should log tracking metrics', function(t) {
5757
const { agent } = t.nr
58-
assertPackageMetrics({ agent, pkg: 'amqplib', version })
58+
assertPackageMetrics({ agent, pkg: 'amqplib', version, subscriberType: true })
5959
})
6060

6161
await t.test('connect in a transaction', async function (t) {

test/versioned/bunyan/bunyan.test.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,10 @@ test('logging enabled/disabled', async (t) => {
5151

5252
await t.test('should log tracking metrics', function(t) {
5353
setup(t.nr, { application_logging: { enabled: true } })
54-
const { agent } = t.nr
54+
const { agent, bunyan } = t.nr
5555
const { version } = require('bunyan/package.json')
56-
assertPackageMetrics({ agent, pkg: 'bunyan', version })
56+
bunyan.createLogger({ name: 'test-logger' })
57+
assertPackageMetrics({ agent, pkg: 'bunyan', version, subscriberType: true })
5758
})
5859

5960
await t.test('logging enabled', (t) => {

test/versioned/elastic/elasticsearch.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ test('Elasticsearch instrumentation', async (t) => {
407407
expected['Datastore/instance/ElasticSearch/' + HOST_ID] = 5
408408
checkMetrics(unscoped, expected)
409409
const pkgName = '@elastic/elasticsearch'
410-
assertPackageMetrics({ agent, pkg: pkgName, version: pkgVersion })
410+
assertPackageMetrics({ agent, pkg: pkgName, version: pkgVersion, subscriberType: true })
411411
})
412412
})
413413

test/versioned/express/app-use.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ test('app should be at top of stack when mounted', async function (t) {
3939

4040
const express = require('express')
4141
const { version } = require('express/package.json')
42-
assertPackageMetrics({ agent, pkg: 'express', version })
4342
const main = express()
4443
const app = express()
4544
const app2 = express()
@@ -67,6 +66,7 @@ test('app should be at top of stack when mounted', async function (t) {
6766
// store finished transactions
6867
const finishedTransactions = {}
6968
agent.on('transactionFinished', function (tx) {
69+
assertPackageMetrics({ agent, pkg: 'express', version, subscriberType: true })
7070
finishedTransactions[tx.id] = tx
7171
})
7272

test/versioned/google-genai/chat-completions.test.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,21 @@ test.afterEach((ctx) => {
5353
removeModules('@google/genai')
5454
})
5555

56-
test('should log tracking metrics', function(t) {
57-
const { agent } = t.nr
58-
assertPackageMetrics({ agent, pkg: '@google/genai', version: pkgVersion })
56+
test('should log tracking metrics', function(t, end) {
57+
t.plan(5)
58+
const { agent, client } = t.nr
59+
helper.runInTransaction(agent, async () => {
60+
const model = 'gemini-2.0-flash'
61+
await client.models.generateContent({
62+
model,
63+
contents: 'You are a mathematician.'
64+
})
65+
assertPackageMetrics(
66+
{ agent, pkg: '@google/genai', version: pkgVersion, subscriberType: true },
67+
{ assert: t.assert }
68+
)
69+
end()
70+
})
5971
})
6072

6173
test('should create span on successful models generateContent', (t, end) => {

test/versioned/ioredis/ioredis.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ test('ioredis instrumentation', async (t) => {
6363
expected['Datastore/instance/Redis/' + HOST_ID] = 2
6464

6565
assertMetrics(tx.metrics, expected, false, false, { assert: plan })
66-
assertPackageMetrics({ agent, pkg: 'ioredis', version: pkgVersion })
66+
assertPackageMetrics({ agent, pkg: 'ioredis', version: pkgVersion, subscriberType: true })
6767
})
6868

6969
helper.runInTransaction(agent, async (transaction) => {

test/versioned/iovalkey/iovalkey.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@ test('iovalkey instrumentation', async (t) => {
6363
expected['Datastore/instance/Valkey/' + HOST_ID] = 2
6464

6565
assertMetrics(tx.metrics, expected, false, false, { assert: plan })
66-
assertPackageMetrics({ agent, pkg: 'iovalkey', version: pkgVersion })
66+
assertPackageMetrics({
67+
agent,
68+
pkg: 'iovalkey',
69+
version: pkgVersion,
70+
subscriberType: true
71+
})
6772
})
6873

6974
helper.runInTransaction(agent, async (transaction) => {

0 commit comments

Comments
 (0)