DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Trending

  • No More Cheap Claude: 4 First Principles of Token Economics in 2026
  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • Implementing Observability in Distributed Systems Using OpenTelemetry
  1. DZone
  2. Coding
  3. Frameworks
  4. Monitoring Spring Boot Applications With Prometheus and Grafana

Monitoring Spring Boot Applications With Prometheus and Grafana

At my current project we've been building three different applications. All three applications are based on Spring Boot, but have very different workloads. T...

By 
Jeroen Reijn user avatar
Jeroen Reijn
DZone Core CORE ·
Feb. 01, 21 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
14.3K Views

Join the DZone community and get the full member experience.

Join For Free

At my current project, we've been building three different applications. All three applications are based on Spring Boot but have very different workloads. They've all reached their way to the production environment and have been running steadily for quite some time now. We do regular (weekly basis) deployments of our applications to production with bug fixes, new features, and technical improvements. The organisation has a traditional infrastructure workflow in the sense that deployments to the VM instances on acceptance and production happen via the (remote hosting) provider.

The hosting provider is responsible for the uptime of the applications and therefore they keep an eye on system metrics through the usage of their own monitoring system. As a team, we are able to look in the system, but it doesn't say much about the internals of our application. In the past, we've asked to add some additional metrics to their system, but the system isn't that easy to configure with additional metrics. To us as a team runtime statistics about our applications and the impact our changes have on the overall health are crucial to understanding the impact of our work.

Spring Boot Actuator and Micrometer

If you've used Spring Boot before you've probably heard of Spring Boot Actuator. Actuator is a set of features that help you monitor and manage your application when it moves away from your local development environment and onto a test, staging or production environment. It helps expose operational information about the running application - health, metrics, audit entries, scheduled task, env settings, etc. You can query the information via either several HTTP endpoints or JMX beans. Being able to view the information is useful, but it's hard to spot trends or see the behaviour over a period of time.

In our project, we're using Spring Boot 2 and my team was pretty excited that we were able to start using Micrometer, an instrumentation library powering the delivery of application metrics. Micrometer is the default metrics library in Spring Boot 2 and it doesn't just give you metrics from your Spring application, but can also deliver JVM metrics (garbage collection and memory pools, etc) and also metrics from the application container. Micrometer has several different libraries that can be included to ship metrics to different backends and has support for Prometheus, Netflix Atlas, CloudWatch, Datadog, Graphite, Ganglia, JMX, Influx/Telegraf, New Relic, StatsD, SignalFx, and Wavefront.

Because we didn't have a lot of control over the way our applications were deployed we looked at the several different backends supported by a micrometer. Most of the above backends work by pushing data out to a remote (cloud service). Since the organisation we work for doesn't allow us to push this 'sensitive' data to a remote party we looked at self-hosted solutions. We started with looking into Prometheus (and Grafana) and we soon learned that it was really easy to get a monitoring system up and running within an hour. In the rest of this post, I'll show you how easy it is to start monitoring Spring Boot applications with Prometheus and Grafana.

Prometheus

Prometheus is an open-source system monitoring and alerting toolkit originally built at SoundCloud and now part of the Cloud Native Computing Foundation. Some of the features that appealed to us were:

  • No reliance on distributed storage; single server nodes are autonomous.
  • Time-series collection happens via a pull model over HTTP.
  • Targets are discovered via service discovery or static configuration.
  • Multiple modes of graphing and dashboarding support.

Prometheus uses a file called prometheus.yml as its main configuration file. Within the configuration file, you can specify where it can find the targets it needs to monitor, specify recording rules and alerting rules.

The following example shows a configuration with a set of statics targets for test and staging environments. You can decide to monitor all environments within one Prometheus instance, but you could of course also use a separate Prometheus instance for monitoring just the production environment.

YAML
 




xxxxxxxxxx
1
29


 
1
global:
2
  scrape_interval:   10s # By default, scrape targets every 10 seconds.
3
 
           
4
  # Attach these labels to any time series or alerts when communicating with
