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

bug: support array argument to labels() again #553

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ project adheres to [Semantic Versioning](http://semver.org/).
avoid failures from the server when using `Content-Encoding: gzip` header.
- Refactor `escapeString` helper in `lib/registry.js` to improve performance and
avoid an unnecessarily complex regex.
- Support array argument to `metric.labels()`. This was supported prior to
version 13.1 (undocumented). [#553](https://github.com/siimon/prom-client/pull/553)

### Added

Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ gauge.set({ method: 'GET', statusCode: '200' }, 100);
gauge.labels({ method: 'GET', statusCode: '200' }).set(100);
// 3rd version: And again the same effect as above
gauge.labels('GET', '200').set(100);
// 4th version: And again the same effect as above
gauge.labels(['GET', '200']).set(100);
```

It is also possible to use timers with labels, both before and after the timer
Expand Down
52 changes: 52 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ export class Counter<T extends string = string> {
*/
get(): Promise<MetricObjectWithValues<MetricValue<T>>>;

/**
* Return the child for given labels
* @param values Label values
* @return Configured counter with given labels
*/
labels(values: string[]): Counter.Internal;

/**
* Return the child for given labels
* @param values Label values
Expand All @@ -277,6 +284,12 @@ export class Counter<T extends string = string> {
*/
reset(): void;

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

/**
* Remove metrics for the given label values
* @param values Label values
Expand Down Expand Up @@ -373,6 +386,13 @@ export class Gauge<T extends string = string> {
*/
startTimer(labels?: LabelValues<T>): (labels?: LabelValues<T>) => number;

/**
* Return the child for given labels
* @param values Label values
* @return Configured gauge with given labels
*/
labels(values: string[]): Gauge.Internal<T>;

/**
* Return the child for given labels
* @param values Label values
Expand All @@ -392,6 +412,12 @@ export class Gauge<T extends string = string> {
*/
reset(): void;

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

/**
* Remove metrics for the given label values
* @param values Label values
Expand Down Expand Up @@ -497,6 +523,13 @@ export class Histogram<T extends string = string> {
*/
zero(labels: LabelValues<T>): void;

/**
* Return the child for given labels
* @param values Label values
* @return Configured histogram with given labels
*/
labels(values: string[]): Histogram.Internal<T>;

/**
* Return the child for given labels
* @param values Label values
Expand All @@ -511,6 +544,12 @@ export class Histogram<T extends string = string> {
*/
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 values Label values
Expand Down Expand Up @@ -598,6 +637,13 @@ export class Summary<T extends string = string> {
*/
reset(): void;

/**
* Return the child for given labels
* @param values Label values
* @return Configured summary with given labels
*/
labels(values: string[]): Summary.Internal<T>;

/**
* Return the child for given labels
* @param values Label values
Expand All @@ -612,6 +658,12 @@ export class Summary<T extends string = string> {
*/
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 values Label values
Expand Down
2 changes: 1 addition & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ exports.setValueDelta = function setValueDelta(
};

exports.getLabels = function (labelNames, args) {
if (typeof args[0] === 'object') {
if (typeof args[0] === 'object' && !Array.isArray(args[0])) {
return args[0];
}

Expand Down
7 changes: 7 additions & 0 deletions test/gaugeTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ describe.each([
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' });
});
it('should handle labels provided as an array', async () => {
instance.labels([200]).inc();

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