fastify-metrics
Advanced tools
Comparing version 10.1.0 to 10.2.0
@@ -60,2 +60,4 @@ import { FastifyInstance } from 'fastify'; | ||
private collectRouteMetrics; | ||
/** Get custom labels for route metrics */ | ||
private collectCustomLabels; | ||
/** | ||
@@ -62,0 +64,0 @@ * Initialize metrics in registries. Useful if you call `registry.clear()` to |
@@ -173,2 +173,3 @@ "use strict"; | ||
}; | ||
const customLabelNames = Object.keys(this.options.routeMetrics.customLabels ?? {}); | ||
const routeHist = new this.deps.client.Histogram({ | ||
@@ -184,2 +185,3 @@ ...this.options.routeMetrics.overrides?.histogram, | ||
labelNames.status, | ||
...customLabelNames, | ||
], | ||
@@ -197,2 +199,3 @@ }); | ||
labelNames.status, | ||
...customLabelNames, | ||
], | ||
@@ -243,2 +246,3 @@ }); | ||
[this.routeMetrics.labelNames.status]: statusCode, | ||
...this.collectCustomLabels(request, reply), | ||
}; | ||
@@ -250,2 +254,16 @@ metrics.sum(labels); | ||
} | ||
/** Get custom labels for route metrics */ | ||
collectCustomLabels(request, reply) { | ||
const customLabels = this.options.routeMetrics.customLabels ?? {}; | ||
const labels = {}; | ||
for (const [labelName, labelValue] of Object.entries(customLabels)) { | ||
if (typeof labelValue === 'function') { | ||
labels[labelName] = labelValue(request, reply); | ||
} | ||
else { | ||
labels[labelName] = labelValue; | ||
} | ||
} | ||
return labels; | ||
} | ||
/** | ||
@@ -252,0 +270,0 @@ * Initialize metrics in registries. Useful if you call `registry.clear()` to |
@@ -1,2 +0,2 @@ | ||
import { FastifyRequest, HTTPMethods, RouteOptions } from 'fastify'; | ||
import { FastifyReply, FastifyRequest, HTTPMethods, RouteOptions } from 'fastify'; | ||
import client, { DefaultMetricsCollectorConfiguration, HistogramConfiguration, SummaryConfiguration } from 'prom-client'; | ||
@@ -174,2 +174,17 @@ /** | ||
invalidRouteGroup?: string; | ||
/** | ||
* Custom labels to add to metrics | ||
* | ||
* @example | ||
* | ||
* ```ts | ||
* customLabels: { | ||
* myLabel: 'my-value', | ||
* myLabel2: (request, reply) => request.headers['x-my-header'], | ||
* } | ||
* ``` | ||
* | ||
* @defaultValue `undefined` | ||
*/ | ||
customLabels?: Record<string, string | ((request: FastifyRequest, reply: FastifyReply) => string)>; | ||
/** Metric configuration overrides */ | ||
@@ -176,0 +191,0 @@ overrides?: IRouteMetricsOverrides; |
{ | ||
"$schema": "https://json.schemastore.org/package.json", | ||
"name": "fastify-metrics", | ||
"version": "10.1.0", | ||
"version": "10.2.0", | ||
"description": "Prometheus metrics exporter for Fastify", | ||
@@ -6,0 +6,0 @@ "keywords": [ |
@@ -140,11 +140,12 @@ # fastify-metrics | ||
| Property | Type | Default Value | | ||
| ------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------- | --------------------------------------- | | ||
| [enabled?](./docs/fastify-metrics.iroutemetricsconfig.enabled.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',]` | | ||
| [overrides?](./docs/fastify-metrics.iroutemetricsconfig.overrides.md) | [IRouteMetricsOverrides](./docs/fastify-metrics.iroutemetricsoverrides.md) | | ||
| [registeredRoutesOnly?](./docs/fastify-metrics.iroutemetricsconfig.registeredroutesonly.md) | boolean | `true` | | ||
| [routeBlacklist?](./docs/fastify-metrics.iroutemetricsconfig.routeblacklist.md) | readonly string\[\] | `[]` | | ||
| Property | Type | Default Value | | ||
| ------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------- | --------------------------------------- | | ||
| [enabled?](./docs/fastify-metrics.iroutemetricsconfig.enabled.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',]` | | ||
| [overrides?](./docs/fastify-metrics.iroutemetricsconfig.overrides.md) | [IRouteMetricsOverrides](./docs/fastify-metrics.iroutemetricsoverrides.md) | | | ||
| [registeredRoutesOnly?](./docs/fastify-metrics.iroutemetricsconfig.registeredroutesonly.md) | boolean | `true` | | ||
| [customLabels?](./fastify-metrics.iroutemetricsconfig.customlabels.md) | Record<string, string \| ((request: FastifyRequest, reply: FastifyReply) => string)> | `undefined` | | ||
| [routeBlacklist?](./docs/fastify-metrics.iroutemetricsconfig.routeblacklist.md) | readonly string\[\] | `[]` | | ||
@@ -173,4 +174,4 @@ ##### Route metrics overrides | ||
}, | ||
} | ||
} | ||
}, | ||
}, | ||
}); | ||
@@ -181,15 +182,16 @@ ``` | ||
| Property | Type | Default value | | ||
| ----------------------------------------------------------------- | ------ | --------------- | | ||
| [method?](./docs/fastify-metrics.iroutelabelsoverrides.method.md) | string | `'method'` | | ||
| [route?](./docs/fastify-metrics.iroutelabelsoverrides.route.md) | string | `'route'` | | ||
| [status?](./docs/fastify-metrics.iroutelabelsoverrides.status.md) | string | `'status_code'` | | ||
| Property | Type | Default value | | ||
| -------------------------------------------------------------------------- | -------------------------------------- | --------------- | | ||
| [getRouteLabel?](./fastify-metrics.iroutelabelsoverrides.getroutelabel.md) | (request: FastifyRequest) => string | `undefined` | | ||
| [method?](./docs/fastify-metrics.iroutelabelsoverrides.method.md) | string | `'method'` | | ||
| [route?](./docs/fastify-metrics.iroutelabelsoverrides.route.md) | string | `'route'` | | ||
| [status?](./docs/fastify-metrics.iroutelabelsoverrides.status.md) | string | `'status_code'` | | ||
###### Request durations summary | ||
| Property | Type | Default value | | ||
| ----------------------------------------------------------------------- | ---------- | ---------------------------------------- | | ||
| [name?](./docs/fastify-metrics.isummaryoverrides.name.md) | string | `'http_request_summary_seconds'` | | ||
| Property | Type | Default value | | ||
| ----------------------------------------------------------------------- | ---------- | --------------------------------------- | | ||
| [name?](./docs/fastify-metrics.isummaryoverrides.name.md) | string | `'http_request_summary_seconds'` | | ||
| [help?](./docs/fastify-metrics.isummaryoverrides.help.md) | string | `'request duration in seconds summary'` | | ||
| [percentiles?](./docs/fastify-metrics.isummaryoverrides.percentiles.md) | number\[\] | `[0.5, 0.9, 0.95, 0.99]` | | ||
| [percentiles?](./docs/fastify-metrics.isummaryoverrides.percentiles.md) | number\[\] | `[0.5, 0.9, 0.95, 0.99]` | | ||
@@ -196,0 +198,0 @@ ###### Request durations histogram |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52784
702
241
98084