diff --git a/index.d.ts b/index.d.ts index 1fd1eac9..01543060 100644 --- a/index.d.ts +++ b/index.d.ts @@ -649,6 +649,20 @@ export class Summary { */ labels(labels: LabelValues): Summary.Internal; + /** + * Initializes to 0 for given label values + * @param values Label values + * @return Configured summary with given labels + */ + initLabels(...values: string[]): Summary.Internal; + + /** + * Initializes to 0 for given label values + * @param labels Object with label keys and values + * @return Configured counter with given labels + */ + initLabels(labels: LabelValues): Summary.Internal; + /** * Remove metrics for the given label values * @param values Label values diff --git a/lib/summary.js b/lib/summary.js index 28fdb07a..ed44e2e6 100644 --- a/lib/summary.js +++ b/lib/summary.js @@ -103,6 +103,22 @@ class Summary extends Metric { return startTimer.call(this, labels)(); } + initLabels(...args) { + const labels = getLabels(this.labelNames, args); + validateLabel(this.labelNames, labels); + const hash = hashObject(labels, this.sortedLabelNames); + let summaryOfLabel = this.hashMap[hash]; + if (!summaryOfLabel) { + summaryOfLabel = { + labels, + td: new timeWindowQuantiles(this.maxAgeSeconds, this.ageBuckets), + count: 0, + sum: 0, + }; + this.hashMap[hash] = summaryOfLabel; + } + } + labels(...args) { const labels = getLabels(this.labelNames, args); validateLabel(this.labelNames, labels); diff --git a/test/summaryTest.js b/test/summaryTest.js index 306c9b8b..50a03963 100644 --- a/test/summaryTest.js +++ b/test/summaryTest.js @@ -307,6 +307,45 @@ describe.each([ expect(value.labels.method).toBe('GET'); }); }); + + it.only('can init labels to 0', async () => { + instance.initLabels('GET', '/test'); + + const { values } = await instance.get(); + expect(values).toHaveLength(3); + expect(values[0].labels.method).toEqual('GET'); + expect(values[0].labels.endpoint).toEqual('/test'); + expect(values[0].labels.quantile).toEqual(0.9); + expect(values[0].value).toEqual(0); + + expect(values[1].metricName).toEqual('summary_test_sum'); + expect(values[1].labels.method).toEqual('GET'); + expect(values[1].labels.endpoint).toEqual('/test'); + expect(values[1].value).toEqual(0); + + expect(values[2].metricName).toEqual('summary_test_count'); + expect(values[2].labels.method).toEqual('GET'); + expect(values[2].labels.endpoint).toEqual('/test'); + expect(values[2].value).toEqual(0); + }); + + it.only('can init labels passed as an object', async () => { + instance.initLabels({ method: 'GET' }); + + const { values } = await instance.get(); + expect(values).toHaveLength(3); + expect(values[0].labels.method).toEqual('GET'); + expect(values[0].labels.quantile).toEqual(0.9); + expect(values[0].value).toEqual(0); + + expect(values[1].metricName).toEqual('summary_test_sum'); + expect(values[1].labels.method).toEqual('GET'); + expect(values[1].value).toEqual(0); + + expect(values[2].metricName).toEqual('summary_test_count'); + expect(values[2].labels.method).toEqual('GET'); + expect(values[2].value).toEqual(0); + }); }); describe('remove', () => {