2

I would like to push metrics, e.g. processing time of an HTTP request or current heap size, to Prometheus through a Pushgateway in a Spring Boot application.

I'm struggling because there are many documents, either from Spring Metrics or Prometheus but neither of them address my specific problem.

The easiest way would be to use the @Timed annotation on a RestController class, as described here: http://projects.spring.io/spring-metrics/ However, I don't see, where I could configure an URL for the pushgateway and ask myself if the @Timed annotation just exports metrics to the /prometheus endpoint where they should be pulled from.

The Prometheus guides, however, tell me to use a simpleclient_pushgateway library: https://github.com/prometheus/client_java#exporting-to-a-pushgateway

Lastly, there's another simpleclient called simpleclient_spring_boot, which would be the third way to integrate Prometheus into Spring Boot.

Could you please tell me, how I can accomplish

A) pushing metrics from preferrably an annotation based approach for HTTP requests

B) some approach for JVM metrics every x seconds

C) to a configurable (in the application.yml file) pushgateway

Thank you very much

Michael Doubez
  • 5,937
  • 25
  • 39
user3105453
  • 1,881
  • 5
  • 32
  • 55

2 Answers2

0

This is the wrong way to approach things, the pushgateway isn't for per-instance metrics like heap size or request timings (https://prometheus.io/docs/practices/pushing/).

What you want to do is use simpleclient_spring_boot to expose a /metrics endpoint which Prometheus would scrape directly.

brian-brazil
  • 31,678
  • 6
  • 93
  • 86
  • Sorry I should have stated why I need the pushgateway :-) Our apps are deployed to Cloud Foundry. Polling them with Prometheus would result in different values, based on which instance currently responds to the request. That's why I like to push the metrics per-instance to the pushgateway and distinguish them in Grafana. – user3105453 Jun 15 '17 at 08:41
  • That's still not the recommended way to approach things. I believe the approach with Cloud Foundry is to do some service discovery, and then do something to add a HTTP header to talk to the right instance. – brian-brazil Jun 15 '17 at 12:50
  • @brian-brazil - Could you please explain why is it not the correct way to do it? Let's say that there are multiple Sprint boot services, which act as a mutliple workers. The amout of workers are configurale (docker scale) based on the system load. Each worker doesn't expose HTTP port, but just handle some job processing. Is it still the wrong approache? – kmualem Jul 02 '20 at 19:36
0

I could solve it only after adding this package in dependencies

io.prometheus:simpleclient_pushgateway

As it has been told in documentation https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html#actuator.metrics.export.prometheus

And configuration of metrics exporting as in this documentaions https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#application-properties.actuator.management.prometheus.metrics.export.pushgateway.base-url