Skip to content

Commit 9b78622

Browse files
authored
fix: crashing error when active:false and agent.registerMetrics is called (#2290)
* fix: crashing error when active:false and agent.registerMetrics is called Fixes: #1799
1 parent 1207b49 commit 9b78622

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Notes:
4343
* Ensure the internal HTTP(S) client requests made by the APM agent to APM
4444
server are not themselves traced. ({issues}1168[#1168], {issues}1136[#1136])
4545
46+
* Fix crashing error with `agent.registerMetric` and `active:false` configuration. ({issues}1799[#1799], {pull}2290[#2290])
4647
4748
[[release-notes-3.20.0]]
4849
==== 3.20.0 2021/08/12

lib/metrics/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ class Metrics {
4242
}
4343

4444
getOrCreateCounter (...args) {
45+
if (!this[registrySymbol]) {
46+
return
47+
}
4548
return this[registrySymbol].getOrCreateCounter(...args)
4649
}
4750

@@ -54,13 +57,19 @@ class Metrics {
5457
}
5558

5659
getOrCreateGauge (...args) {
60+
if (!this[registrySymbol]) {
61+
return
62+
}
5763
return this[registrySymbol].getOrCreateGauge(...args)
5864
}
5965

6066
// factory function for creating a queue metrics collector
6167
//
6268
// called from instrumentation, only when the agent receives a queue message
6369
createQueueMetricsCollector (queueOrTopicName) {
70+
if (!this[registrySymbol]) {
71+
return
72+
}
6473
const collector = createQueueMetrics(queueOrTopicName, this[registrySymbol])
6574
return collector
6675
}

test/metrics/index.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,18 @@ test('noop counter when not active', function (t) {
211211
t.end()
212212
})
213213

214+
test('Metrics objects do not throw/crash when not started', function (t) {
215+
const metrics = new Metrics()
216+
217+
t.equals(metrics.stop(), undefined, 'unset registrySymbol does not crash metrics object')
218+
t.equals(metrics.getOrCreateCounter('foo', {}, null), undefined, 'unset registrySymbol does not crash metrics object')
219+
t.equals(metrics.incrementCounter('foo'), undefined, 'unset registrySymbol does not crash metrics object')
220+
t.equals(metrics.getOrCreateGauge('foo', function () {}), undefined, 'unset registrySymbol does not crash metrics object')
221+
t.equals(metrics.createQueueMetricsCollector('foo'), undefined, 'unset registrySymbol does not crash metrics object')
222+
223+
t.end()
224+
})
225+
214226
function spinCPUFor (durationMs) {
215227
const start = Date.now()
216228
while (Date.now() - start < durationMs) {}

0 commit comments

Comments
 (0)