Observing the Reliability of your Java Apps and Services with Spring Boot, Micrometer, Prometheus & Reliably
Tags: devops, sre, prometheus, spring
Published: 2021-08-04 14:49:16

![Photo by [Marten Newhall](https://unsplash.com/@laughayette?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/magnifying-glass?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)](https://cdn-images-1.medium.com/max/9216/1*bd7UX4O4EL3clA4mFxeLEw.jpeg)

Here at Reliably we are huge fans of [Spring Boot](https://spring.io/projects/spring-boot) and the [Micrometer](https://micrometer.io) dimensional metrics instrumentation library for providing the rich set of possible metrics that can be a great foundation for the Service Level Indicators that provide coverage for your [Reliably Service Level Objectives as Code](https://reliably.com/docs/getting-started/slos/).

As of Spring Boot 2.0, Micrometer became the default instrumentation library for the huge range of Spring Boot applications, from monoliths to microservices. With Micrometer bakes in by default, we started to explore just how easy it would be to bring Reliably's "Developer-First" SLOs to bear on your Spring Boot apps and services.

In this article we share our findings including how Reliably really can work "Bootifully" (TM, Josh Long :) ) with all your Spring Boot apps and services, out of the box and with no extra code required!
>  NOTE: The full coded sample for this article is [available on GitHub](https://github.com/Lawouach/spring-boot-prometheus-reliably).

## The Setup: Spring Boot, Prometheus & Reliably

The exercise we wanted to conduct was to show how you could define and collaborate on Reliably Service Level Objectives that were measuring the availability of a simple Spring Boot service. To do this we needed three pieces in the mix:

![](https://cdn-images-1.medium.com/max/3576/1*6E8cWoNwVH_ogyCjZml-lw.png)

With this approach, Spring Boot and Micrometer would push dimensional metrics to Prometheus. Then Reliably would use Prometheus queries to collate Service Level Indicators to back the Service Level Objectives being observed. Simple? Actually, it is…
>  NOTE: We chose Prometheus for this particular article but we could just have easily picked one of the other tools supported by Micrometer and Reliably, such as [DataDog](https://dev.to/reliably/bringing-reliability-closer-to-you-with-reliably-and-datadog-2jbm).

## Sourcing Metrics from our Spring Boot Service

To get things built as quickly and easily as possible, we used the [Spring Initializr](https://start.spring.io) to generate a very simple HTTP-based application that did nothing more than provide a default root / response of "Greetings from Spring Boot!" to provide the service that we'd look to observe our SLOs on.

As mentioned in the introduction, by default Spring Boot applications come with all the power of Micrometer by default, the only thing we needed to do was make sure that our Spring Boot service's metrics could be scraped by Prometheus by adding a single line to our service's application.properties:
{% gist https://gist.github.com/russmiles/e3802ae1aff84b591e2aaf683fe0f0ad.js %}

## Setting up Prometheus to Scrape the Metrics

Next we added a simple Scraper configuration to our instance of Prometheus to periodically grab all the Micrometer metrics for our Spring Boot service from the endpoint we configured in the previous step:
{% gist https://gist.github.com/russmiles/6958dbe1416948c0d7469b818b34b71d.js %}
With this config in place, Prometheus will grab the metrics from our Spring Boot service every 15 seconds.

## Creating and Observing the Reliably SLOs as Code

The final step was for us to use the new Prometheus support in the Reliably CLI v0.23.0 to create our SLO with an SLI implemented as an appropriate Prometheus query:
{% gist https://gist.github.com/russmiles/e9ac568845cc60fa162faf90af891bd2.js %}

This SLO definition includes a Service Level Indicator (SLI) that queries Prometheus for the appropriate metrics to help us judge that the SLO is being met, or not. 

## Pushing your SLIs to Reliably

With your Service Level Objective, and corresponding Prometheus-driven Service Level Indicators, in hand, you can now begin pushing the SLO and SLIs over time to Reliably using the `reliably slo agent` command:

```bash
$ reliably slo agent -i 10
INFO[0000] --- starting slo indicator agent ---         
INFO[0000] getting indicators for objective: [name='99% of requests return 2xx over last 1 hour', service='my spring boot app'] 
INFO[0000] indicator percent: [98.91] for objective: [name='99% of requests return 2xx over last 1 hour', service='my spring boot app'] 
```

## Observing your SLOs from your Command Line

With our Spring Boot service running and receiving requests, our Prometheus instance scraping the available metrics, and Reliably monitoring the above SLO, we have successfully defined and can observe a Java Spring Boot application's reliability with as little code as possible! The final step is to observe the status of your SLO using the `reliably slo report -w -m reliably.yaml` command:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iwnnrp67vr9amv07re8c.png)

## Where to go next: We need … You!

Our goal is to shift reliability left by making it as easy as possible for you and your team to be able to define, observe and learn how to make your system's reliable.  As such we are constantly looking to make it easier for people to collaborate, code and observe Service Level Objectives and Indicators for their own bespoke needs. 

You can check out all the different tools that we currently integrate with in [our docs](https://reliably.com/docs/), but if there's something you don't see then please [get in touch](https://reliably.com/contact/) or maybe even raise a ticket and PR yourself on our [free and open source Reliably CLI project](https://github.com/reliablyhq/cli).