Skip to content

Commit 188229c

Browse files
committed
fix(apm): add typecheck
1 parent 7952dd8 commit 188229c

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

lib/agent/metrics/apm/index.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,19 @@ ApmMetrics.prototype._nanoToMilli = function (value) {
113113

114114
ApmMetrics.prototype.getEventLoop = function () {
115115
var stats = eventLoopStats.sense()
116+
117+
var formattedStats = stats && {
118+
max: stats.max,
119+
maxUnit: 'ms',
120+
min: stats.min,
121+
minUnit: 'ms',
122+
sum: stats.sum,
123+
sumUnit: 'ms',
124+
num: stats.num,
125+
numUnit: 'pcs'
126+
}
116127
return {
117-
stats: {
118-
max: stats.max,
119-
maxUnit: 'ms',
120-
min: stats.min,
121-
minUnit: 'ms',
122-
sum: stats.sum,
123-
sumUnit: 'ms',
124-
num: stats.num,
125-
numUnit: 'pcs'
126-
},
128+
stats: formattedStats,
127129
requests: process._getActiveRequests().length,
128130
requestsUnit: 'pcs',
129131
handlers: process._getActiveHandles().length,

lib/agent/metrics/apm/index.spec.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,83 @@ describe('The ApmMetrics module', function () {
103103
})
104104
})
105105

106+
it('does not fail when eventLoopStats is not loaded', function () {
107+
var activeRequests = 3
108+
var activeHandlers = 5
109+
var ISOString = 'date-string'
110+
var loadAvg = 3
111+
var memoryUsed = 24000000
112+
var memoryTotal = 12000000
113+
var memoryRss = 15000000
114+
115+
var collectorApi = {
116+
sendApmMetrics: this.sandbox.spy()
117+
}
118+
119+
var apmMetrics = ApmMetrics.create({
120+
collectorApi: collectorApi,
121+
config: {
122+
collectInterval: 1
123+
}
124+
})
125+
apmMetrics.lagId = 1
126+
127+
apmMetrics.gc = {
128+
time: 6700000,
129+
scavenge: 3,
130+
marksweep: 1
131+
}
132+
133+
this.sandbox.stub(eventLoopStats, 'sense')
134+
this.sandbox.stub(process, '_getActiveRequests', function () {
135+
return new Array(activeRequests)
136+
})
137+
this.sandbox.stub(process, '_getActiveHandles', function () {
138+
return new Array(activeHandlers)
139+
})
140+
this.sandbox.stub(process, 'memoryUsage', function () {
141+
return {
142+
rss: memoryRss,
143+
heapTotal: memoryTotal,
144+
heapUsed: memoryUsed
145+
}
146+
})
147+
this.sandbox.stub(Date.prototype, 'toISOString', function () {
148+
return ISOString
149+
})
150+
this.sandbox.stub(os, 'loadavg', function () {
151+
return [loadAvg]
152+
})
153+
154+
this.sandbox.stub(apmMetrics, 'cpuCount', 2)
155+
156+
apmMetrics.sendMetrics()
157+
158+
expect(collectorApi.sendApmMetrics).to.be.calledWith({
159+
memory: {
160+
used: 22,
161+
usedUnit: 'MB',
162+
total: 11,
163+
totalUnit: 'MB',
164+
rss: 14,
165+
rssUnit: 'MB'
166+
},
167+
cpu: {
168+
utilization: 150,
169+
utilizationUnit: 'pct'
170+
},
171+
gc: {
172+
time: 6,
173+
timeUnit: 'ms',
174+
scavenge: 3,
175+
scavengeUnit: 'pcs',
176+
marksweep: 1,
177+
marksweepUnit: 'pcs'
178+
},
179+
timestamp: ISOString
180+
})
181+
})
182+
106183
it('skips utilization if running in VM', function () {
107184
var collectorApi = {
108185
sendApmMetrics: this.sandbox.spy()

0 commit comments

Comments
 (0)