Skip to content

Commit 48a1c71

Browse files
committed
ensure metrics are recorded only once
1 parent 0a3c636 commit 48a1c71

File tree

4 files changed

+86
-2
lines changed

4 files changed

+86
-2
lines changed

lib/subscribers/record-supportability-metric.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ function recordSupportabilityMetric({
2626
let metric = agent.metrics.getOrCreateMetric(
2727
`${SUBSCRIBER_USED}/${moduleName}/${major}`
2828
)
29-
metric.incrementCallCount()
29+
if (metric.callCount === 0) {
30+
metric.incrementCallCount()
31+
}
3032

3133
metric = agent.metrics.getOrCreateMetric(
3234
`${SUBSCRIBER_USED}/${moduleName}`
3335
)
34-
metric.incrementCallCount()
36+
if (metric.callCount === 0) {
37+
metric.incrementCallCount()
38+
}
3539
}

test/lib/agent_helper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,19 @@ helper.makeGetRequest = (url, options, callback) => {
436436
*/
437437
helper.makeGetRequestAsync = util.promisify(helper.makeGetRequest)
438438

439+
helper.asyncHttpCall = function (url, options) {
440+
return new Promise((resolve, reject) => {
441+
helper.makeRequest(url, options || {}, callback)
442+
443+
function callback (error, incomingMessage, body) {
444+
if (error) {
445+
return reject(error)
446+
}
447+
resolve({ response: incomingMessage, body })
448+
}
449+
})
450+
}
451+
439452
helper.makeRequest = (url, options, callback) => {
440453
if (!options || typeof options === 'function') {
441454
callback = options

test/versioned/fastify/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@
6161
"errors.test.js",
6262
"new-state-tracking.test.js"
6363
]
64+
},
65+
66+
{
67+
"engines": { "node": ">=20" },
68+
"dependencies": {
69+
"fastify": ">=5.0.0"
70+
},
71+
"files": [ "usage-metric.test.js" ]
6472
}
6573
]
6674
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2026 New Relic Corporation. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
'use strict'
7+
8+
const test = require('node:test')
9+
const helper = require('../../lib/agent_helper')
10+
11+
test('only records one usage metric', async (t) => {
12+
t.plan(4)
13+
14+
const agent = helper.instrumentMockedAgent()
15+
const fastify = require('fastify')
16+
const pkg = require('fastify/package.json')
17+
const version = Number(pkg.version.split('.', 1))
18+
const keyBase = 'Supportability/Features/Instrumentation/SubscriberUsed/fastify'
19+
const server = fastify({ logger: false })
20+
21+
t.after(() => {
22+
helper.unloadAgent(agent)
23+
server.close()
24+
})
25+
26+
server.decorate('test', {
27+
getter() {
28+
return this._test
29+
},
30+
setter(value) {
31+
this._test = value
32+
}
33+
})
34+
35+
server.addHook('preHandler', function(req, res, done) {
36+
this.test = true
37+
done()
38+
})
39+
40+
server.route({
41+
path: '/',
42+
method: 'get',
43+
handler (req, res) {
44+
t.assert.equal(this.test, true)
45+
res.send('ok')
46+
}
47+
})
48+
49+
const address = await server.listen({ port: 0 })
50+
51+
agent.on('transactionFinished', () => {
52+
const metrics = agent.metrics._metrics.unscoped
53+
t.assert.equal(metrics[keyBase].callCount, 1)
54+
t.assert.equal(metrics[`${keyBase}/${version}`].callCount, 1)
55+
})
56+
57+
const { body } = await helper.asyncHttpCall(address)
58+
t.assert.equal(body, 'ok')
59+
})

0 commit comments

Comments
 (0)