Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Commit

Permalink
Add support for Gauge metrics. (#10)
Browse files Browse the repository at this point in the history
* Add support for Gauge metrics.

* Update package-lock.json
  • Loading branch information
akodali18 authored and vikramraman committed May 6, 2019
1 parent fc47a37 commit fce7f3d
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 26 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ const registry = new metrics.Registry();

// Counter with metric level tags
let c = new metrics.Counter();
registry.addTaggedMetric("http.requests", c, {"key1":"val1"});
registry.addTaggedMetric("request.counter", c, {"key1":"val1"});
c.inc();

// Gauge with metric level tags
let g = new metrics.Gauge(2.2);
registry.addTaggedMetric("request.gauge", g, {"key1":"val1"});

// Histogram
let h = new metrics.Histogram();
registry.addTaggedMetric("request.duration", h, {"key1":"val1"});
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ exports.Histogram = Metrics.Histogram;
exports.Meter = Metrics.Meter;
exports.Counter = Metrics.Counter;
exports.Timer = Metrics.Timer;
exports.Gauge = Metrics.Gauge;

exports.deltaCounterName = Delta.deltaCounterName;
exports.Registry = require('./registry');
Expand Down
9 changes: 9 additions & 0 deletions lib/wavefront-direct-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ class WavefrontDirectReporter extends ScheduledReporter {
metrics.counters.forEach(count => self.appendCounter(count, points));
}

if (metrics.gauges.length != 0) {
metrics.gauges.forEach(value => self.appendGauge(value, points));
}

if (metrics.meters.length != 0) {
metrics.meters.forEach(meter => self.appendMeter(meter, points));
}
Expand Down Expand Up @@ -128,6 +132,11 @@ class WavefrontDirectReporter extends ScheduledReporter {
points.push(formatter.counterPoint(counter, this.prefix, '', tags));
}

appendGauge(gauge, points) {
let tags = helper.tagger(this.registry.getTags(gauge.name), this.globaltags);
points.push(formatter.gaugePoint(gauge, this.prefix, '', tags));
}

appendMeter(meter, points) {
let tags = helper.tagger(this.registry.getTags(meter.name), this.globaltags);
points.push(...formatter.meterPoints(meter, this.prefix, '', tags));
Expand Down
5 changes: 5 additions & 0 deletions lib/wavefront-metrics-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ function counterPoint(counter, prefix, ts, tags) {
}
}

function gaugePoint(gauge, prefix, ts, tags) {
return pointLine(prefix, gauge.name, '', gauge.value, ts, tags);
}

function meterPoints(meter, prefix, ts, tags) {
let points = [];
points.push(pointLine(prefix, meter.name, '.count', meter.count, ts, tags));
Expand Down Expand Up @@ -68,6 +72,7 @@ function pointLine(prefix, name, suffix, value, ts, tags) {

module.exports = {
counterPoint,
gaugePoint,
meterPoints,
timerPoints,
histoPoints
Expand Down
9 changes: 9 additions & 0 deletions lib/wavefront-proxy-reporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class WavefrontProxyReporter extends ScheduledReporter {
metrics.counters.forEach(count => self.reportCounter(count, timestamp));
}

if (metrics.gauges.length != 0) {
metrics.gauges.forEach(value => self.reportGauge(value, timestamp));
}

if (metrics.meters.length != 0) {
metrics.meters.forEach(meter => self.reportMeter(meter, timestamp));
}
Expand Down Expand Up @@ -104,6 +108,11 @@ class WavefrontProxyReporter extends ScheduledReporter {
this.send(formatter.counterPoint(counter, this.prefix, timestamp, tags));
}

reportGauge(gauge, timestamp) {
let tags = helper.tagger(this.registry.getTags(gauge.name), this.globaltags);
this.send(formatter.gaugePoint(gauge, this.prefix, timestamp, tags));
}

reportMeter(meter, timestamp) {
let tags = helper.tagger(this.registry.getTags(meter.name), this.globaltags);
let points = formatter.meterPoints(meter, this.prefix, timestamp, tags);
Expand Down
48 changes: 24 additions & 24 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"mocha": "^5.2.0"
},
"dependencies": {
"metrics": "^0.1.11"
"metrics": "^0.1.21"
}
}
13 changes: 13 additions & 0 deletions test/formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,19 @@ describe('deltaPoint', function() {
});
});

describe('gaugePoint', function() {
it('Validate gauge point format', function() {
const gauge = new metrics.Gauge(2.2);
gauge.name = "requests.value";

let point = formatter.gaugePoint(gauge, 'test', '', "key1=\"val1\"");
expect(point).to.be.equal('test.requests.value 2.2 key1=\"val1\"');

point = formatter.gaugePoint(gauge, 'test', '1350450000', "key1=\"val1\"")
expect(point).to.be.equal('test.requests.value 2.2 1350450000 key1=\"val1\"');
});
});

describe('meterPoints', function() {
it('Validate meter point format', function() {
const meter = new metrics.Meter();
Expand Down

0 comments on commit fce7f3d

Please sign in to comment.