How to expose metrics to Prometheus from a Java (Spring boot) application

kramsiv1234 Source

My Spring-Boot application simply has a counter metric. I just don't know how to send this information to Prometheus. I am using Maven (build tool) and Spring Boot (Java).

mavenspring-bootinstrumentationgrafanaprometheus

Answers

answered 8 months ago Darrell Teague #1

Prometheus, like Graphite, is a time-series storage engine.

Grafana can then query Prometheus to generic graphics and alerts.

https://prometheus.io/docs/introduction/faq/

As the documentation cites, Prometheus, unlike other metrics storage systems, uses a (debatable) "pull" model.

This means that there is a (stand-alone) Prometheus server that must be downloaded/installed. This server then periodically makes HTTP GET requests (pull) to a list of application servers - such as a Java SpringBoot server to fetch (in-memory) stored metrics.

Ref: https://prometheus.io/docs/introduction/faq/#why-do-you-pull-rather-than-push?

Thus the (Spring Boot) application must expose a metrics end-point that the Prometheus server can pull from (default is /metrics).

Ref: https://github.com/prometheus/client_java

Thus there is much documentation available on Google but that is the (arguably convoluted) topology - along with arguments from the SoundCloud and Prometheus folks as to why a "pull" model is preferred over "push" as every other metrics framework employs.

answered 8 months ago Chids #2

For Intergrating Prometheus, add the following dependencies in your POM.XML

<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_spring_boot</artifactId>
    <version>0.1.0</version>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_servlet</artifactId>
    <version>0.1.0</version>
</dependency>
<dependency>
    <groupId>io.prometheus</groupId>
    <artifactId>simpleclient_hotspot</artifactId>
    <version>0.1.0</version>
</dependency>

In your SpringBoot Application Class, add the Annonation @EnablePrometheusEndpoint

In your Controller, you can define a Custom Counter

private static final Counter myCounter = Counter.build()
        .name("CounterName")
        .labelNames("status")
        .help("Counter desc").register();

In your service, you can have appropriate logic for your Counter which would be automatically pulled by Prometheus

@RequestMapping("/myService")
    public void endpoint() {
           String processingResult = yourLogic(myCounter);
            myCounter.labels("label1",processingResult).inc();
            }

comments powered by Disqus