Skip to content

Commit

Permalink
feat: Added the ability to pass labels as an object to labels() and r…
Browse files Browse the repository at this point in the history
…emove()

Typings: updated for labels and remove object args

returned passed labels as string arguments in README

returned a lost line from changelog
  • Loading branch information
boris-chernysh authored and zbjornson committed Jan 23, 2021
1 parent 487611c commit 3a86d05
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 10 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ project adheres to [Semantic Versioning](http://semver.org/).

### Added

- feat: added the ability to pass labels as an object to `labels()` and `remove()`

## [13.0.0] - 2020-12-16

### Breaking
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ const gauge = new client.Gauge({
// 1st version: Set value to 100 with "method" set to "GET" and "statusCode" to "200"
gauge.set({ method: 'GET', statusCode: '200' }, 100);
// 2nd version: Same effect as above
gauge.labels({ method: 'GET', statusCode: '200' }).set(100);
// 3nd version: And again the same effect as above
gauge.labels('GET', '200').set(100);
```

Expand Down
52 changes: 52 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,13 @@ export class Counter<T extends string> {
*/
labels(...values: string[]): Counter.Internal;

/**
* Return the child for given labels
* @param labels Object with label keys and values
* @return Configured counter with given labels
*/
labels(labels: LabelValues<T>): Counter.Internal;

/**
* Reset counter values
*/
Expand All @@ -194,6 +201,12 @@ export class Counter<T extends string> {
* @param values Label values
*/
remove(...values: string[]): void;

/**
* Remove metrics for the given label values
* @param labels Object with label keys and values
*/
remove(labels: LabelValues<T>): void;
}

export namespace Counter {
Expand Down Expand Up @@ -279,6 +292,13 @@ export class Gauge<T extends string> {
*/
labels(...values: string[]): Gauge.Internal<T>;

/**
* Return the child for given labels
* @param labels Object with label keys and values
* @return Configured counter with given labels
*/
labels(labels: LabelValues<T>): Gauge.Internal<T>;

/**
* Reset gauge values
*/
Expand All @@ -289,6 +309,12 @@ export class Gauge<T extends string> {
* @param values Label values
*/
remove(...values: string[]): void;

/**
* Remove metrics for the given label values
* @param labels Object with label keys and values
*/
remove(labels: LabelValues<T>): void;
}

export namespace Gauge {
Expand Down Expand Up @@ -370,11 +396,24 @@ export class Histogram<T extends string> {
*/
labels(...values: string[]): Histogram.Internal<T>;

/**
* Return the child for given labels
* @param labels Object with label keys and values
* @return Configured counter with given labels
*/
labels(labels: LabelValues<T>): Histogram.Internal<T>;

/**
* Remove metrics for the given label values
* @param values Label values
*/
remove(...values: string[]): void;

/**
* Remove metrics for the given label values
* @param labels Object with label keys and values
*/
remove(labels: LabelValues<T>): void;
}

export namespace Histogram {
Expand Down Expand Up @@ -450,11 +489,24 @@ export class Summary<T extends string> {
*/
labels(...values: string[]): Summary.Internal<T>;

/**
* Return the child for given labels
* @param labels Object with label keys and values
* @return Configured counter with given labels
*/
labels(labels: LabelValues<T>): Summary.Internal<T>;

/**
* Remove metrics for the given label values
* @param values Label values
*/
remove(...values: string[]): void;

/**
* Remove metrics for the given label values
* @param labels Object with label keys and values
*/
remove(labels: LabelValues<T>): void;
}

export namespace Summary {
Expand Down
3 changes: 2 additions & 1 deletion lib/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,16 @@ class Counter extends Metric {

labels() {
const labels = getLabels(this.labelNames, arguments) || {};
const hash = hashObject(labels);
validateLabel(this.labelNames, labels);
const hash = hashObject(labels);
return {
inc: inc.call(this, labels, hash),
};
}

remove() {
const labels = getLabels(this.labelNames, arguments) || {};
validateLabel(this.labelNames, labels);
return removeLabels.call(this, this.hashMap, labels);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/gauge.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class Gauge extends Metric {

labels() {
const labels = getLabels(this.labelNames, arguments);
validateLabel(this.labelNames, labels);
return {
inc: inc.call(this, labels),
dec: dec.call(this, labels),
Expand All @@ -113,6 +114,7 @@ class Gauge extends Metric {

remove() {
const labels = getLabels(this.labelNames, arguments);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/histogram.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Histogram extends Metric {

labels() {
const labels = getLabels(this.labelNames, arguments);
validateLabel(this.labelNames, labels);
return {
observe: observe.call(this, labels),
startTimer: startTimer.call(this, labels),
Expand All @@ -98,6 +99,7 @@ class Histogram extends Metric {

remove() {
const labels = getLabels(this.labelNames, arguments);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class Summary extends Metric {

labels() {
const labels = getLabels(this.labelNames, arguments);
validateLabel(this.labelNames, labels);
return {
observe: observe.call(this, labels),
startTimer: startTimer.call(this, labels),
Expand All @@ -104,6 +105,7 @@ class Summary extends Metric {

remove() {
const labels = getLabels(this.labelNames, arguments);
validateLabel(this.labelNames, labels);
removeLabels.call(this, this.hashMap, labels);
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ exports.setValue = function setValue(hashMap, value, labels) {

// TODO: For node 6, use rest params
exports.getLabels = function (labelNames, args) {
if (typeof args[0] === 'object') {
return args[0];
}

if (labelNames.length !== args.length) {
throw new Error('Invalid number of arguments');
}
Expand Down
2 changes: 0 additions & 2 deletions test/__snapshots__/counterTest.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ exports[`counter remove should throw error if label lengths does not match 1`] =

exports[`counter with params as object labels should throw error if label lengths does not match 1`] = `"Invalid number of arguments"`;

exports[`counter with params as object labels should throw error if label lengths does not match 2`] = `"Invalid number of arguments"`;

exports[`counter with params as object should not be possible to decrease a counter 1`] = `"It is not possible to decrease a counter"`;

exports[`counter with params as object should throw an error when the value is not a number 1`] = `"Value is not a valid number: 3ms"`;
28 changes: 21 additions & 7 deletions test/counterTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ describe('counter', () => {
expect(values).toHaveLength(2);
});

it('should handle labels provided as an object', async () => {
instance.labels({ method: 'POST', endpoint: '/test' }).inc();
const values = (await instance.get()).values;
expect(values).toHaveLength(1);
expect(values[0].labels).toEqual({
method: 'POST',
endpoint: '/test',
});
});

it('should handle labels which are provided as arguments to inc()', async () => {
instance.inc({ method: 'GET', endpoint: '/test' });
instance.inc({ method: 'POST', endpoint: '/test' });
Expand All @@ -80,13 +90,6 @@ describe('counter', () => {
expect(fn).toThrowErrorMatchingSnapshot();
});

it('should throw error if label lengths does not match', () => {
const fn = function () {
instance.labels('GET').inc();
};
expect(fn).toThrowErrorMatchingSnapshot();
});

it('should increment label value with provided value', async () => {
instance.labels('GET', '/test').inc(100);
const values = (await instance.get()).values;
Expand Down Expand Up @@ -121,6 +124,17 @@ describe('counter', () => {
expect(values[0].timestamp).toEqual(undefined);
});

it('should remove by labels object', async () => {
instance.remove({ method: 'POST', endpoint: '/test' });

const values = (await instance.get()).values;
expect(values).toHaveLength(1);
expect(values[0].labels).toEqual({
method: 'GET',
endpoint: '/test',
});
});

it('should remove all labels', async () => {
instance.remove('GET', '/test');
instance.remove('POST', '/test');
Expand Down
14 changes: 14 additions & 0 deletions test/gaugeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ describe('gauge', () => {
end({ code: '400' });
expect(startLabels).toEqual({ code: '200' });
});
it('should handle labels provided as an object', async () => {
instance.labels({ code: '200' }).inc();

const values = (await instance.get()).values;
expect(values).toHaveLength(1);
expect(values[0].labels).toEqual({ code: '200' });
});
});

describe('with remove', () => {
Expand All @@ -159,6 +166,13 @@ describe('gauge', () => {
expect(values[0].labels.code).toEqual('400');
expect(values[0].value).toEqual(0);
});
it('should remove by labels object', async () => {
instance.remove({ code: '200' });
const values = (await instance.get()).values;
expect(values).toHaveLength(1);
expect(values[0].labels).toEqual({ code: '400' });
expect(values[0].value).toEqual(0);
});
it('should be able to remove all labels', async () => {
instance.remove('200');
instance.remove('400');
Expand Down
14 changes: 14 additions & 0 deletions test/histogramTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,14 @@ describe('histogram', () => {
end({ method: 'POST' });
expect(startLabels).toEqual({ method: 'GET' });
});

it('should handle labels provided as an object', async () => {
instance.labels({ method: 'GET' }).startTimer()();
const values = (await instance.get()).values;
values.forEach(value => {
expect(value.labels.method).toBe('GET');
});
});
});

describe('remove', () => {
Expand Down Expand Up @@ -355,6 +363,12 @@ describe('histogram', () => {
expect((await instance.get()).values).toHaveLength(0);
jest.useRealTimers();
});

it('should remove by labels object', async () => {
instance.observe({ method: 'GET' }, 1);
instance.remove({ method: 'GET' });
expect((await instance.get()).values).toHaveLength(0);
});
});
});

Expand Down
17 changes: 17 additions & 0 deletions test/summaryTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ describe('summary', () => {
end({ endpoint: '/test' });
expect(startLabels).toEqual({ method: 'GET' });
});

it('should handle labels provided as an object', async () => {
instance.labels({ method: 'GET' }).startTimer()();
const values = (await instance.get()).values;
values.forEach(value => {
expect(value.labels.method).toBe('GET');
});
});
});

describe('remove', () => {
Expand Down Expand Up @@ -408,6 +416,15 @@ describe('summary', () => {

jest.useRealTimers();
});

it('should remove by labels object', async () => {
instance.observe({ endpoint: '/test' }, 1);
instance.remove({ endpoint: '/test' });
const values = (await instance.get()).values;
values.forEach(value => {
expect(value.labels).not.toEqual({ endpoint: '/test' });
});
});
});
});
});
Expand Down

0 comments on commit 3a86d05

Please sign in to comment.