Skip to content

Commit 8647d43

Browse files
authored
chore: Clarified system metrics sampler naming (#2987)
1 parent 6bf1ccc commit 8647d43

File tree

4 files changed

+61
-58
lines changed

4 files changed

+61
-58
lines changed

lib/agent.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const MetricNormalizer = require('./metrics/normalizer')
2121
const MetricAggregator = require('./metrics/metric-aggregator')
2222
const NAMES = require('./metrics/names')
2323
const QueryTraceAggregator = require('./db/query-trace-aggregator')
24-
const sampler = require('./sampler')
24+
const systemMetricsSampler = require('./system-metrics-sampler')
2525
const TransactionTraceAggregator = require('./transaction/trace/aggregator')
2626
const TransactionEventAggregator = require('./transaction/transaction-event-aggregator')
2727
const Tracer = require('./transaction/tracer')
@@ -334,7 +334,7 @@ Agent.prototype.start = function start(callback) {
334334
return process.nextTick(callback)
335335
}
336336

337-
sampler.start(agent)
337+
systemMetricsSampler.start(agent)
338338

339339
if (this.config.serverless_mode.enabled) {
340340
return this._serverlessModeStart(callback)
@@ -349,7 +349,7 @@ Agent.prototype.start = function start(callback) {
349349
this.healthReporter.setStatus(HealthReporter.STATUS_LICENSE_KEY_MISSING)
350350

351351
this.setState('errored')
352-
sampler.stop()
352+
systemMetricsSampler.stop()
353353
return process.nextTick(function onNextTick() {
354354
agent.healthReporter.stop(() => {
355355
callback(new Error('Not starting without license key!'))
@@ -362,7 +362,7 @@ Agent.prototype.start = function start(callback) {
362362
if (error || response.shouldShutdownRun()) {
363363
agent.healthReporter.setStatus(HealthReporter.STATUS_CONNECT_ERROR)
364364
agent.setState('errored')
365-
sampler.stop()
365+
systemMetricsSampler.stop()
366366
callback(error || new Error('Failed to connect to collector'), response && response.payload)
367367
return
368368
}
@@ -482,7 +482,7 @@ Agent.prototype.stop = function stop(callback) {
482482

483483
this.harvester.stop()
484484

485-
sampler.stop()
485+
systemMetricsSampler.stop()
486486

487487
this.healthReporter.setStatus(HealthReporter.STATUS_AGENT_SHUTDOWN)
488488
this.healthReporter.stop(() => {

lib/sampler.js lib/system-metrics-sampler.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
'use strict'
77

8-
const NAMES = require('./metrics/names')
9-
const logger = require('./logger').child({ component: 'sampler' })
8+
const os = require('node:os')
9+
const logger = require('./logger').child({ component: 'system-metrics-sampler' })
1010
const Timer = require('./timer')
11-
const os = require('os')
11+
12+
const NAMES = require('./metrics/names')
1213

1314
/*
1415
*
@@ -22,12 +23,12 @@ const SAMPLE_INTERVAL = 15 * MILLIS
2223

2324
let samplers = []
2425

25-
function Sampler(sampler, interval) {
26+
function SystemMetricsSampler(sampler, interval) {
2627
this.id = setInterval(sampler, interval)
2728
this.id.unref()
2829
}
2930

30-
Sampler.prototype.stop = function stop() {
31+
SystemMetricsSampler.prototype.stop = function stop() {
3132
clearInterval(this.id)
3233
}
3334

@@ -148,8 +149,8 @@ module.exports = {
148149
nativeMetrics: null,
149150

150151
start: function start(agent) {
151-
samplers.push(new Sampler(sampleMemory(agent), 5 * MILLIS))
152-
samplers.push(new Sampler(checkEvents(agent), SAMPLE_INTERVAL))
152+
samplers.push(new SystemMetricsSampler(sampleMemory(agent), 5 * MILLIS))
153+
samplers.push(new SystemMetricsSampler(checkEvents(agent), SAMPLE_INTERVAL))
153154

154155
// This requires a native module which may have failed to build.
155156
if (agent.config.plugins.native_metrics.enabled && !this.nativeMetrics) {
@@ -175,17 +176,17 @@ module.exports = {
175176

176177
// Add GC events if available.
177178
if (this.nativeMetrics.gcEnabled) {
178-
samplers.push(new Sampler(sampleGc(agent, this.nativeMetrics), SAMPLE_INTERVAL))
179+
samplers.push(new SystemMetricsSampler(sampleGc(agent, this.nativeMetrics), SAMPLE_INTERVAL))
179180
}
180181

181182
// Add loop metrics if available.
182183
if (this.nativeMetrics.loopEnabled) {
183-
samplers.push(new Sampler(sampleLoop(agent, this.nativeMetrics), SAMPLE_INTERVAL))
184+
samplers.push(new SystemMetricsSampler(sampleLoop(agent, this.nativeMetrics), SAMPLE_INTERVAL))
184185
}
185186
}
186187

187188
// Add CPU sampling using the built-in data (introduced in 6.1.0)
188-
samplers.push(new Sampler(sampleCpu(agent), SAMPLE_INTERVAL))
189+
samplers.push(new SystemMetricsSampler(sampleCpu(agent), SAMPLE_INTERVAL))
189190

190191
this.state = 'running'
191192
},

test/unit/agent/agent.test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const Collector = require('../../lib/test-collector')
1212

1313
const sinon = require('sinon')
1414
const helper = require('../../lib/agent_helper')
15-
const sampler = require('../../../lib/sampler')
15+
const systemMetricsSampler = require('#agentlib/system-metrics-sampler.js')
1616
const configurator = require('../../../lib/config')
1717
const Agent = require('../../../lib/agent')
1818
const Transaction = require('../../../lib/transaction')
@@ -590,10 +590,10 @@ test('when stopping', async (t) => {
590590

591591
await t.test('should stop sampler', (t) => {
592592
const { agent } = t.nr
593-
sampler.start(agent)
593+
systemMetricsSampler.start(agent)
594594
agent.collector.shutdown = () => {}
595595
agent.stop(() => {})
596-
assert.equal(sampler.state, 'stopped')
596+
assert.equal(systemMetricsSampler.state, 'stopped')
597597
})
598598

599599
await t.test('should stop health reporter', async (t) => {
@@ -609,7 +609,7 @@ test('when stopping', async (t) => {
609609
}
610610

611611
const { agent } = t.nr
612-
sampler.start(agent)
612+
systemMetricsSampler.start(agent)
613613
agent.collector.shutdown = () => {}
614614
agent.stop(() => {})
615615

@@ -618,7 +618,7 @@ test('when stopping', async (t) => {
618618

619619
await t.test('should change state to "stopping"', (t) => {
620620
const { agent } = t.nr
621-
sampler.start(agent)
621+
systemMetricsSampler.start(agent)
622622
agent.collector.shutdown = () => {}
623623
agent.stop(() => {})
624624
assert.equal(agent._state, 'stopping')

test/unit/sampler.test.js test/unit/system-metrics-sampler.test.js

+40-38
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
*/
55

66
'use strict'
7+
78
const assert = require('node:assert')
89
const test = require('node:test')
9-
const Agent = require('../../lib/agent')
10-
const configurator = require('../../lib/config')
11-
const sampler = require('../../lib/sampler')
1210
const sinon = require('sinon')
13-
const numCpus = require('os').cpus().length
14-
const NAMES = require('../../lib/metrics/names')
11+
const Agent = require('#agentlib/agent.js')
12+
const configurator = require('#agentlib/config/index.js')
13+
const systemMetricsSampler = require('#agentlib/system-metrics-sampler.js')
14+
15+
const numCpus = require('node:os').cpus().length
16+
const NAMES = require('#agentlib/metrics/names.js')
1517

1618
test('environmental sampler', async function (t) {
1719
t.beforeEach(function (ctx) {
@@ -28,7 +30,7 @@ test('environmental sampler', async function (t) {
2830
})
2931

3032
t.afterEach(function (ctx) {
31-
sampler.stop()
33+
systemMetricsSampler.stop()
3234
ctx.nr.sandbox.restore()
3335
})
3436

@@ -40,17 +42,17 @@ test('environmental sampler', async function (t) {
4042

4143
await t.test('should still gather native metrics when bound and unbound', function (t, end) {
4244
const { agent } = t.nr
43-
sampler.start(agent)
44-
sampler.stop()
45-
sampler.start(agent)
45+
systemMetricsSampler.start(agent)
46+
systemMetricsSampler.stop()
47+
systemMetricsSampler.start(agent)
4648

4749
// Clear up the current state of the metrics.
48-
sampler.nativeMetrics.getGCMetrics()
49-
sampler.nativeMetrics.getLoopMetrics()
50+
systemMetricsSampler.nativeMetrics.getGCMetrics()
51+
systemMetricsSampler.nativeMetrics.getLoopMetrics()
5052

5153
spinLoop(function runLoop() {
52-
sampler.sampleLoop(agent, sampler.nativeMetrics)()
53-
sampler.sampleGc(agent, sampler.nativeMetrics)()
54+
systemMetricsSampler.sampleLoop(agent, systemMetricsSampler.nativeMetrics)()
55+
systemMetricsSampler.sampleGc(agent, systemMetricsSampler.nativeMetrics)()
5456

5557
const loop = agent.metrics.getOrCreateMetric(NAMES.LOOP.USAGE)
5658
assert.ok(loop.callCount > 1)
@@ -81,10 +83,10 @@ test('environmental sampler', async function (t) {
8183

8284
await t.test('should gather loop metrics', function (t, end) {
8385
const { agent } = t.nr
84-
sampler.start(agent)
85-
sampler.nativeMetrics.getLoopMetrics()
86+
systemMetricsSampler.start(agent)
87+
systemMetricsSampler.nativeMetrics.getLoopMetrics()
8688
spinLoop(function runLoop() {
87-
sampler.sampleLoop(agent, sampler.nativeMetrics)()
89+
systemMetricsSampler.sampleLoop(agent, systemMetricsSampler.nativeMetrics)()
8890

8991
const stats = agent.metrics.getOrCreateMetric(NAMES.LOOP.USAGE)
9092
assert.ok(stats.callCount > 1)
@@ -98,34 +100,34 @@ test('environmental sampler', async function (t) {
98100
await t.test('should depend on Agent to provide the current metrics summary', function (t) {
99101
const { agent } = t.nr
100102
assert.doesNotThrow(function () {
101-
sampler.start(agent)
103+
systemMetricsSampler.start(agent)
102104
})
103105
assert.doesNotThrow(function () {
104-
sampler.stop(agent)
106+
systemMetricsSampler.stop(agent)
105107
})
106108
})
107109

108110
await t.test('should default to a state of stopped', function () {
109-
assert.equal(sampler.state, 'stopped')
111+
assert.equal(systemMetricsSampler.state, 'stopped')
110112
})
111113

112114
await t.test('should say it is running after start', function (t) {
113115
const { agent } = t.nr
114-
sampler.start(agent)
115-
assert.equal(sampler.state, 'running')
116+
systemMetricsSampler.start(agent)
117+
assert.equal(systemMetricsSampler.state, 'running')
116118
})
117119

118120
await t.test('should say it is stopped after stop', function (t) {
119121
const { agent } = t.nr
120-
sampler.start(agent)
121-
assert.equal(sampler.state, 'running')
122-
sampler.stop(agent)
123-
assert.equal(sampler.state, 'stopped')
122+
systemMetricsSampler.start(agent)
123+
assert.equal(systemMetricsSampler.state, 'running')
124+
systemMetricsSampler.stop(agent)
125+
assert.equal(systemMetricsSampler.state, 'stopped')
124126
})
125127

126128
await t.test('should gather CPU user utilization metric', function (t) {
127129
const { agent } = t.nr
128-
sampler.sampleCpu(agent)()
130+
systemMetricsSampler.sampleCpu(agent)()
129131

130132
const stats = agent.metrics.getOrCreateMetric(NAMES.CPU.USER_UTILIZATION)
131133
assert.equal(stats.callCount, 1)
@@ -134,7 +136,7 @@ test('environmental sampler', async function (t) {
134136

135137
await t.test('should gather CPU system utilization metric', function (t) {
136138
const { agent } = t.nr
137-
sampler.sampleCpu(agent)()
139+
systemMetricsSampler.sampleCpu(agent)()
138140

139141
const stats = agent.metrics.getOrCreateMetric(NAMES.CPU.SYSTEM_UTILIZATION)
140142
assert.equal(stats.callCount, 1)
@@ -143,7 +145,7 @@ test('environmental sampler', async function (t) {
143145

144146
await t.test('should gather CPU user time metric', function (t) {
145147
const { agent } = t.nr
146-
sampler.sampleCpu(agent)()
148+
systemMetricsSampler.sampleCpu(agent)()
147149

148150
const stats = agent.metrics.getOrCreateMetric(NAMES.CPU.USER_TIME)
149151
assert.equal(stats.callCount, 1)
@@ -152,7 +154,7 @@ test('environmental sampler', async function (t) {
152154

153155
await t.test('should gather CPU sytem time metric', function (t) {
154156
const { agent } = t.nr
155-
sampler.sampleCpu(agent)()
157+
systemMetricsSampler.sampleCpu(agent)()
156158

157159
const stats = agent.metrics.getOrCreateMetric(NAMES.CPU.SYSTEM_TIME)
158160
assert.equal(stats.callCount, 1)
@@ -161,13 +163,13 @@ test('environmental sampler', async function (t) {
161163

162164
await t.test('should gather GC metrics', function (t, end) {
163165
const { agent } = t.nr
164-
sampler.start(agent)
166+
systemMetricsSampler.start(agent)
165167

166168
// Clear up the current state of the metrics.
167-
sampler.nativeMetrics.getGCMetrics()
169+
systemMetricsSampler.nativeMetrics.getGCMetrics()
168170

169171
spinLoop(function runLoop() {
170-
sampler.sampleGc(agent, sampler.nativeMetrics)()
172+
systemMetricsSampler.sampleGc(agent, systemMetricsSampler.nativeMetrics)()
171173

172174
// Find at least one typed GC metric.
173175
const type = [
@@ -197,23 +199,23 @@ test('environmental sampler', async function (t) {
197199
await t.test('should not gather GC metrics if disabled', function (t) {
198200
const { agent } = t.nr
199201
agent.config.plugins.native_metrics.enabled = false
200-
sampler.start(agent)
201-
assert.ok(!sampler.nativeMetrics)
202+
systemMetricsSampler.start(agent)
203+
assert.ok(!systemMetricsSampler.nativeMetrics)
202204
})
203205

204206
await t.test('should catch if process.cpuUsage throws an error', function (t) {
205207
const { agent } = t.nr
206208
const err = new Error('ohhhhhh boyyyyyy')
207209
process.cpuUsage.throws(err)
208-
sampler.sampleCpu(agent)()
210+
systemMetricsSampler.sampleCpu(agent)()
209211

210212
const stats = agent.metrics.getOrCreateMetric('CPU/User/Utilization')
211213
assert.equal(stats.callCount, 0)
212214
})
213215

214216
await t.test('should collect all specified memory statistics', function (t) {
215217
const { agent } = t.nr
216-
sampler.sampleMemory(agent)()
218+
systemMetricsSampler.sampleMemory(agent)()
217219

218220
Object.keys(NAMES.MEMORY).forEach(function testStat(memoryStat) {
219221
const metricName = NAMES.MEMORY[memoryStat]
@@ -228,15 +230,15 @@ test('environmental sampler', async function (t) {
228230
sandbox.stub(process, 'memoryUsage').callsFake(() => {
229231
throw new Error('your computer is on fire')
230232
})
231-
sampler.sampleMemory(agent)()
233+
systemMetricsSampler.sampleMemory(agent)()
232234

233235
const stats = agent.metrics.getOrCreateMetric('Memory/Physical')
234236
assert.equal(stats.callCount, 0)
235237
})
236238

237239
await t.test('should have some rough idea of how deep the event queue is', function (t, end) {
238240
const { agent } = t.nr
239-
sampler.checkEvents(agent)()
241+
systemMetricsSampler.checkEvents(agent)()
240242

241243
/* sampler.checkEvents works by creating a timer and using
242244
* setTimeout to schedule an "immediate" callback execution,

0 commit comments

Comments
 (0)