Skip to content

Commit fd5ff1c

Browse files
tstelzerpvradarkp
authored andcommitted
feat: respect pruneAgedBuckets, update README
1 parent edfb999 commit fd5ff1c

File tree

4 files changed

+60
-13
lines changed

4 files changed

+60
-13
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ Additional options for **summary**:
6969
* **percentiles**: percentiles used for `http_request_duration_seconds` summary
7070
* **ageBuckets**: ageBuckets configures how many buckets we have in our sliding window for the summary
7171
* **maxAgeSeconds**: the maxAgeSeconds will tell how old a bucket can be before it is reset
72+
* **pruneAgedBuckets**: When enabled, timed out buckets will be removed entirely. By default, buckets are reset to 0.
7273

7374
### Transformation callbacks ###
7475

spec/index.spec.js

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ function extractBucket (instance, key) {
2020
}
2121
}
2222

23+
function getMetricCount (s) {
24+
return s.replace(/^#.*$\n|^$\n/gm, '').trim().split('\n').length;
25+
}
26+
2327
describe('index', () => {
2428
beforeEach(() => {
2529
promClient.register.clear();
@@ -342,7 +346,7 @@ describe('index', () => {
342346

343347
describe('usage of normalizePath()', () => {
344348

345-
it('normalizePath can be replaced gloablly', done => {
349+
it('normalizePath can be replaced globally', done => {
346350
const app = express();
347351
const original = bundle.normalizePath;
348352
bundle.normalizePath = () => 'dummy';
@@ -366,6 +370,40 @@ describe('index', () => {
366370
});
367371
});
368372

373+
it('respects pruneAgedBuckets', done => {
374+
const app = express();
375+
const metricsApp = express();
376+
const bundled = bundle({
377+
metricsApp,
378+
metricType: 'summary',
379+
includePath: true,
380+
maxAgeSeconds: 1,
381+
percentiles: [0.5],
382+
ageBuckets: 1,
383+
pruneAgedBuckets: true,
384+
});
385+
386+
app.use(bundled);
387+
388+
const agent = supertest(app);
389+
const metricsAgent = supertest(metricsApp);
390+
agent.get('/foo')
391+
.then(() => metricsAgent.get('/metrics'))
392+
.then(response => {
393+
expect(response.status).toBe(200);
394+
// up + bucket, sum, count
395+
expect(getMetricCount(response.text)).toBe(4);
396+
})
397+
.then(() => new Promise(r => setTimeout(r, 1010)))
398+
.then(() => metricsAgent.get('/metrics'))
399+
.then(response => {
400+
expect(response.status).toBe(200);
401+
// only up
402+
expect(getMetricCount(response.text)).toBe(1);
403+
})
404+
.finally(done);
405+
});
406+
369407
it('normalizePath function can be overridden', done => {
370408
const app = express();
371409
const instance = bundle({

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ function main(opts) {
112112
percentiles: opts.percentiles || [0.5, 0.75, 0.95, 0.98, 0.99, 0.999],
113113
maxAgeSeconds: opts.maxAgeSeconds,
114114
ageBuckets: opts.ageBuckets,
115-
registers: [opts.promRegistry]
115+
registers: [opts.promRegistry],
116+
pruneAgedBuckets: opts.pruneAgedBuckets
116117
});
117118
} else if (opts.metricType === 'histogram' || !opts.metricType) {
118119
return new promClient.Histogram({

types/index.d.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ declare namespace express_prom_bundle {
1717
type NormalizeStatusCodeFn = (res: Response) => number | string;
1818
type TransformLabelsFn = (labels: Labels, req: Request, res: Response) => void;
1919

20-
interface Opts {
20+
interface BaseOptions {
2121
autoregister?: boolean;
2222

2323
customLabels?: { [key: string]: any };
@@ -36,16 +36,6 @@ declare namespace express_prom_bundle {
3636

3737
excludeRoutes?: Array<string | RegExp>;
3838

39-
metricType?: 'summary' | 'histogram';
40-
41-
// https://github.com/siimon/prom-client#histogram
42-
buckets?: number[];
43-
44-
// https://github.com/siimon/prom-client#summary
45-
percentiles?: number[];
46-
maxAgeSeconds?: number;
47-
ageBuckets?: number;
48-
4939
metricsPath?: string;
5040
httpDurationMetricName?: string;
5141
promClient?: { collectDefaultMetrics?: DefaultMetricsCollectorConfiguration<RegistryContentType> };
@@ -65,6 +55,23 @@ declare namespace express_prom_bundle {
6555
};
6656
}
6757

58+
/** @see https://github.com/siimon/prom-client#summary */
59+
type SummaryOptions = BaseOptions & {
60+
metricType?: 'summary';
61+
percentiles?: number[];
62+
maxAgeSeconds?: number;
63+
ageBuckets?: number;
64+
pruneAgedBuckets?: boolean;
65+
}
66+
67+
/** @see https://github.com/siimon/prom-client#histogram */
68+
type HistogramOptions = BaseOptions & {
69+
metricType?: 'histogram';
70+
buckets?: number[];
71+
}
72+
73+
type Opts = SummaryOptions | HistogramOptions;
74+
6875
interface Middleware extends RequestHandler {
6976
metricsMiddleware: RequestHandler;
7077
}

0 commit comments

Comments
 (0)