Monitor Spring Boot Web Application Performance Using Micrometer and InfluxDB
By leveraging Spring Boot Actuator, Micrometer with InfluxDB, and Grafana, you can gain crucial insights into your application’s performance.
Join the DZone community and get the full member experience.
Join For FreeAs a Java developer, there's nothing more frustrating than dealing with sluggish application performance in production. Diagnosing issues within complex microservice architectures can quickly become a time-consuming and daunting task. Fortunately, the Spring Boot framework offers a powerful observability stack that streamlines real-time monitoring and performance analysis. By leveraging tools like Spring Boot Actuator, Micrometer with InfluxDB, and Grafana, you can gather meaningful insights easily and quickly.
In this article, we'll walk through setting up this stack using a simple "Card-Playing" app/game as our use case. For your reference, the complete working example is available on GitHub.
Setting Up the Project
First, initialize your Spring Boot project and add the necessary dependencies. In this example, we’ll use Maven and configure the dependencies in the pom.xml file. The key dependencies for this setup are Spring Boot Actuator and Micrometer with InfluxDB registry.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-influx</artifactId>
<version>1.13.1</version>
</dependency>
Spring Boot Actuator
Spring Boot Actuator offers production-ready features that help monitor and manage your application. By default, it exposes endpoints like /actuator/health
and /actuator/info
. You can enable additional endpoints securely by configuring the application.properties file, as shown below:
# SpringBoot Actuator Endpoints configuration
management.endpoints.web.exposure.include=health,info,metrics
Micrometer With InfluxDB
Micrometer is a lightweight library that integrates seamlessly with Spring Boot to enable application instrumentation. Once you’ve added the micrometer-registry-influx
dependency to your pom.xml, you need to configure it in the application.properties file. This configuration will expose the relevant metrics to InfluxDB for monitoring. You’ll need to provide your InfluxDB token, URL, organization, and bucket information, which can be found in your InfluxDB portal.
# InfluxDB Micrometer Registry Configuration for v2
management.metrics.tags.application=PickACard
management.metrics.export.influx.api-version=v2
management.metrics.export.influx.batch-size=10000
management.metrics.export.influx.compressed=true
management.metrics.export.influx.connect-timeout=1s
management.metrics.export.influx.enabled=true
management.metrics.export.influx.num-threads=2
management.metrics.export.influx.read-timeout=10s
management.metrics.export.influx.step=1m
management.metrics.export.influx.bucket=YOUR_INFLUXDB_BUCKET
management.metrics.export.influx.org=YOUR_INFLUXDB_ORG
management.metrics.export.influx.token=YOUR_INFLUXDB_TOKEN
management.metrics.export.influx.uri=YOUR_INFLUXDB_URI
Application Architecture
The application logic revolves around three main classes: CardController.java
, DeckOfCardService.java
, and InfluxDBConfig.java
.
CardController.java
: This serves as the entry point for the application, handling HTTP requests from users. When a user makes a request to the/card
endpoint, the controller invokes theDeckOfCardService
.DeckOfCardService.java
: This class interacts with the external Deck of Cards API to fetch a random card and return it as an SVG image.InfluxDBConfig.java
: This class configures InfluxDB metrics, ensuring that the application’s performance data is logged and accessible for analysis and monitoring.
Meanwhile, the InfluxDBConfig.java
class handles the configuration of the Micrometer integration with InfluxDB, based on the settings in the application.properties file. As the application processes requests, Micrometer collects various performance metrics — such as response times and request counts — and sends them to the InfluxDB Bucket for storage. These metrics are then visualized in Grafana, allowing developers to monitor the application’s performance in real time.
Key Micrometer Concepts
- Metrics: Data points that describe the behavior of your application, such as HTTP request counts, response times, and memory usage
- Meters: Instruments that record metrics
- Timers: Measure the duration of events (e.g., request handling time)
- Counters: Measure an increasing value (e.g., number of HTTP requests)
- Gauges: Represent a single value that can increase or decrease (e.g., memory usage)
- Tags: Labels (key-value pairs) that provide context to a metric (e.g., HTTP status codes)
- Registry: A place where metrics are stored, such as the
InfluxMeterRegistry
for InfluxDB
In our example application, we have a simple card game that uses a third-party HTTP API to fetch random cards. While I’m using a free API for this example, monitoring API calls becomes even more critical when working with a paid service. With Spring Boot Actuator and Micrometer integrated with InfluxDB, we can monitor custom metrics, like tracking how long each request takes, the result of the request, and how much memory the application uses. Here’s an example of the code:
@Controller
public class CardController {
@GetMapping("/card")
@ResponseBody
public Mono<String> getRandomCard() {
return deckOfCardService.getRandomCardSvg();
}
}
Logging and Monitoring With InfluxDB and Grafana
Once your Spring Boot application is running, it will send metrics to the InfluxDB bucket you’ve configured. You can view these metrics directly in the InfluxDB Dashboard or connect InfluxDB with a popular visualization tool like Grafana to create a custom dashboard for all your relevant metrics. This allows you to track important data, such as how many times the endpoint was called, when it was called, and the result of each request, giving you valuable insights into your application's performance.
The Bottom Line
By leveraging Spring Boot Actuator, Micrometer with InfluxDB, and Grafana, you can gain crucial insights into your application’s performance. This proactive approach helps identify and resolve issues early while ensuring a seamless user experience, particularly in complex environments like Kubernetes clusters. Tracking key performance metrics and health indicators empowers you to embrace a DevOps mindset, enabling you to deploy with confidence.
Feel free to explore the sample app on GitHub (linked earlier), and remember: you can start using InfluxDB Cloud for free. If you have any questions or feedback, don't hesitate to comment below or tag me on social media.
Published at DZone with permission of Suyash Joshi. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments