'Micrometer and Prometheus registry scrape

Looking at Micrometer, I get the idea that the whole idea is the use the Metrics e.g. classes from "io.micrometer.core.instrument" (because it's a facade) and it ends up in the global registry for basic usage.

And the documentation says that for Prometheus output, one should then use the PrometheusMeterRegistry.scrape().

But it escapes me how Micrometer and Prometheus is connected. I have an @GET "metrics" endpoint which pretty much looks like

    Metrics.globalRegistry.counter("foo").increment();
    PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
    return Response.ok(prometheusRegistry.scrape()).build();

but nothing is shown, probably because the Prometheus registry is brand new (well, so it's in the Micrometer Prometheus section). How is the link made so that the Micrometer global registry is connected to the PrometheusMeterRegistry?



Solution 1:[1]

Do not use the Global registry. If you use it you need to register the registry you want to use. You only need to create your registry(ies) once on startup and use them throughout the whole app lifecycle.

If you want to use Prometheus, you can find an example in the docs: https://micrometer.io/docs/registry/prometheus

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jonatan Ivanov