Send Spring Boot Metrics into InfluxDB and Grafana

One of the notable Spring Boot 2 features is the introduction of Micrometer (SLF4J for application metrics).

Spring Boot ships with a whole series of built-in metrics collecting JVM metrics  (memory usage, garbage collection, threads, and classes), CPU usage, Tomcat metrics as well as others.

// dependencies block
compile 'org.springframework.boot:spring-boot-starter-actuator'
# enable actuator, use * to enable all endpoints
management.endpoints.web.exposure.include=info,health,metrics

You can query the metrics using the actuator HTTP endpoint:

curl http://localhost:8080/actuator/metrics/jvm.memory.used
{
   "name":"jvm.memory.used",
   "description":"The amount of used memory",
   "baseUnit":"bytes",
   "measurements":[
      {
         "statistic":"VALUE",
         "value":1.009509256E9
      }
   ],
   "availableTags":[
      {
         "tag":"area",
         "values":[
            "heap",
            "nonheap"
         ]
      },
      {
         "tag":"id",
         "values":[
            "Compressed Class Space",
            "PS Survivor Space",
            "PS Old Gen",
            "Metaspace",
            "PS Eden Space",
            "Code Cache"
         ]
      }
   ]
}

Please note that you need to authenticate if Spring Security is present.

Install InfluxDB and Grafana using Docker Compose

A simple way to run InfluxDB and Grafana locally is Docker Compose.

grafana:
  image: grafana/grafana:6.2.2
  ports:
    - 3000:3000
  volumes:
    - /var/lib/grafana
  environment:
    GF_SECURITY_ADMIN_PASSWORD: admin
influxdb:
  image: influxdb:1.7.6
  ports:
    - 8086:8086
  volumes:
    - /var/lib/influxdb
  environment:
    INFLUXDB_HTTP_AUTH_ENABLED: "true"
    INFLUXDB_USER: user
    INFLUXDB_USER_PASSWORD: pass
    INFLUXDB_DB: boot
    INFLUXDB_ADMIN_USER: admin
    INFLUXDB_ADMIN_PASSWORD: secret

Start InfluxDB and Grafana with docker-compose up .

Grafana is now running on http://localhost:3000. The default user is admin/admin.

Send Spring Boot Metrics to InfluxDB

Micrometer can send metrics to a series of destination using just configuration. You’ll only need to add the specific dependency and a few configuration values.

// dependencies block
compile 'io.micrometer:micrometer-registry-influx'
management.metrics.export.influx.db=boot
management.metrics.export.influx.userName=user
management.metrics.export.influx.password=pass
management.metrics.export.influx.uri=http://localhost:8086
management.metrics.export.influx.step=15s
management.metrics.export.influx.autoCreateDb=false

Grafana Configuration

The missing pieces to get Grafana up and running are:

  1. Log in and change the admin password.
  2. Add the InfluxDB data source.
    (host: http://influxdb:8086, admin credentials as in docker-compose.yml)
  3. Create a Dashboard.

Fully Working Example

All steps above are combined into a working example published on github.com/gysel/spring-boot-metrics-influxdb. The example includes and automatic provisioning of Grafana. You don’t need to add a data source of a dashboard. Everything is configured in startup.

4 Replies to “Send Spring Boot Metrics into InfluxDB and Grafana”

    1. Thanks for your hint. I’ve spent some time on this and you are correct. The exact field is called “userName” in the source code, “user-name” is likely a variation that Spring Boot accepts.

      I’ve not encountered this issue in my setup because I missed INFLUXDB_HTTP_AUTH_ENABLED=true in my local setup and I linked Spring Boot and InfluxDB without any authentication. My example code on GitHub has now authentication enabled.

      This post is also updated to include the correct authentication.

    1. Looking at Micrometer’s InfluxMeterRegistry and PushMeterRegistry I’m pretty certain that failed metric pushes are only logged and the data discarded.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.