|
| 1 | +# spring-boot-with-metrics |
| 2 | + |
| 3 | +This demo app shows how a Spring Boot application can expose a Prometheus metrics endpoint for scraping. |
| 4 | + |
| 5 | +🚼 The app was initially created with [Spring Initializr][init] and then by following the [RESTful service tutorial on spring.io][rest-tutorial]. |
| 6 | + |
| 7 | +To run the app: |
| 8 | + |
| 9 | + mvn clean spring-boot:run |
| 10 | + |
| 11 | +This will expose Prometheus metrics at `/actuator/prometheus`. This is a simple `key value` listing of metrics. You can check them out using `curl`, for example: |
| 12 | + |
| 13 | + $ curl http://localhost:8080/actuator/prometheus |
| 14 | + # HELP process_uptime_seconds The uptime of the Java virtual machine |
| 15 | + # TYPE process_uptime_seconds gauge |
| 16 | + process_uptime_seconds 10.284 |
| 17 | + # HELP jvm_threads_states_threads The current number of threads having NEW state |
| 18 | + # TYPE jvm_threads_states_threads gauge |
| 19 | + jvm_threads_states_threads{state="runnable",} 9.0 |
| 20 | + jvm_threads_states_threads{state="blocked",} 0.0 |
| 21 | + jvm_threads_states_threads{state="waiting",} 11.0 |
| 22 | + ... |
| 23 | + # TYPE tomcat_sessions_alive_max_seconds gauge |
| 24 | + tomcat_sessions_alive_max_seconds 0.0 |
| 25 | + ... |
| 26 | + |
| 27 | +Once you make a request to the service at <http://localhost:8080/greeting>, you will also see a new metric exposed, `greeting_time_seconds` exposed, which shows the execution time of the `greeting` method: |
| 28 | + |
| 29 | + # HELP greeting_time_seconds Time taken to return greeting |
| 30 | + # TYPE greeting_time_seconds summary |
| 31 | + greeting_time_seconds{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",met |
| 32 | + hod="greeting",quantile="0.9",} 0.02097152 |
| 33 | + greeting_time_seconds_count{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="non |
| 34 | + e",method="greeting",} 1.0 |
| 35 | + greeting_time_seconds_sum{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none" |
| 36 | + ,method="greeting",} 0.021689345 |
| 37 | + # HELP greeting_time_seconds_max Time taken to return greeting |
| 38 | + # TYPE greeting_time_seconds_max gauge |
| 39 | + greeting_time_seconds_max{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",method="greeting",} 0.021689345 |
| 40 | + |
| 41 | + |
| 42 | +From the [Micrometer docs][timerdocs]: |
| 43 | + |
| 44 | +> All implementations of _Timer_ report at least the total time and count of events as separate time series. |
| 45 | +
|
| 46 | +Now we need to get these metrics into Prometheus. |
| 47 | + |
| 48 | +In another terminal, use Podman to start an ephemeral Prometheus in a container, and use the `host` networking option, which will allow the container to access the Spring Boot app on `localhost`: |
| 49 | + |
| 50 | + podman run --net=host \ |
| 51 | + -v prometheus.yml:/etc/prometheus/prometheus.yml \ |
| 52 | + prom/prometheus |
| 53 | + |
| 54 | +Check the Prometheus console at <http://localhost:9090>. |
| 55 | + |
| 56 | +- Go to _Targets_, you should see the Spring Boot app being scraped successfully. |
| 57 | + |
| 58 | +- On the _Graph_ page, you should be able to type in a metric from the application (e.g. `tomcat_sessions_active_current_sessions`, or `greeting_time_seconds`) and see the raw data, or plot a graph |
| 59 | + |
| 60 | +## Troubleshooting |
| 61 | + |
| 62 | +Inside the Prometheus container, you can check that you can access the Spring Boot metrics: |
| 63 | + |
| 64 | + $ podman exec -it <container-name> sh |
| 65 | + $$ wget -S -O - http://localhost:8080/actuator/prometheus |
| 66 | + |
| 67 | + |
| 68 | +[rest-tutorial]: https://spring.io/guides/gs/rest-service/ |
| 69 | +[init]: https://start.spring.io |
| 70 | +[timerdocs]: https://micrometer.io/docs/concepts#_timers |
0 commit comments