Skip to content

Commit 5f781b4

Browse files
committed
feat(healthcheck): adding healthcheck module
1 parent 2d198b5 commit 5f781b4

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

lib/agent/api/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ function CollectorApi (options) {
1717
this.COLLECTOR_API_RPM_METRICS = url.resolve(options.collectorApiUrl, options.collectorApiRpmMetricsEndpoint)
1818
this.COLLECTOR_API_INCOMING_EDGE_METRICS = url.resolve(options.collectorApiUrl, options.collectorApiIncomingEdgeMetricsEndpoint)
1919
this.COLLECTOR_API_EXTERNAL_EDGE_METRICS = url.resolve(options.collectorApiUrl, options.collectorApiExternalEdgeMetricsEndpoint)
20+
this.COLLECTOR_API_HEALTHCHECK = url.resolve(options.collectorApiUrl, options.collectorApiHealthcheckEndpoint)
2021

2122
this.collectorLanguage = options.collectorLanguage
2223
this.apiKey = options.apiKey
@@ -96,6 +97,16 @@ CollectorApi.prototype.sendRpmMetrics = function (data) {
9697
this._send(url, this._withInstanceInfo(data))
9798
}
9899

100+
CollectorApi.prototype.ping = function () {
101+
if (!isNumber(this.serviceKey)) {
102+
debug('Service id not present, cannot do healthcheck')
103+
return
104+
}
105+
106+
var url = util.format(this.COLLECTOR_API_HEALTHCHECK, this.serviceKey)
107+
this._send(url, this._withInstanceInfo({}))
108+
}
109+
99110
CollectorApi.prototype.sendApmMetrics = function (data) {
100111
if (!isNumber(this.serviceKey)) {
101112
debug('Service id not present, cannot send metrics')

lib/agent/api/index.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('The Trace CollectorApi module', function () {
2121
collectorApiEdgeMetricsEndpoint: '/service/%s/edge-metrics',
2222
collectorApiIncomingEdgeMetricsEndpoint: '/service/%s/edge-incoming',
2323
collectorApiExternalEdgeMetricsEndpoint: '/service/%s/edge-external',
24+
collectorApiHealthcheckEndpoint: '/service/%s/healthcheck',
2425
system: {
2526
hostname: 'test.org',
2627
processVersion: '4.3.1',

lib/agent/healthcheck/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function Healthcheck (options) {
2+
var _this = this
3+
4+
this.collectorApi = options.collectorApi
5+
this.config = options.config
6+
this.healthcheckInterval = this.config.healthcheckInterval
7+
this.interval = setInterval(function () {
8+
_this.ping()
9+
}, this.healthcheckInterval)
10+
}
11+
12+
Healthcheck.prototype.ping = function () {
13+
this.collectorApi.ping()
14+
}
15+
16+
function create (options) {
17+
return new Healthcheck(options)
18+
}
19+
20+
module.exports.create = create
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var expect = require('chai').expect
2+
3+
var Healthcheck = require('./')
4+
5+
describe('The Healthcheck module', function () {
6+
it('pings the collector', function () {
7+
var collectorApi = {
8+
ping: this.sandbox.spy()
9+
}
10+
11+
var healthcheck = Healthcheck.create({
12+
collectorApi: collectorApi,
13+
config: {
14+
healthcheckInterval: 1
15+
}
16+
})
17+
18+
healthcheck.ping()
19+
20+
expect(collectorApi.ping).to.be.called
21+
})
22+
})

lib/agent/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var assign = require('lodash.assign')
77

88
var CollectorApi = require('./api')
99
var Metrics = require('./metrics')
10+
var Healthcheck = require('./healthcheck')
1011
var consts = require('../consts')
1112
var ReservoirSampler = require('./reservoir_sampler')
1213

@@ -34,6 +35,11 @@ function Agent (options) {
3435
config: this.config
3536
})
3637

38+
this.healthcheck = Healthcheck.create({
39+
collectorApi: this.collectorApi,
40+
config: this.config
41+
})
42+
3743
this.rpmMetrics = Metrics.rpm.create({
3844
collectorApi: this.collectorApi,
3945
config: this.config

lib/config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var url = require('url')
22
var config = {}
33

44
config.collectInterval = 2 * 60 * 1000
5+
config.healthcheckInterval = 10 * 1000
56

67
config.collectorLanguage = 'nodejs'
78
config.collectorApiUrl = 'https://trace-collector-api.risingstack.com'
@@ -12,6 +13,7 @@ config.collectorApiRpmMetricsEndpoint = '/service/%s/rpm-metrics'
1213
config.collectorApiEdgeMetricsEndpoint = '/service/%s/edge-metrics'
1314
config.collectorApiIncomingEdgeMetricsEndpoint = '/service/%s/edge-incoming'
1415
config.collectorApiExternalEdgeMetricsEndpoint = '/service/%s/edge-external'
16+
config.collectorApiHealthcheckEndpoint = '/service/%s/healthcheck'
1517

1618
config.configPath = 'trace.config'
1719

0 commit comments

Comments
 (0)