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:
- Log in and change the admin password.
- Add the InfluxDB data source.
(host: http://influxdb:8086, admin credentials as in docker-compose.yml) - 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.
This config have a problem. management.metrics.export.influx.user=user showed me “unable to parse authentication credentials” error. When i red the documentation, i can saw that the field should be “user-name”- http://micrometer.io/docs/registry/influx. 2 hours of headache XD.
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.
what happens when the influxdb-endpoint isn’t available?
Is your app going to store all info in memory or is it dropped?
Looking at Micrometer’s InfluxMeterRegistry and PushMeterRegistry I’m pretty certain that failed metric pushes are only logged and the data discarded.