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
…#96)

* feat: add option to disable summary collection in routeMetrics config
  • Loading branch information
Neriuzz authored Nov 28, 2023
1 parent e52d92c commit c0cb240
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 10 deletions.
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ See for details [docs](docs/api/fastify-metrics.imetricspluginoptions.md)

| Property | Type | Default Value |
| ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | --------------------------------------- |
| [enabled?](./docs/fastify-metrics.iroutemetricsconfig.enabled.md) | boolean | `true` |
| [enabled?](./docs/fastify-metrics.iroutemetricsconfig.enabled.md) | boolean \| { histogram: boolean, summary: 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 All @@ -149,6 +150,24 @@ See for details [docs](docs/api/fastify-metrics.imetricspluginoptions.md)
| [customLabels?](./fastify-metrics.iroutemetricsconfig.customlabels.md) | Record<string, string \| ((request: FastifyRequest, reply: FastifyReply) => string)> | `undefined` |
| [routeBlacklist?](./docs/fastify-metrics.iroutemetricsconfig.routeblacklist.md) | readonly string\[\] | `[]` |

#### Route metrics enabled

The `enabled` configuration option can be either a boolean which enables/disables generation of both histograms and summaries, or it can be set to an object that allows you to pick individually whether you want histograms or summaries to be generated, for example:

```
{
...
routeMetrics: {
enabled: {
histogram: true,
summary: false
}
}
}
```

would result in the library only generating histograms.

##### Route metrics overrides

You may override default metrics settings. You may provide overrides for two metrics tracking http request durations: `histogram` and `summary`.
Expand Down Expand Up @@ -205,7 +224,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 (subject to the `enabled` configuration option). Note suffixes like `_bucket`, `_sum`, `_count` are added automatically.

| metric | labels | description |
| -------------------------------------- | -------------------------------- | ----------------------------- |
Expand Down
58 changes: 58 additions & 0 deletions src/__tests__/route-metrics.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -626,4 +626,62 @@ describe('route metrics', () => {
);
});
});

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

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

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

await app.register(fastifyPlugin, {
endpoint: '/metrics',
routeMetrics: {
enabled: {
summary: 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',
])
);
});
});
});
18 changes: 16 additions & 2 deletions src/fastify-metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,22 @@ export class FastifyMetrics implements IFastifyMetrics {
[this.routeMetrics.labelNames.status]: statusCode,
...this.collectCustomLabels(request, reply),
};
metrics.sum(labels);
metrics.hist(labels);

if (this.options.routeMetrics.enabled instanceof Object) {
if (!(this.options.routeMetrics.enabled.summary === false)) {
metrics.sum(labels);
}
if (!(this.options.routeMetrics.enabled.histogram === false)) {
metrics.hist(labels);
}
done();
return;
}

if (!(this.options.routeMetrics.enabled === false)) {
metrics.sum(labels);
metrics.hist(labels);
}

done();
});
Expand Down
24 changes: 18 additions & 6 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,24 @@ export interface IRouteMetricsOverrides {
* @public
*/
export interface IRouteMetricsConfig {
/**
* Enables collection of fastify routes metrics response time.
*
* @defaultValue `false`
*/
enabled?: boolean;
enabled?:
| boolean
| {
/**
* Enables collection of fastify routes metrics response time via
* histogram.
*
* @defaultValue `true`
*/
histogram?: boolean;
/**
* Enables collection of fastify routes metrics response time via
* summary.
*
* @defaultValue `true`
*/
summary?: boolean;
};

/**
* Collect metrics only for registered routes. If `false`, then metrics for
Expand Down

0 comments on commit c0cb240

Please sign in to comment.