Generate application metrics using SpringBoot and Micrometer

Sabyasachi Bhattacharya
3 min readMay 22, 2022

--

Introduction

Observability is one of the pillars of modern microservices architecture. Application metrices are one dimension of that observbility. When an application runs on production we may want to know various operational metrics like memory,cpu ,threadpool usage etc as well as buisness metrics for example how many request for a particular operations are made.
Spring boot with the help of micrometer enables application developers to expose various metrices.

Setup metrics

To add support for metrics , we need to add actuator dependency

and we also need to enable the metrics endpoint like following -

That’s it our application now set to expose metrics.

The below curl

curl localhost:8080/actuator/metrics | jq .

will give response like -

These are the metrics that spring boot provides out of the box. We can see it includes jvm memory, threads, cpu usage etc.

The below request will show used jvm memory

A metrics can have multiple dimensions. For example jvm.memory.max has heap and nonheap size. We can drill down to metrics using its tags like .

So far we know that spring boot exposes metrics and we can request metric endpoint to get those metrics and if required we can drill down to this metrics using available tags.

Custom Metrics

What if we need more metrics?

Spring uses Micrometer( https://micrometer.io/) underneath which takes care of generating and exposing metrics.
MeterRegistry is the core concept of micrometer that holds multiple metrices.

We can simply inject an instance of MeterRegistry in our custom metrics provider like below -

Here we have created a new metrics named products , every time we add a new product we will increment the value.

Now if we curl our metrics endpoint with we get product count

Streaming metrics

On production we would like to stream metrices to a data store like elasticsearch, influxdb etc. Spring Boot supports out of the box various data sinks https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.metrics .

For this post I ran a influxdb docker image locally . We can see our custom metrics pushed to influx -

On the application side configuration looks like -

Config properties for influxdb 1.x and 2.x different.
The above config is for influxdb 2.x. For 1.x we need to use username , password and db instead of token,bucket and org. Also autoCreateDb should be false as otherwise springboot will try to create a db named mydb .

Micrometer metric Types

Counter Counters are monotonically increasing metrics. It can be incremented by a fixed amount which is always positive.

Gauge Gauges are instantaneous metrics this can either go up or down.

Timer Timers are short duration latencies and frequency of events.

Our custom metric is a type of counter to measure the added products .

This is at a very high level how to expose metrics.

In production though we need to be aware of the volume of metrics, because if volumes and frequency is high our dashboards can become really slow.

We should also think about a retention policy of data to not to store unecessary old data. This will help saving some storage.

In conclusion metrics are essential part of our services and spring boot make it easy to gather and expose it to various data sinks.

Originally published at https://dev.to on May 22, 2022.

--

--

Sabyasachi Bhattacharya
Sabyasachi Bhattacharya

No responses yet