Skip to content

Commit 61f0b51

Browse files
committed
fix(test): Use manually started timers
1 parent d977835 commit 61f0b51

19 files changed

+294
-34
lines changed

lib/agent/control/control.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
var debug = require('debug')('risingstack/trace')
22

3+
var Timer = require('../timer')
4+
35
function Control (options) {
46
var _this = this
7+
this.name = 'Control'
8+
59
this.collectorApi = options.collectorApi
610
this.controlBus = options.controlBus
711
this.config = options.config
@@ -10,7 +14,7 @@ function Control (options) {
1014

1115
this.getUpdates()
1216

13-
this.interval = setInterval(function () {
17+
this.timer = new Timer(function () {
1418
_this.getUpdates()
1519
}, this.updateInterval)
1620
}

lib/agent/control/control.spec.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
var expect = require('chai').expect
22

33
var Control = require('./')
4+
var Timer = require('../timer')
45

56
describe('The Control module', function () {
6-
it('interprets commands', function () {
7+
it('gets memory-heapdump', function () {
78
var collectorApi = {
89
getUpdates: function (options, cb) {
910
cb(null, {
@@ -16,9 +17,7 @@ describe('The Control module', function () {
1617
}
1718

1819
var controlBus = {
19-
on: function () {
20-
21-
},
20+
on: function () {},
2221
emit: this.sandbox.spy()
2322
}
2423

@@ -32,4 +31,16 @@ describe('The Control module', function () {
3231
profiler.getUpdates()
3332
expect(controlBus.emit).to.be.calledWith('memory-heapdump', undefined)
3433
})
34+
it('should have a timer', function () {
35+
var collectorApi = {
36+
getUpdates: function () { }
37+
}
38+
var profiler = Control.create({
39+
collectorApi: collectorApi,
40+
config: {
41+
updateInterval: 1
42+
}
43+
})
44+
expect(profiler.timer).to.be.instanceof(Timer)
45+
})
3546
})

lib/agent/healthcheck/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
var Timer = require('../timer')
2+
13
function Healthcheck (options) {
4+
this.name = 'Healthcheck'
25
var _this = this
3-
46
this.collectorApi = options.collectorApi
57
this.config = options.config
68
this.healthcheckInterval = this.config.healthcheckInterval
7-
this.interval = setInterval(function () {
9+
10+
this.timer = new Timer(function () {
811
_this.ping()
912
}, this.healthcheckInterval)
1013
}

lib/agent/healthcheck/index.spec.js

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var expect = require('chai').expect
22

33
var Healthcheck = require('./')
4+
var Timer = require('../timer')
45

56
describe('The Healthcheck module', function () {
67
it('pings the collector', function () {
@@ -19,4 +20,18 @@ describe('The Healthcheck module', function () {
1920

2021
expect(collectorApi.ping).to.be.called
2122
})
23+
24+
it('should have a timer', function () {
25+
var collectorApi = {
26+
ping: this.sandbox.spy()
27+
}
28+
29+
var healthcheck = Healthcheck.create({
30+
collectorApi: collectorApi,
31+
config: {
32+
healthcheckInterval: 1
33+
}
34+
})
35+
expect(healthcheck.timer).to.be.instanceof(Timer)
36+
})
2237
})

lib/agent/index.js

+51-19
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var ReservoirSampler = require('./reservoir_sampler')
1515
var Profiler = require('./profiler')
1616
var Control = require('./control')
1717

18+
var Timer = require('./timer')
19+
1820
var controlBus = new EventEmitter()
1921

2022
var REQUEST_ID = 'request-id'
@@ -28,13 +30,6 @@ function Agent (options) {
2830
this.collectorApi = CollectorApi.create(options.config)
2931
// config
3032
this.config = options.config
31-
this.collectInterval = this.config.collectInterval
32-
33-
this.totalRequestCount = 0
34-
this.mustCollectCount = 0
35-
36-
// init required variables
37-
this.partials = {}
3833

3934
this.apmMetrics = Metrics.apm.create({
4035
collectorApi: this.collectorApi,
@@ -79,29 +74,66 @@ function Agent (options) {
7974
controlBus: controlBus
8075
})
8176

77+
// TODO: The Tracer agent, to be extracted
78+
this.name = 'Tracer'
79+
80+
this.collectInterval = this.config.collectInterval
81+
82+
this.totalRequestCount = 0
83+
this.mustCollectCount = 0
84+
85+
// init required variables
86+
this.partials = {}
87+
88+
this.reservoirSampler = new ReservoirSampler(MUST_COLLECT_LIMIT)
89+
90+
this.timer = new Timer(function () {
91+
_this._send()
92+
}, this.collectInterval)
93+
94+
this.tracer = this
95+
96+
//
97+
98+
this.agents = [
99+
this.tracer,
100+
this.apmMetrics,
101+
this.healthcheck,
102+
this.rpmMetrics,
103+
this.externalEdgeMetrics,
104+
this.incomingEdgeMetrics,
105+
this.memoryProfiler,
106+
this.cpuProfiler,
107+
this.control
108+
]
109+
}
110+
111+
Agent.prototype.start = function () {
112+
var _this = this
82113
this.collectorApi.getService(function (err, serviceKey) {
83114
if (err) {
84115
return debug(err.message)
85116
}
86117
debug('Agent serviceKey is set to: ', serviceKey)
87118
_this.serviceKey = serviceKey
88-
_this.start()
119+
_this._startAll()
89120
})
90-
91-
this.reservoirSampler = new ReservoirSampler(MUST_COLLECT_LIMIT)
92121
}
93122

94-
Agent.prototype.start = function () {
95-
var _this = this
96-
debug('Agent started collecting')
97-
this.transactionIntervalId = setInterval(function () {
98-
_this._send()
99-
}, this.collectInterval)
123+
Agent.prototype._startAll = function () {
124+
this.agents.forEach(function (agent) {
125+
debug(agent.name + ' started')
126+
if (agent.timer != null) {
127+
agent.timer.start()
128+
}
129+
})
100130
}
101131

102-
Agent.prototype.stop = function () {
103-
debug('Agent stopped collecting')
104-
clearInterval(this.transactionIntervalId)
132+
Agent.prototype._stopAll = function () {
133+
this.agents.forEach(function (agent) {
134+
debug(agent.name + ' stopped')
135+
agent.timer.end()
136+
})
105137
}
106138

107139
Agent.prototype.getServiceKey = function () {

lib/agent/metrics/apm/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
var os = require('os')
22
var gc = require('../../../optionalDependencies/gc-stats')()
33
var eventLoopStats = require('../../../optionalDependencies/event-loop-stats')
4+
var Timer = require('../../timer')
45

56
var BYTES_TO_MEGABYTES = 1024 * 1024
67

78
function ApmMetrics (options) {
9+
this.name = 'Metrics/APM'
810
var _this = this
911
this.cpuCount = os.cpus().length
1012
this.pid = process.pid
@@ -18,9 +20,6 @@ function ApmMetrics (options) {
1820
scavenge: 0,
1921
marksweep: 0
2022
}
21-
this.interval = setInterval(function () {
22-
_this.sendMetrics()
23-
}, this.collectInterval)
2423

2524
gc.on('stats', function (stats) {
2625
// time is in nanoseconds
@@ -39,6 +38,10 @@ function ApmMetrics (options) {
3938
break
4039
}
4140
})
41+
42+
this.timer = new Timer(function () {
43+
_this.sendMetrics()
44+
}, this.collectInterval)
4245
}
4346

4447
ApmMetrics.prototype.sendMetrics = function () {

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

+17
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var os = require('os')
33
var eventLoopStats = require('event-loop-stats')
44

55
var ApmMetrics = require('./')
6+
var Timer = require('../../timer')
67

78
describe('The ApmMetrics module', function () {
89
it('sends metrics', function () {
@@ -100,4 +101,20 @@ describe('The ApmMetrics module', function () {
100101
utilization: null
101102
})
102103
})
104+
105+
it('should have a timer', function () {
106+
var collectorApi = {
107+
sendApmMetrics: this.sandbox.spy()
108+
}
109+
110+
var apmMetrics = ApmMetrics.create({
111+
collectorApi: collectorApi,
112+
config: {
113+
collectInterval: 1,
114+
isRunningInVm: true
115+
}
116+
})
117+
118+
expect(apmMetrics.timer).to.be.instanceof(Timer)
119+
})
103120
})

lib/agent/metrics/externalEdge/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
var debug = require('debug')('risingstack/trace')
22
var flatMap = require('lodash.flatmap')
33
var consts = require('../../../consts')
4+
var Timer = require('../../timer')
45

56
function ExternalEdgeMetrics (options) {
7+
this.name = 'Metrics/ExternalEdge'
68
var _this = this
79
this.collectorApi = options.collectorApi
810
this.config = options.config
@@ -11,7 +13,7 @@ function ExternalEdgeMetrics (options) {
1113
// metrics
1214
this.metrics = {}
1315

14-
this.interval = setInterval(function () {
16+
this.timer = new Timer(function () {
1517
_this.sendMetrics()
1618
}, this.collectInterval)
1719
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var expect = require('chai').expect
22

33
var ExternalEdgeMetrics = require('./')
4+
var Timer = require('../../timer')
45

56
describe('The ExternalEdgeMetrics module', function () {
67
it('discards undefined responseTime values', function () {
@@ -112,4 +113,18 @@ describe('The ExternalEdgeMetrics module', function () {
112113
data: expectedHostMetrics
113114
})
114115
})
116+
117+
it('should have a timer', function () {
118+
var collectorApi = {
119+
sendExternalEdgeMetrics: this.sandbox.spy()
120+
}
121+
var edgeMetrics = ExternalEdgeMetrics.create({
122+
collectorApi: collectorApi,
123+
config: {
124+
collectInterval: 1
125+
}
126+
})
127+
128+
expect(edgeMetrics.timer).to.be.instanceof(Timer)
129+
})
115130
})

lib/agent/metrics/incomingEdge/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var flatMap = require('lodash.flatmap')
2+
var Timer = require('../../timer')
23

34
function IncomingEdgeMetrics (options) {
5+
this.name = 'Metrics/IncomingEdge'
46
var _this = this
57
this.collectorApi = options.collectorApi
68
this.config = options.config
@@ -9,7 +11,7 @@ function IncomingEdgeMetrics (options) {
911
// metrics
1012
this.metrics = {}
1113

12-
this.interval = setInterval(function () {
14+
this.timer = new Timer(function () {
1315
_this.sendMetrics()
1416
}, this.collectInterval)
1517
}

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var expect = require('chai').expect
22

33
var EdgeMetrics = require('./')
4-
4+
var Timer = require('../../timer')
55
describe('The IncomingEdgeMetrics module', function () {
66
it('discards negative transportDelays', function () {
77
var ISOString = 'date-string'
@@ -160,4 +160,18 @@ describe('The IncomingEdgeMetrics module', function () {
160160
protocol: 'http'
161161
}])
162162
})
163+
it('should have a timer', function () {
164+
var collectorApi = {
165+
sendIncomingEdgeMetrics: this.sandbox.spy()
166+
}
167+
168+
var edgeMetrics = EdgeMetrics.create({
169+
collectorApi: collectorApi,
170+
config: {
171+
collectInterval: 1
172+
}
173+
})
174+
175+
expect(edgeMetrics.timer).to.be.instanceof(Timer)
176+
})
163177
})

lib/agent/metrics/rpm/index.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
var Timer = require('../../timer')
2+
13
function RpmMetrics (options) {
4+
this.name = 'Metrics/RPM'
25
var _this = this
36
this.collectorApi = options.collectorApi
47
this.config = options.config
@@ -9,7 +12,7 @@ function RpmMetrics (options) {
912
this.responseTimes = []
1013
this.totalRequestCount = 0
1114

12-
this.interval = setInterval(function () {
15+
this.timer = new Timer(function () {
1316
_this.sendMetrics()
1417
}, this.collectInterval)
1518
}

0 commit comments

Comments
 (0)