Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init metrics to zero #139

Merged
merged 8 commits into from
Jul 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
## [Unreleased]
### Breaking
### Added
- Metrics should be initialized to 0 when there are no labels
### Changed

## [10.0.0] - 2017-07-04
Expand Down
4 changes: 4 additions & 0 deletions lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class Counter {

this.labelNames = config.labelNames || [];

if (this.labelNames.length === 0) {
this.hashMap = createValue({}, 0);
}

this.help = config.help;

config.registers.forEach(registryInstance =>
Expand Down
3 changes: 3 additions & 0 deletions lib/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class Gauge {
this.name = config.name;
this.labelNames = config.labelNames || [];
this.hashMap = {};
if (this.labelNames.length === 0) {
this.hashMap = createValue({}, 0, {});
}
this.help = config.help;

config.registers.forEach(registryInstance =>
Expand Down
9 changes: 9 additions & 0 deletions lib/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ class Histogram {
this.hashMap = {};
this.labelNames = config.labelNames || [];

if (this.labelNames.length === 0) {
this.hashMap = {
[hashObject({})]: createBaseValues(
{},
Object.assign({}, this.bucketValues)
)
};
}

config.registers.forEach(registryInstance =>
registryInstance.registerMetric(this)
);
Expand Down
11 changes: 11 additions & 0 deletions lib/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,17 @@ class Summary {
this.hashMap = {};
this.labelNames = config.labelNames || [];

if (this.labelNames.length === 0) {
this.hashMap = {
[hashObject({})]: {
labels: {},
td: new TDigest(),
count: 0,
sum: 0
}
};
}

config.registers.forEach(registryInstance =>
registryInstance.registerMetric(this)
);
Expand Down
56 changes: 56 additions & 0 deletions test/__snapshots__/registerTest.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`register should not output all initialized metrics at value 0 if labels 1`] = `
"# HELP counter help
# TYPE counter counter

# HELP gauge help
# TYPE gauge gauge

# HELP histogram help
# TYPE histogram histogram

# HELP summary help
# TYPE summary summary
"
`;

exports[`register should output all initialized metrics at value 0 1`] = `
"# HELP counter help
# TYPE counter counter
counter 0

# HELP gauge help
# TYPE gauge gauge
gauge 0

# HELP histogram help
# TYPE histogram histogram
histogram_bucket{le=\\"0.005\\"} 0
histogram_bucket{le=\\"0.01\\"} 0
histogram_bucket{le=\\"0.025\\"} 0
histogram_bucket{le=\\"0.05\\"} 0
histogram_bucket{le=\\"0.1\\"} 0
histogram_bucket{le=\\"0.25\\"} 0
histogram_bucket{le=\\"0.5\\"} 0
histogram_bucket{le=\\"1\\"} 0
histogram_bucket{le=\\"2.5\\"} 0
histogram_bucket{le=\\"5\\"} 0
histogram_bucket{le=\\"10\\"} 0
histogram_bucket{le=\\"+Inf\\"} 0
histogram_sum 0
histogram_count 0

# HELP summary help
# TYPE summary summary
summary{quantile=\\"0.01\\"} 0
summary{quantile=\\"0.05\\"} 0
summary{quantile=\\"0.5\\"} 0
summary{quantile=\\"0.9\\"} 0
summary{quantile=\\"0.95\\"} 0
summary{quantile=\\"0.99\\"} 0
summary{quantile=\\"0.999\\"} 0
summary_sum 0
summary_count 0
"
`;
5 changes: 5 additions & 0 deletions test/counterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ describe('counter', () => {
instance.inc(0);
expect(instance.get().values[0].value).toEqual(0);
});
it('should init counter to 0', () => {
const values = instance.get().values;
expect(values).toHaveLength(1);
expect(values[0].value).toEqual(0);
});

describe('labels', () => {
beforeEach(() => {
Expand Down
8 changes: 8 additions & 0 deletions test/gaugeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ describe('gauge', () => {
expect(fn).toThrowError(Error);
});

it('should init to 0', () => {
instance = new Gauge({
name: 'init_gauge',
help: 'somehelp'
});
expectValue(0);
});

describe('with labels', () => {
beforeEach(() => {
instance = new Gauge({
Expand Down
6 changes: 6 additions & 0 deletions test/histogramTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,12 @@ describe('histogram', () => {
expect(valuePair.value).toEqual(undefined);
});

it('should init to 0', () => {
instance.get().values.forEach(bucket => {
expect(bucket.value).toEqual(0);
});
});

describe('labels', () => {
beforeEach(() => {
instance = new Histogram({
Expand Down
22 changes: 22 additions & 0 deletions test/registerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

describe('register', () => {
const register = require('../index').register;
const Counter = require('../index').Counter;
const Gauge = require('../index').Gauge;
const Histogram = require('../index').Histogram;
const Summary = require('../index').Summary;

beforeEach(() => {
register.clear();
Expand Down Expand Up @@ -101,6 +105,24 @@ describe('register', () => {
);
});

it('should output all initialized metrics at value 0', () => {
new Counter({ name: 'counter', help: 'help' });
new Gauge({ name: 'gauge', help: 'help' });
new Histogram({ name: 'histogram', help: 'help' });
new Summary({ name: 'summary', help: 'help' });

expect(register.metrics()).toMatchSnapshot();
});

it('should not output all initialized metrics at value 0 if labels', () => {
new Counter({ name: 'counter', help: 'help', labelNames: ['label'] });
new Gauge({ name: 'gauge', help: 'help', labelNames: ['label'] });
new Histogram({ name: 'histogram', help: 'help', labelNames: ['label'] });
new Summary({ name: 'summary', help: 'help', labelNames: ['label'] });

expect(register.metrics()).toMatchSnapshot();
});

describe('should escape', () => {
let escapedResult;
beforeEach(() => {
Expand Down
6 changes: 6 additions & 0 deletions test/summaryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ describe('summary', () => {
expect(instance.get().values[2].value).toEqual(0);
});

it('should init to 0', () => {
instance.get().values.forEach(percentile => {
expect(percentile.value).toEqual(0);
});
});

describe('labels', () => {
beforeEach(() => {
globalRegistry.clear();
Expand Down