2

I have an app that contains 2 dozen of spring batch cron jobs.There is no rest controller as it is an analytics app and it runs daily and read data from db, process it, and then store aggregated data in another db.I want to have spring inbuilt metrics on the jobs using micrometer and push them to Prometheus .As my app is not a webserver app, so still micrometer will be publishing results on HOST:8080? Will actuator automatically start a new server on HOST:8080?or do we need to have application server running on 8080?

My understanding is that actuator and application server can run of different ports as these are different processes ?Even if application server is there or not, actuator should be able to either use same port as application server port, or it can use different port?

So if my application is not a webserver based app, still I can access metrics at localhost:8080/actuator/ and publish to Prometheus?

user124
  • 423
  • 2
  • 7
  • 26
  • But if it's a batch it will only be availalbe for a short time. Checkout https://github.com/spring-projects/spring-batch/blob/master/spring-batch-samples/src/main/java/org/springframework/batch/sample/metrics/PrometheusConfiguration.java – Simon Martinelli Mar 16 '21 at 16:52
  • What do you mean by short time?Also, as i refer online,some source said,i just need to add dependency on artifacts. micrometer-core and micrometer-registry-prometheus and nothing else,i assume it will automatically push to /actuator/promethous and then i can scraping can be done from there.Why do we need PrometheusConfiguration class? – user124 Mar 16 '21 at 18:23
  • It's a batch application, isn't it? How do you start it? – Simon Martinelli Mar 16 '21 at 18:58
  • yes its a spring batch application.When i run main class, then all the jobs will be loaded and since all jobs are scheduled daily based on cron expression, so the main will be running forever continuous as there are always jobs to execute in future?Did i miss anything? – user124 Mar 16 '21 at 19:51
  • Now I understand. If you only add Actuator there will be no Webserver. You have to add the web starter too – Simon Martinelli Mar 16 '21 at 19:57
  • okay.so that web starter will start server for actuator to run, and then that end point can be scraped by Prometheus, correct? – user124 Mar 16 '21 at 20:00
  • Exactly. That's how it works – Simon Martinelli Mar 16 '21 at 20:20
  • @SimonMartinelli, as I mentioned below in comment, that there are 1-2 rest api also that are exposed. So can see my last comment and see if you answer it?Shall I use actuator if there are 2 rest endpoints exposed, that are just for health check of app,and rest all are batch jobs or shall i use pushgateway? – user124 Mar 17 '21 at 18:41

2 Answers2

0

Prometheus is a pull-based system, meaning you give it a URL from your running application and it will go pull metrics from it. If your application is an ephemeral batch application, it does not make sense to make it a webapp for the only sake of exposing a URL for a short period of time. That's exactly why Prometheus folks created the Push gateway, see When to use the Push Gateway.

Now with is in mind, in order for your batch applications to send metrics to Prometheus, you need:

  • A Prometheus server
  • A Pushgateway server
  • An optional metrics dashbaord (Grafana or similar, Prometheus also provides a built-in UI)
  • Make your batch applications push metrics to the gateway

A complete example with this setup can be found in the Batch metrics with Micrometer. This example is actually similar to your use case. It shows two jobs scheduled to run every few seconds which store metrics in Micrometer's main registry and a background task that pushes metrics regularly from Micrometer's registry to Prometheus's gateway.

Another option is to use the RSocket protocol, which is provided for free if you use Spring Cloud Dataflow.

For Spring Boot, there are no actuator endpoints for Spring Batch, please refer to Actuator endpoint for Spring Batch for more details about the reasons about this decision.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • but https://docs.spring.io/spring-batch/docs/current/reference/html/monitoring-and-metrics.html, it will automatically get metrics and if actuator is set up, then even if my app is not a web server app, the actuator should give me end point to monitor health and all and on same end point I can get batch metrics also as per this doc? – user124 Mar 17 '21 at 12:57
  • Why do you need the actuator at all? A spring boot based batch application (non webapp) can use micrometer and send metrics to a backend without the actuator dependency and without exposing any endpoint. You are probably not seeing metrics because the global registry in micrometer is an empty composite by default. You need to add at least one registry to retain metrics, see https://stackoverflow.com/questions/64869472/spring-batch-and-boot-micrometer. – Mahmoud Ben Hassine Mar 17 '21 at 13:06
  • i understand you point. This is some code that I am trying to understand and now just now I saw , in the particular branch I need to work, it has webserver, in which it exposes just one API of (/pingCheck), in which all the app replies "I am awake". So now,I have a 2 dozen of batch and there one one pinCheck(API), what do you suggest here, does it make sense to expose actuator or shall I use PushGteway? – user124 Mar 17 '21 at 17:07
  • Please also see once https://stackoverflow.com/questions/66678874/how-to-publish-spring-batch-metrics-to-prometheus-gateway. Its extending to above, but i though it is separate.Also for above comment if u can suggest, it would be of great help – user124 Mar 17 '21 at 18:19
  • `does it make sense to expose actuator or shall I use PushGteway?`: as mentioned in the answer, there are no actuator endpoints for spring batch. So unless you want to develop them yourself, you need to use the pushgatewy or rsocket way. – Mahmoud Ben Hassine Mar 18 '21 at 06:05
0

@Mahmoud I think there are valid use cases for exposing the health endpoints optionally. The first question to consider is when we say a batch operation runs for a short time, how short is that time - a few minutes? I agree there's no need; but how about jobs that run for a few hours? it's important for some jobs that we get metrics especially when such jobs are bound by a business SLA and the operator needs to know if the job is processing at the required operations per second, has the right connection pool size etc.

There are also a variety of implementation details of the running platform - we can use Spring Batch without SCDF, not be in control of the Prometheus gateway to be able to use push, run in a cloud where Istio will pull the metrics automatically etc.

For the OPs question, in general one can run a spring batch job in web instance, as far as I have used Spring Batch with a web instance, the application does shut down after job completion.

Zubin Kavarana
  • 190
  • 2
  • 11