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

Allow setting Gauge values to NaN. #202

Merged
merged 1 commit into from Aug 16, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Changed

* Allow setting Gauge values to NaN, +Inf, and -Inf

### Added

## [11.1.1] - 2018-06-29
Expand Down
2 changes: 1 addition & 1 deletion lib/gauge.js
Expand Up @@ -215,7 +215,7 @@ function inc(labels) {

function set(labels) {
return (value, timestamp) => {
if (!Number.isFinite(value)) {
if (typeof value !== 'number') {
throw new TypeError(`Value is not a valid number: ${util.format(value)}`);
}
if (timestamp && !isDate(timestamp) && !Number.isFinite(timestamp)) {
Expand Down
3 changes: 2 additions & 1 deletion lib/registry.js
@@ -1,4 +1,5 @@
'use strict';
const getValueAsString = require('./util').getValueAsString;

function escapeString(str) {
return str.replace(/\n/g, '\\n').replace(/\\(?!n)/g, '\\\\');
Expand Down Expand Up @@ -44,7 +45,7 @@ class Registry {
metricName += `{${labels.join(',')}}`;
}

const line = [metricName, val.value];
const line = [metricName, getValueAsString(val.value)];
if (opts.timestamps) {
line.push(val.timestamp);
}
Expand Down
16 changes: 15 additions & 1 deletion lib/util.js
Expand Up @@ -9,10 +9,24 @@ exports.getPropertiesFromObj = function(hashMap) {
return obj;
};

exports.getValueAsString = function getValueString(value) {
if (Number.isNaN(value)) {
return 'Nan';
} else if (!Number.isFinite(value)) {
if (value < 0) {
return '-Inf';
} else {
return '+Inf';
}
} else {
return `${value}`;
}
};

exports.setValue = function setValue(hashMap, value, labels, timestamp) {
const hash = hashObject(labels);
hashMap[hash] = {
value: Number.isFinite(value) ? value : 0,
value: typeof value === 'number' ? value : 0,
labels: labels || {},
timestamp: isDate(timestamp)
? timestamp.valueOf()
Expand Down
15 changes: 15 additions & 0 deletions test/gaugeTest.js
Expand Up @@ -64,6 +64,21 @@ describe('gauge', () => {
expect(fn).toThrowErrorMatchingSnapshot();
});

it('should allow NaN as a value', () => {
instance.set(NaN);
expectValue(NaN);
});

it('should allow +Inf as a value', () => {
instance.set(Infinity);
expectValue(Infinity);
});

it('should allow -Inf as a value', () => {
instance.set(-Infinity);
expectValue(-Infinity);
});

describe('with labels', () => {
beforeEach(() => {
instance = new Gauge('name', 'help', ['code']);
Expand Down
60 changes: 60 additions & 0 deletions test/registerTest.js
Expand Up @@ -44,6 +44,66 @@ describe('register', () => {
);
});

it('should handle and output a metric with a NaN value', () => {
register.registerMetric({
get() {
return {
name: 'test_metric',
type: 'gauge',
help: 'A test metric',
values: [
{
value: NaN
}
]
};
}
});
const lines = register.metrics().split('\n');
expect(lines).toHaveLength(4);
expect(lines[2]).toEqual('test_metric Nan');
});

it('should handle and output a metric with an +Infinity value', () => {
register.registerMetric({
get() {
return {
name: 'test_metric',
type: 'gauge',
help: 'A test metric',
values: [
{
value: Infinity
}
]
};
}
});
const lines = register.metrics().split('\n');
expect(lines).toHaveLength(4);
expect(lines[2]).toEqual('test_metric +Inf');
});

it('should handle and output a metric with an -Infinity value', () => {
register.registerMetric({
get() {
return {
name: 'test_metric',
type: 'gauge',
help: 'A test metric',
values: [
{
value: -Infinity
}
]
};
}
});
const lines = register.metrics().split('\n');
expect(lines).toHaveLength(4);
expect(lines[2]).toEqual('test_metric -Inf');
});

it('should handle a metric without labels', () => {
register.registerMetric({
get() {
Expand Down