Skip to content

Commit cf004f1

Browse files
committed
feat(custom-metrics): upgrade to v2 API
1 parent d2cb54e commit cf004f1

7 files changed

Lines changed: 64 additions & 6 deletions

File tree

lib/agent/metrics/custom/index.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
var inherits = require('util').inherits
22
var Agent = require('../../agent')
3+
var reservoirSampler = require('../../util/samplers/reservoir')
34

45
function CustomMetrics (options) {
56
this.name = 'Metrics/Custom'
67
this.collectorApi = options.collectorApi
78
this.config = options.config
89
this.collectInterval = this.config.collectInterval
10+
this.samplerLimit = 15
911

1012
this.incrementMetrics = {}
1113
this.recordMetrics = {}
@@ -36,14 +38,30 @@ CustomMetrics.prototype.record = function (name, value) {
3638
}
3739

3840
if (this.recordMetrics[name]) {
39-
this.recordMetrics[name].push(value)
41+
var metric = this.recordMetrics[name]
42+
metric.sampler.add(value)
43+
metric.sum += value
44+
metric.min = metric.min < value ? metric.min : value
45+
metric.max = metric.max > value ? metric.max : value
46+
++metric.count
4047
} else {
41-
this.recordMetrics[name] = [value]
48+
this.recordMetrics[name] = {
49+
sampler: reservoirSampler.create(this.samplerLimit),
50+
count: 1,
51+
sum: value,
52+
max: value,
53+
min: value
54+
}
55+
this.recordMetrics[name].sampler.add(value)
4256
}
4357
}
4458

4559
CustomMetrics.prototype.sendMetrics = function (callback) {
4660
callback = callback || function () {}
61+
var self = this
62+
Object.keys(this.recordMetrics).forEach(function (key) {
63+
prepareRecordMetric(self.recordMetrics[key])
64+
})
4765
this.collectorApi.sendCustomMetrics({
4866
incrementMetrics: this.incrementMetrics,
4967
recordMetrics: this.recordMetrics
@@ -62,5 +80,13 @@ function create (options) {
6280
return new CustomMetrics(options)
6381
}
6482

83+
function prepareRecordMetric (recordMetric) {
84+
var samples = recordMetric.sampler.flush()
85+
delete recordMetric.sampler
86+
recordMetric.samples = samples
87+
recordMetric.avg = recordMetric.sum / recordMetric.count
88+
delete recordMetric.sum
89+
}
90+
6591
module.exports = CustomMetrics
6692
module.exports.create = create

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,40 @@ describe('The CustomMetrics module', function () {
3434
'/TestCategory/TestName': 5
3535
},
3636
recordMetrics: {
37-
'/TestCategory/TestRecord': [10, 2]
37+
'/TestCategory/TestRecord': {
38+
avg: 6,
39+
count: 2,
40+
max: 10,
41+
min: 2,
42+
samples: [10, 2]
43+
}
3844
}
3945
})
4046
})
47+
48+
it('samples recordMetrics', function () {
49+
var ISOString = 'date-string'
50+
var collectorApi = {
51+
sendCustomMetrics: this.sandbox.spy()
52+
}
53+
54+
this.sandbox.stub(Date.prototype, 'toISOString', function () {
55+
return ISOString
56+
})
57+
58+
var customMetrics = CustomMetrics.create({
59+
collectorApi: collectorApi,
60+
config: {
61+
collectInterval: 1
62+
}
63+
})
64+
65+
for (var i = 30; i > 0; --i) {
66+
customMetrics.record('/TestCategory/TestRecord', 10)
67+
}
68+
69+
customMetrics.sendMetrics()
70+
71+
expect(collectorApi.sendCustomMetrics.args[0][0].recordMetrics['/TestCategory/TestRecord'].samples).to.have.length(15)
72+
})
4173
})

lib/agent/tracer/collector.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var uuid = require('uuid')
77
var assign = require('lodash.assign')
88
var levels = require('./severity')
99
var some = require('lodash.some')
10-
var reservoirSampler = require('./samplers/reservoir')
10+
var reservoirSampler = require('../util/samplers/reservoir')
1111

1212
var EVENT_TYPE = {
1313
CLIENT_SEND: 'cs',
File renamed without changes.

lib/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ config.collectorApiHealthcheckEndpoint = '/service/%s/healthcheck'
1919
config.collectorApiProfilerMemoryHeapdumpEndpoint = '/service/%s/memory-heapdump'
2020
config.collectorApiProfilerCpuProfileEndpoint = '/service/%s/cpu-profile'
2121
config.collectorApiControlEndpoint = '/service/%s/control'
22-
config.collectorApiCustomMetrics = '/service/%s/custom-metrics'
22+
config.collectorApiCustomMetrics = '/v2/service/%s/custom-metrics'
2323
config.collectorApiSecurityDependenciesEndpoint = '/service/%s/dependencies'
2424

2525
config.configPath = 'trace.config'

test/e2e/utils/serviceMocks.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function mockCustomMetricsRequest (opts) {
4040
'Authorization': 'Bearer ' + opts.apiKey
4141
}
4242
})
43-
.post('/service/' + opts.serviceKey + '/custom-metrics')
43+
.post('/v2/service/' + opts.serviceKey + '/custom-metrics')
4444
.times(opts.maxTimes || 1)
4545
.reply(opts.callback || 200)
4646
}

0 commit comments

Comments
 (0)