Skip to content

Commit

Permalink
feat: respect pruneAgedBuckets, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
tstelzer authored and pvradarkp committed Jan 6, 2024
1 parent edfb999 commit fd5ff1c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Additional options for **summary**:
* **percentiles**: percentiles used for `http_request_duration_seconds` summary
* **ageBuckets**: ageBuckets configures how many buckets we have in our sliding window for the summary
* **maxAgeSeconds**: the maxAgeSeconds will tell how old a bucket can be before it is reset
* **pruneAgedBuckets**: When enabled, timed out buckets will be removed entirely. By default, buckets are reset to 0.

### Transformation callbacks ###

Expand Down
40 changes: 39 additions & 1 deletion spec/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ function extractBucket (instance, key) {
}
}

function getMetricCount (s) {
return s.replace(/^#.*$\n|^$\n/gm, '').trim().split('\n').length;
}

describe('index', () => {
beforeEach(() => {
promClient.register.clear();
Expand Down Expand Up @@ -342,7 +346,7 @@ describe('index', () => {

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

it('normalizePath can be replaced gloablly', done => {
it('normalizePath can be replaced globally', done => {
const app = express();
const original = bundle.normalizePath;
bundle.normalizePath = () => 'dummy';
Expand All @@ -366,6 +370,40 @@ describe('index', () => {
});
});

it('respects pruneAgedBuckets', done => {
const app = express();
const metricsApp = express();
const bundled = bundle({
metricsApp,
metricType: 'summary',
includePath: true,
maxAgeSeconds: 1,
percentiles: [0.5],
ageBuckets: 1,
pruneAgedBuckets: true,
});

app.use(bundled);

const agent = supertest(app);
const metricsAgent = supertest(metricsApp);
agent.get('/foo')
.then(() => metricsAgent.get('/metrics'))
.then(response => {
expect(response.status).toBe(200);
// up + bucket, sum, count
expect(getMetricCount(response.text)).toBe(4);
})
.then(() => new Promise(r => setTimeout(r, 1010)))
.then(() => metricsAgent.get('/metrics'))
.then(response => {
expect(response.status).toBe(200);
// only up
expect(getMetricCount(response.text)).toBe(1);
})
.finally(done);
});

it('normalizePath function can be overridden', done => {
const app = express();
const instance = bundle({
Expand Down
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ function main(opts) {
percentiles: opts.percentiles || [0.5, 0.75, 0.95, 0.98, 0.99, 0.999],
maxAgeSeconds: opts.maxAgeSeconds,
ageBuckets: opts.ageBuckets,
registers: [opts.promRegistry]
registers: [opts.promRegistry],
pruneAgedBuckets: opts.pruneAgedBuckets
});
} else if (opts.metricType === 'histogram' || !opts.metricType) {
return new promClient.Histogram({
Expand Down
29 changes: 18 additions & 11 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare namespace express_prom_bundle {
type NormalizeStatusCodeFn = (res: Response) => number | string;
type TransformLabelsFn = (labels: Labels, req: Request, res: Response) => void;

interface Opts {
interface BaseOptions {
autoregister?: boolean;

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

excludeRoutes?: Array<string | RegExp>;

metricType?: 'summary' | 'histogram';

// https://github.com/siimon/prom-client#histogram
buckets?: number[];

// https://github.com/siimon/prom-client#summary
percentiles?: number[];
maxAgeSeconds?: number;
ageBuckets?: number;

metricsPath?: string;
httpDurationMetricName?: string;
promClient?: { collectDefaultMetrics?: DefaultMetricsCollectorConfiguration<RegistryContentType> };
Expand All @@ -65,6 +55,23 @@ declare namespace express_prom_bundle {
};
}

/** @see https://github.com/siimon/prom-client#summary */
type SummaryOptions = BaseOptions & {
metricType?: 'summary';
percentiles?: number[];
maxAgeSeconds?: number;
ageBuckets?: number;
pruneAgedBuckets?: boolean;
}

/** @see https://github.com/siimon/prom-client#histogram */
type HistogramOptions = BaseOptions & {
metricType?: 'histogram';
buckets?: number[];
}

type Opts = SummaryOptions | HistogramOptions;

interface Middleware extends RequestHandler {
metricsMiddleware: RequestHandler;
}
Expand Down

0 comments on commit fd5ff1c

Please sign in to comment.