Code Monkey home page Code Monkey logo

spring-boot-with-metrics's Introduction

spring-boot-with-metrics

This demo app shows how a Spring Boot application can expose a Prometheus metrics endpoint for scraping.

๐Ÿšผ The app was initially created with Spring Initializr and then by following the RESTful service tutorial on spring.io.

To run

To run the app:

mvn clean spring-boot:run

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:

$ curl http://localhost:8080/actuator/prometheus
# HELP process_uptime_seconds The uptime of the Java virtual machine
# TYPE process_uptime_seconds gauge
process_uptime_seconds 10.284
# HELP jvm_threads_states_threads The current number of threads having NEW state
# TYPE jvm_threads_states_threads gauge
jvm_threads_states_threads{state="runnable",} 9.0
jvm_threads_states_threads{state="blocked",} 0.0
jvm_threads_states_threads{state="waiting",} 11.0
...
# TYPE tomcat_sessions_alive_max_seconds gauge
tomcat_sessions_alive_max_seconds 0.0
...    

Timing a custom method

The method in the application which responds to the REST request has been annotated with @Timed, so Micrometer will capture the execution time of this method:

@GetMapping("/greeting")
@Timed(value = "greeting.time", description = "Time taken to return greeting",
        percentiles = {0.5, 0.90})
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

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:

# HELP greeting_time_seconds Time taken to return greeting
# TYPE greeting_time_seconds summary
greeting_time_seconds{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",met
hod="greeting",quantile="0.9",} 0.02097152
greeting_time_seconds_count{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="non
e",method="greeting",} 1.0
greeting_time_seconds_sum{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none"
,method="greeting",} 0.021689345
# HELP greeting_time_seconds_max Time taken to return greeting
# TYPE greeting_time_seconds_max gauge
greeting_time_seconds_max{class="com.tutorialworks.demos.springbootwithmetrics.GreetingController",exception="none",method="greeting",} 0.021689345

From the Micrometer docs:

All implementations of Timer report at least the total time and count of events as separate time series.

Getting metrics into Prometheus

Now we need to get these metrics into Prometheus.

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:

podman run --net=host \
    -v ./prometheus.yml:/etc/prometheus/prometheus.yml:Z \
    prom/prometheus

Check the Prometheus console at http://localhost:9090.

  • Go to Targets, you should see the Spring Boot app being scraped successfully.

  • 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

Troubleshooting

Inside the Prometheus container, you can check that you can access the Spring Boot metrics:

$ podman exec -it <container-name> sh
$$ wget -S -O - http://localhost:8080/actuator/prometheus

spring-boot-with-metrics's People

Contributors

monodot avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.