From ab207ef74378d608923aa0dd1aed846c5d08b704 Mon Sep 17 00:00:00 2001 From: Zach Bjornson Date: Fri, 10 Mar 2023 20:55:34 -0800 Subject: [PATCH] bug: support array argument to labels() again This was undocumented and untested, and broke in 13.1.0. Fixes #499 --- CHANGELOG.md | 2 ++ README.md | 2 ++ index.d.ts | 52 +++++++++++++++++++++++++++++++++++++++++++++++ lib/util.js | 2 +- test/gaugeTest.js | 7 +++++++ 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 24678ef3..25bd7f08 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index d49de3a2..31958dbc 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/index.d.ts b/index.d.ts index 7bb196d8..6dfd015e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -258,6 +258,13 @@ export class Counter { */ get(): Promise>>; + /** + * 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 @@ -277,6 +284,12 @@ export class Counter { */ 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 @@ -373,6 +386,13 @@ export class Gauge { */ startTimer(labels?: LabelValues): (labels?: LabelValues) => number; + /** + * Return the child for given labels + * @param values Label values + * @return Configured gauge with given labels + */ + labels(values: string[]): Gauge.Internal; + /** * Return the child for given labels * @param values Label values @@ -392,6 +412,12 @@ export class Gauge { */ 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 @@ -497,6 +523,13 @@ export class Histogram { */ zero(labels: LabelValues): void; + /** + * Return the child for given labels + * @param values Label values + * @return Configured histogram with given labels + */ + labels(values: string[]): Histogram.Internal; + /** * Return the child for given labels * @param values Label values @@ -511,6 +544,12 @@ export class Histogram { */ labels(labels: LabelValues): Histogram.Internal; + /** + * 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 @@ -598,6 +637,13 @@ export class Summary { */ reset(): void; + /** + * Return the child for given labels + * @param values Label values + * @return Configured summary with given labels + */ + labels(values: string[]): Summary.Internal; + /** * Return the child for given labels * @param values Label values @@ -612,6 +658,12 @@ export class Summary { */ labels(labels: LabelValues): Summary.Internal; + /** + * 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 diff --git a/lib/util.js b/lib/util.js index c9e3cb3b..c9b47be6 100644 --- a/lib/util.js +++ b/lib/util.js @@ -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]; } diff --git a/test/gaugeTest.js b/test/gaugeTest.js index c7d9ec59..9e20887e 100644 --- a/test/gaugeTest.js +++ b/test/gaugeTest.js @@ -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' });