How to Use InfluxDB for Real-Time SpringBoot Application Monitoring

Navigate to:

Enterprise Java developers understand the frustration of sluggish application performance in production. Diagnosing issues within complex microservice architectures can be a time-consuming nightmare. Thankfully, the popular Java framework SpringBoot provides a robust observability stack to simplify real-time monitoring and analysis. By harnessing the power of libraries and tools such as SpringBoot Actuator, Micrometer with InfluxDB, and Grafana, you can gather meaningful insights easily and quickly.

In this article, we explore how to set it all up within a simple use case of a “Card-Playing” app/game. For reference purposes, you can also find the complete working example on GitHub. Card-App-Screenshot

Setting up the project

First, after initializing the SpringBoot project, we must add the necessary dependencies. Here, we will do that using Maven Project’s pom.xml file. The most important ones are Spring Boot Actuator and Micrometer with the 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>

SpringBoot Actuator

Spring Boot Actuator provides production-ready features that help you monitor and manage your application. By default, it exposes basic endpoints like /actuator/health and /actuator/info. However, you can enable more endpoints securely depending on your needs by editing the “application.properties” file, which looks like this:

# SpringBoot Actuator Endpoints configuration
management.endpoints.web.exposure.include=health,info,metrics

Micrometer with InfluxDB

Micrometer is an independent library that integrates well with Springboot to enable application instrumentation. Once you have the micrometer-influx dependency in your project’s pom.xml file, as shown above, the next step is to edit the application.properties file to configure the micrometer. This way, it can expose the relevant metrics to the InfluxDB bucket for data logging and monitoring purposes. Here, you will also need your InfluxDB “token,” URL, “org,” and “bucket” information, which you can find on the 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 architecture logic centers around three key classes: “CardController.java”, “DeckOfCardService.java”, and “InfluxDBConfig.java”. The CardController serves as the entry point, handling HTTP requests from users. When a request is made to the /card endpoint, the controller calls the DeckOfCardService, which interacts with the external Deck of Cards API to fetch a random card and return it as an SVG image via the index.html webpage to the client. Meanwhile, the InfluxDBConfig.java class configures the Micrometer integration with InfluxDB based on the application.properties file. As the application processes requests, Micrometer collects metrics such as response times and request counts and sends them to 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, like 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 go up or down (e.g., memory usage)

Tags: Labels (key-value pairs) that add context to a metric (e.g., HTTP status codes)

Registry: Where metrics are stored, like InfluxMeterRegistry for InfluxDB

Our example application has a simple card game using 3rd party HTTP API that provides random cards. Here, I’m using a free API; however, monitoring API calls would become even more critical if you use a paid service. With SpringBoot Actuator and Micrometer with InfluxDB configured, in the code snippet below, we can monitor a custom metrics “card” to track how long each request takes, what was the result of the request, and how much memory your application is using, etc.

@Controller
public class CardController {
@GetMapping("/card")
@ResponseBody
public Mono<String> getRandomCard() {
return deckOfCardService.getRandomCardSvg();
 }
}

Logging & monitoring using InfluxDB & Grafana

Once your Spring Boot application is running, it will start pushing metrics to the InfluxDB bucket you configured. You can then see the same using InfluxDB Dashboard or connect it with a popular visualization tool such as Grafana and create a custom dashboard for all your relevant metrics. Here, we can see how many times the endpoint was called, at what time, and the result.

Conclusion

By effectively using Spring Boot Actuator, Micrometer with InfluxDB, and Grafana, you can gain valuable insights into your application’s performance.This proactive approach helps prevent issues and ensures a smooth user experience, especially in complex environments like Kubernetes clusters.Tracking key application performance metrics and health indicators empowers you to adopt a DevOps mindset, allowing you to deploy to production confidently.

You are encouraged to explore the sample app on GitHub, and remember you can start using InfluxDB Cloud for free. If you have any questions or feedback, don’t hesitate to reach out to us on our community Slack or community forum