SkeLLLa / fastify-metrics

Prometheus metrics exporter for Fastify
https://savelife.in.ua/en/donate-en/
MIT License
99 stars 33 forks source link

High cardinality of labels in prometheus metrics - fastify-metrics v6.x #99

Open fritzfs opened 11 months ago

fritzfs commented 11 months ago

Is there a way to enable some sort of heuristic for grouping labels or manually specifying routes because with default configuration, my metrics is becoming too big? It has records for each possibly route /api/v1/projects/{id}.

For version 6.x.

Here are the records just for 2 requests - /api/v1/projects/2 and /api/v1/projects/3.

http_request_duration_seconds_bucket{le="0.05",method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_bucket{le="0.1",method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_bucket{le="0.5",method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_bucket{le="1",method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_bucket{le="3",method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_bucket{le="5",method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_bucket{le="10",method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_bucket{le="+Inf",method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_sum{method="GET",route="/GET/api/v1/projects/2",status_code="404"} 0.000185834
http_request_duration_seconds_count{method="GET",route="/GET/api/v1/projects/2",status_code="404"} 1
http_request_duration_seconds_bucket{le="0.05",method="GET",route="/GET/api/v1/projects/3",status_code="404"} 1
http_request_duration_seconds_bucket{le="0.1",method="GET",route="/GET/api/v1/projects/3",status_code="404"} 1
http_request_duration_seconds_bucket{le="0.5",method="GET",route="/GET/api/v1/projects/3",status_code="404"} 1
http_request_duration_seconds_bucket{le="1",method="GET",route="/GET/api/v1/projects/3",status_code="404"} 1
http_request_duration_seconds_bucket{le="3",method="GET",route="/GET/api/v1/projects/3",status_code="404"} 1
http_request_duration_seconds_bucket{le="5",method="GET",route="/GET/api/v1/projects/3",status_code="404"} 1
http_request_duration_seconds_bucket{le="10",method="GET",route="/GET/api/v1/projects/3",status_code="404"} 1
http_request_duration_seconds_bucket{le="+Inf",method="GET",route="/GET/api/v1/projects/3",status_code="404"} 1

Thank you!

SkeLLLa commented 10 months ago

Hi. Since v6 was a long time ago, I don't exactly remember what capabilities are present in fastify v3. However you may try to check out how it's done in recent version. There metrics are created for routes reported in onRoute hook and there urls are coming in format like you've mentioned: /api/v1/projects/{id}.

Also you may try to check if it's possible to do what you need through route config - it might be that there's config option to override label.

In worst case scenario you can just disable default route metrics collection, and copy-paste part of code from this lib and add your own metrics and report them instead with correct labels.