SkeLLLa / fastify-metrics

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

Unable to prefix names of default metrics in v9 as `prefix` option from v8.0.0 is not a thing in v9 #60

Closed bhovhannes closed 2 years ago

bhovhannes commented 2 years ago

Hello,

I upgraded fastify-metrics from 8.0.0 to 9.2.1. Changed plugin registration code to use new configuration options. Metrics are being collected and exposed under the route I want. However, I didn't found any way to add custom prefix to default metric names. prefix option available in 8.0.0 no longer works.

I am wondering if this is an expected behavior? And if so, what is the rationale behind this breaking change or maybe there are some workarounds?

With custom metrics everything is fine. I've included code and endpoint output in sections below.

After upgrade

Code:

app.register(require("fastify-metrics"), {
    prefix: 'app_', // this no longer works. I failed to find an equivalent for this
    routeBlacklist: [
        `/internal/metrics`,
        `/internal/health/readiness`
    ]
})

Endpoint output:

# HELP nodejs_version_info Node.js version info.
# TYPE nodejs_version_info gauge
nodejs_version_info{version="v16.15.0",major="16",minor="15",patch="0"} 1

Before upgrade

Code:

app.register(require("fastify-metrics"), {
    prefix: 'app_',
    blacklist: [
        `/internal/metrics`,
        `/internal/health/readiness`
    ],
    enableDefaultMetrics: true,
    enableRouteMetrics: true
})

Endpoint output:

# HELP nodejs_version_info Node.js version info.
# TYPE nodejs_version_info gauge
app_nodejs_version_info{version="v16.15.0",major="16",minor="15",patch="0"} 1
SkeLLLa commented 2 years ago

Configuration object in v9 changed and there's no compatibility between v8 and v9. See https://github.com/SkeLLLa/fastify-metrics/blob/master/docs/fastify-metrics.idefaultmetricsconfig.md, it extends DefaultMetricsCollectorConfiguration from prom-client where prefix is configured.

bhovhannes commented 2 years ago

After looking into sources of both fastify-metrics and prom-client I figured out how to configure what I want. Posting it here in case someone will face the same problems as me during upgrade.

So, if with v8 we wrote:

app.register(require(fastify-metrics), {
    prefix: 'app_',
    blacklist: [
        `/internal/metrics`,
        `/internal/health/readiness`
    ],
    enableDefaultMetrics: true,
    enableRouteMetrics: true
})

in v9 this should be written as:

app.register(require("fastify-metrics"), {
    defaultMetrics: {
        enabled: true, // this is the default value and can be omitted
        prefix: 'app_'
    },
    routeMetrics: {
        enabled: true, // this is the default value and can be omitted
        routeBlacklist: [
            `/internal/metrics`,
            `/internal/health/readiness`
        ],
        overrides: {
            histogram: {
                name: `app_http_request_duration_seconds`
            },
            summary: {
                name: `app_http_request_summary_seconds`
            }
        },
    }
})