4
4
*/
5
5
6
6
'use strict'
7
+
7
8
const assert = require ( 'node:assert' )
8
9
const test = require ( 'node:test' )
9
- const Agent = require ( '../../lib/agent' )
10
- const configurator = require ( '../../lib/config' )
11
- const sampler = require ( '../../lib/sampler' )
12
10
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' )
15
17
16
18
test ( 'environmental sampler' , async function ( t ) {
17
19
t . beforeEach ( function ( ctx ) {
@@ -28,7 +30,7 @@ test('environmental sampler', async function (t) {
28
30
} )
29
31
30
32
t . afterEach ( function ( ctx ) {
31
- sampler . stop ( )
33
+ systemMetricsSampler . stop ( )
32
34
ctx . nr . sandbox . restore ( )
33
35
} )
34
36
@@ -40,17 +42,17 @@ test('environmental sampler', async function (t) {
40
42
41
43
await t . test ( 'should still gather native metrics when bound and unbound' , function ( t , end ) {
42
44
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 )
46
48
47
49
// Clear up the current state of the metrics.
48
- sampler . nativeMetrics . getGCMetrics ( )
49
- sampler . nativeMetrics . getLoopMetrics ( )
50
+ systemMetricsSampler . nativeMetrics . getGCMetrics ( )
51
+ systemMetricsSampler . nativeMetrics . getLoopMetrics ( )
50
52
51
53
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 ) ( )
54
56
55
57
const loop = agent . metrics . getOrCreateMetric ( NAMES . LOOP . USAGE )
56
58
assert . ok ( loop . callCount > 1 )
@@ -81,10 +83,10 @@ test('environmental sampler', async function (t) {
81
83
82
84
await t . test ( 'should gather loop metrics' , function ( t , end ) {
83
85
const { agent } = t . nr
84
- sampler . start ( agent )
85
- sampler . nativeMetrics . getLoopMetrics ( )
86
+ systemMetricsSampler . start ( agent )
87
+ systemMetricsSampler . nativeMetrics . getLoopMetrics ( )
86
88
spinLoop ( function runLoop ( ) {
87
- sampler . sampleLoop ( agent , sampler . nativeMetrics ) ( )
89
+ systemMetricsSampler . sampleLoop ( agent , systemMetricsSampler . nativeMetrics ) ( )
88
90
89
91
const stats = agent . metrics . getOrCreateMetric ( NAMES . LOOP . USAGE )
90
92
assert . ok ( stats . callCount > 1 )
@@ -98,34 +100,34 @@ test('environmental sampler', async function (t) {
98
100
await t . test ( 'should depend on Agent to provide the current metrics summary' , function ( t ) {
99
101
const { agent } = t . nr
100
102
assert . doesNotThrow ( function ( ) {
101
- sampler . start ( agent )
103
+ systemMetricsSampler . start ( agent )
102
104
} )
103
105
assert . doesNotThrow ( function ( ) {
104
- sampler . stop ( agent )
106
+ systemMetricsSampler . stop ( agent )
105
107
} )
106
108
} )
107
109
108
110
await t . test ( 'should default to a state of stopped' , function ( ) {
109
- assert . equal ( sampler . state , 'stopped' )
111
+ assert . equal ( systemMetricsSampler . state , 'stopped' )
110
112
} )
111
113
112
114
await t . test ( 'should say it is running after start' , function ( t ) {
113
115
const { agent } = t . nr
114
- sampler . start ( agent )
115
- assert . equal ( sampler . state , 'running' )
116
+ systemMetricsSampler . start ( agent )
117
+ assert . equal ( systemMetricsSampler . state , 'running' )
116
118
} )
117
119
118
120
await t . test ( 'should say it is stopped after stop' , function ( t ) {
119
121
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' )
124
126
} )
125
127
126
128
await t . test ( 'should gather CPU user utilization metric' , function ( t ) {
127
129
const { agent } = t . nr
128
- sampler . sampleCpu ( agent ) ( )
130
+ systemMetricsSampler . sampleCpu ( agent ) ( )
129
131
130
132
const stats = agent . metrics . getOrCreateMetric ( NAMES . CPU . USER_UTILIZATION )
131
133
assert . equal ( stats . callCount , 1 )
@@ -134,7 +136,7 @@ test('environmental sampler', async function (t) {
134
136
135
137
await t . test ( 'should gather CPU system utilization metric' , function ( t ) {
136
138
const { agent } = t . nr
137
- sampler . sampleCpu ( agent ) ( )
139
+ systemMetricsSampler . sampleCpu ( agent ) ( )
138
140
139
141
const stats = agent . metrics . getOrCreateMetric ( NAMES . CPU . SYSTEM_UTILIZATION )
140
142
assert . equal ( stats . callCount , 1 )
@@ -143,7 +145,7 @@ test('environmental sampler', async function (t) {
143
145
144
146
await t . test ( 'should gather CPU user time metric' , function ( t ) {
145
147
const { agent } = t . nr
146
- sampler . sampleCpu ( agent ) ( )
148
+ systemMetricsSampler . sampleCpu ( agent ) ( )
147
149
148
150
const stats = agent . metrics . getOrCreateMetric ( NAMES . CPU . USER_TIME )
149
151
assert . equal ( stats . callCount , 1 )
@@ -152,7 +154,7 @@ test('environmental sampler', async function (t) {
152
154
153
155
await t . test ( 'should gather CPU sytem time metric' , function ( t ) {
154
156
const { agent } = t . nr
155
- sampler . sampleCpu ( agent ) ( )
157
+ systemMetricsSampler . sampleCpu ( agent ) ( )
156
158
157
159
const stats = agent . metrics . getOrCreateMetric ( NAMES . CPU . SYSTEM_TIME )
158
160
assert . equal ( stats . callCount , 1 )
@@ -161,13 +163,13 @@ test('environmental sampler', async function (t) {
161
163
162
164
await t . test ( 'should gather GC metrics' , function ( t , end ) {
163
165
const { agent } = t . nr
164
- sampler . start ( agent )
166
+ systemMetricsSampler . start ( agent )
165
167
166
168
// Clear up the current state of the metrics.
167
- sampler . nativeMetrics . getGCMetrics ( )
169
+ systemMetricsSampler . nativeMetrics . getGCMetrics ( )
168
170
169
171
spinLoop ( function runLoop ( ) {
170
- sampler . sampleGc ( agent , sampler . nativeMetrics ) ( )
172
+ systemMetricsSampler . sampleGc ( agent , systemMetricsSampler . nativeMetrics ) ( )
171
173
172
174
// Find at least one typed GC metric.
173
175
const type = [
@@ -197,23 +199,23 @@ test('environmental sampler', async function (t) {
197
199
await t . test ( 'should not gather GC metrics if disabled' , function ( t ) {
198
200
const { agent } = t . nr
199
201
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 )
202
204
} )
203
205
204
206
await t . test ( 'should catch if process.cpuUsage throws an error' , function ( t ) {
205
207
const { agent } = t . nr
206
208
const err = new Error ( 'ohhhhhh boyyyyyy' )
207
209
process . cpuUsage . throws ( err )
208
- sampler . sampleCpu ( agent ) ( )
210
+ systemMetricsSampler . sampleCpu ( agent ) ( )
209
211
210
212
const stats = agent . metrics . getOrCreateMetric ( 'CPU/User/Utilization' )
211
213
assert . equal ( stats . callCount , 0 )
212
214
} )
213
215
214
216
await t . test ( 'should collect all specified memory statistics' , function ( t ) {
215
217
const { agent } = t . nr
216
- sampler . sampleMemory ( agent ) ( )
218
+ systemMetricsSampler . sampleMemory ( agent ) ( )
217
219
218
220
Object . keys ( NAMES . MEMORY ) . forEach ( function testStat ( memoryStat ) {
219
221
const metricName = NAMES . MEMORY [ memoryStat ]
@@ -228,15 +230,15 @@ test('environmental sampler', async function (t) {
228
230
sandbox . stub ( process , 'memoryUsage' ) . callsFake ( ( ) => {
229
231
throw new Error ( 'your computer is on fire' )
230
232
} )
231
- sampler . sampleMemory ( agent ) ( )
233
+ systemMetricsSampler . sampleMemory ( agent ) ( )
232
234
233
235
const stats = agent . metrics . getOrCreateMetric ( 'Memory/Physical' )
234
236
assert . equal ( stats . callCount , 0 )
235
237
} )
236
238
237
239
await t . test ( 'should have some rough idea of how deep the event queue is' , function ( t , end ) {
238
240
const { agent } = t . nr
239
- sampler . checkEvents ( agent ) ( )
241
+ systemMetricsSampler . checkEvents ( agent ) ( )
240
242
241
243
/* sampler.checkEvents works by creating a timer and using
242
244
* setTimeout to schedule an "immediate" callback execution,
0 commit comments