I have activated the spring actuator prometheus endpont /actuator/prometheus
. By adding the dependencies for micrometer and actuator and enabled prometheus endpont. How can i get there custom metrics?
Asked
Active
Viewed 9,290 times
13

Dullimeister
- 504
- 1
- 7
- 18
1 Answers
18
You'll need to register your metrics with the Micrometer Registry.
The following example creates the metrics in the constructor. The Micrometer Registry is injected as a constructor parameter:
@Component
public class MyComponent {
private final Counter myCounter;
public MyComponent(MeterRegistry registry) {
myCounter = Counter
.builder("mycustomcounter")
.description("this is my custom counter")
.register(registry);
}
public String countedCall() {
myCounter.increment();
}
}
Once this is available, you'll have a metric mycustomcounter_total in the registry available in the /prometheus URL. The suffix "total" is added to comply with the Prometheus naming conventions.

ahus1
- 5,782
- 24
- 42
-
And note that `Counter` will be `io.micrometer.core.instrument.Counter`. It seems that Prometheus Java Client for Spring Boot doesn't support Spring Boot 2 (at least by the time of writing): https://github.com/prometheus/client_java/issues/405#issuecomment-409817031 – Touko Nov 28 '18 at 08:49