5
  # external systems (federation, remote storage, Alertmanager).
6
  external_labels:
7
    monitor: 'appteam-monitor'
8
 
           
9
scrape_configs:
10
  - job_name:       'applicationX'
11
 
           
12
    # Override the global default and scrape targets from this job every 15 seconds.
13
    scrape_interval: 15s
14
    # Path can be different depending on your Spring Boot configuration
15
    metrics_path: '/actuator/prometheus'
16
    scheme: https
17
    # Disable tls check due to self signed certificates
18
    tls_config:
19
        insecure_skip_verify: true
20
    static_configs:
21
      - targets: ['api1.test.somedomain.com']
22
        labels:
23
          group: 'test'
24
          application: 'app1'
25
 
           
26
      - targets: ['api1.staging.somedomain.com','api2.staging.somedomain.com']
27
        labels:
28
          group: 'staging'
29
          application: 'app1'



As you can see the configuration is pretty simple. You can add specific labels to the targets which can, later on, be used for querying, filtering, and creating a dashboard based upon the information stored within Prometheus. If you want to get started quickly with Prometheus and have Docker on your environment you can use the official docker Prometheus image by running the following command and provide a custom configuration from your host machine by running:

Shell
 




x


 
1
$ docker run -p 9090:9090 -v /tmp/prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus:v2.24.1



In the above example, we bind-mount the main Prometheus configuration file from the host system.

To get an overview of the toolset surrounding Prometheus, take a look at the following diagram.

Prometheus Server diagram
(Source: https://prometheus.io/docs/introduction/overview/)

Grafana

So what is Grafana and what role does it play in our monitoring stack?

Grafana allows you to query, visualize, alert on and understand your metrics no matter where they are stored. Create, explore, and share dashboards with your team and foster a data-driven culture.

The cool thing about Grafana is (next to the beautiful UI) that it's not tied to Prometheus as its single data source like for instance Kibana is tied to Elasticsearch. Grafana can have many different data sources like AWS Cloudwatch, Elasticsearch, InfluxDB, Prometheus, etc. This makes it a very good option for creating a monitoring dashboard. Grafana talks to Prometheus by using the PromQL query language.

For Grafana there is also an official Docker image available for you to use. You can get Grafana up and running with a simple command.

Shell
 




x


 
1
$ docker run -p 3000:3000 grafana/grafana:5.4.3



Now if we connect Grafana with Prometheus as the data source and install this excellent JVM Micrometer dashboard into Grafana we can instantly start monitoring our Spring Boot application. You will end up with a pretty mature dashboard that lets you switch between different instances of your application.

Spring Boot 2.1 Statistics UI

If you want to start everything all at once you can easily use docker-compose.

YAML
 




x


 
1
version: "3"
2
services:
3
  app:
4
    image: monitoring-demo:latest
5
    container_name: 'monitoring-demo'
6
    build:
7
      context: ./
8
      dockerfile: Dockerfile
9
    ports:
10
    - '8080:8080'
11
  prometheus:
12
    image: prom/prometheus:v2.24.1
13
    container_name: 'prometheus'
14
    volumes:
15
    - ./monitoring/prometheus/:/etc/prometheus/
16
    ports:
17
    - '9090:9090'
18
  grafana:
19
    image: grafana/grafana:5.4.3
20
    container_name: 'grafana'
21
    ports:
22
    - '3000:3000'
23
    volumes:
24
    - ./monitoring/grafana/provisioning/:/etc/grafana/provisioning/
25
    env_file:
26
    - ./monitoring/grafana/config.monitoring
27
    depends_on:
28
    - prometheus



I've put together a small demo project, containing a simple Spring Boot application and the above Prometheus configuration, in a GitHub repository for demo and experimentation purposes. Now if you want to generate some statistics run a small load test with JMeter or Apache Bench. Feel free to use/fork it!

Spring Framework Spring Boot application Grafana

Published at DZone with permission of Jeroen Reijn. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook