Skip to content

Commit

Permalink
feat: add option to disable summary collection in routeMetrics config
Browse files Browse the repository at this point in the history
  • Loading branch information
Neriuzz committed Nov 27, 2023
1 parent e52d92c commit 0f4efad
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ See for details [docs](docs/api/fastify-metrics.imetricspluginoptions.md)
| Property | Type | Default Value |
| ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | --------------------------------------- |
| [enabled?](./docs/fastify-metrics.iroutemetricsconfig.enabled.md) | boolean | `true` |
| [enableSummaries?](./docs/fastify-metrics.iroutemetricsconfig.enablesummaries.md) | boolean | `true` |
| [groupStatusCodes?](./docs/fastify-metrics.iroutemetricsconfig.groupstatuscodes.md) | boolean | `false` |
| [invalidRouteGroup?](./docs/fastify-metrics.iroutemetricsconfig.invalidroutegroup.md) | string | `'__unknown__'` |
| [methodBlacklist?](./docs/fastify-metrics.iroutemetricsconfig.methodblacklist.md) | readonly string\[\] | `['HEAD','OPTIONS','TRACE','CONNECT',]` |
Expand Down Expand Up @@ -205,7 +206,7 @@ await app.register(metricsPlugin, {

### HTTP routes metrics in Prometheus

The following table shows what metrics will be available in Prometheus. Note suffixes like `_bucket`, `_sum`, `_count` are added automatically.
The following table shows what metrics will be available in Prometheus. Note suffixes like `_bucket`, `_sum`, `_count` are added automatically and `summary` type metrics will only be available if `{ routeMetrics: { enableSummaries: true } }`

| metric | labels | description |
| -------------------------------------- | -------------------------------- | ----------------------------- |
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://json.schemastore.org/package.json",
"name": "fastify-metrics",
"version": "10.3.3",
"version": "10.4.0",
"description": "Prometheus metrics exporter for Fastify",
"keywords": [
"fastify-plugin",
Expand Down
57 changes: 57 additions & 0 deletions src/__tests__/route-metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,61 @@ describe('route metrics', () => {
);
});
});

describe(`{ routeMetrics: { enableSummaries: false } }`, () => {
let app = fastify();

afterEach(async () => {
await app.close();
});

beforeEach(async () => {
app = fastify();

await app.register(fastifyPlugin, {
endpoint: '/metrics',
routeMetrics: {
enabled: true,
enableSummaries: false,
customLabels: {
url: (request: FastifyRequest) => request.url,
},
},
});
app.get('*', async (_request, reply) => {
await reply.send('foo');
});
await app.ready();
});

test('summaries are not collected', async () => {
await expect(
app.inject({
method: 'GET',
url: '/test',
})
).resolves.toBeDefined();

const metrics = await app.inject({
method: 'GET',
url: '/metrics',
});

expect(typeof metrics.payload).toBe('string');

const lines = metrics.payload.split('\n');

expect(lines).toEqual(
expect.arrayContaining([
'http_request_duration_seconds_count{method="GET",route="*",status_code="200",url="/test"} 1',
])
);

expect(lines).toEqual(
expect.not.arrayContaining([
'http_request_summary_seconds_count{method="GET",route="*",status_code="200",url="/test"} 1',
])
);
});
});
});
5 changes: 4 additions & 1 deletion src/fastify-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export const DEFAULT_OPTIONS: IMetricsPluginOptions = {
clearRegisterOnInit: false,
routeMetrics: {
enabled: true,
enableSummaries: true,
},
defaultMetrics: {
enabled: true,
Expand Down Expand Up @@ -352,7 +353,9 @@ export class FastifyMetrics implements IFastifyMetrics {
[this.routeMetrics.labelNames.status]: statusCode,
...this.collectCustomLabels(request, reply),
};
metrics.sum(labels);
if (!(this.options.routeMetrics.enableSummaries === false)) {
metrics.sum(labels);
}
metrics.hist(labels);

done();
Expand Down
9 changes: 8 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,17 @@ export interface IRouteMetricsConfig {
/**
* Enables collection of fastify routes metrics response time.
*
* @defaultValue `false`
* @defaultValue `true`
*/
enabled?: boolean;

/**
* Enables computation of summaries for response times.
*
* @defaultValue 'true'
*/
enableSummaries?: boolean;

/**
* Collect metrics only for registered routes. If `false`, then metrics for
* unknown routes `/unknown-unregistered-route` will be collected as well.
Expand Down

0 comments on commit 0f4efad

Please sign in to comment.