Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 887
Nicklas Karlsson Avatar asked Sep 15 '25 22:09

Nicklas Karlsson


1 Answers

You can connect Metrics.globalRegistry to prometheusRegistry by calling globalRegistry.add(prometheusRegistry). E.g. you should re-write you code like this:

Metrics.globalRegistry.counter("foo").increment();
PrometheusMeterRegistry prometheusRegistry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);
Metrics.globalRegistry.add(prometheusRegistry); // <-- that's how you connect them
return Response.ok(prometheusRegistry.scrape()).build();
like image 185
nehaev Avatar answered Sep 19 '25 18:09

nehaev