Monitoring in SpringBoot 2.0: Micrometer + InfluxDB + Chronograf
By: Rohan Saraf
Tags: Grafana, Spring Boot 2, Influxdb, Monitoring, Java

PizzaDelivery Application

Let’s develop an application for a pizza delivery store, for example PizzaHut. The application will expose an api where partners can put orders for a pizza. Suppose our partners are online food ordering apps like UberEats, Zomato, Swiggy etc. Our application will receive orders, puts them in processing and then marks them successful once dispatched for delivery.

Set up a SpringBoot application by the spring initializr. Add the above dependencies. We will start with a PizzaOrderController.

import io.micrometer.core.instrument.Counter;

import io.micrometer.core.instrument.Metrics; @RestController

@RequestMapping(“/v1/”)

public class PizzaOrderController { @RequestMapping(value = “create”, method = RequestMethod.POST)

public long createOrder(@RequestParam String partnerId,

@RequestParam String type,@RequestParam String location) { long orderId = createOrder(type);

increaseCount(partnerId, “received”);

return orderId;

} private void increaseCount(String partnerId, String state) {

// Counter class stores the measurement name and the tags and

// their values

Counter counter =Metrics.counter(“request.orders”, “partnerId”,

partnerId, “state”, state);

counter.increment();

} private long createOrder(String type) {

// create order

return (long) Math.random();

} private void processOrders() {

List<Order> orders = getReceivedOrders();

int processedOrders = 0;

orders.forEach(order -> {

bool processed = processOrder(order);

if(processed){

increaseCount(order.getPartnerId(), “processed”);

}

});

} }

In the above code, we have created an api for receiving orders from our partners. Partner will send us their id, pizza type and location of delivery. We will send them an order id in response.

We want to keep a metric for orders received at a particular moment so that we can see for any spikes. We create Counter which has a measurement (“request.orders”) which can be considered as table (for SQL people) and some tags in form of key-value pairs, somewhat like columns and their values.

Counter counter =Metrics.counter(“request.orders”, “partnerId”,

partnerId.toString(), “state”, state);

measurement = “request.orders”, partnerId = “zomato”, state = “received”

This metric data will be pushed to the actuator endpoint, this work is done by the micrometer, we don’t need to anything for this. Let’s run our application and then see for results. After making random requests we get this.

Micrometer sends this data periodically to the actuator endpoints.

Micrometer is a dimensional-first metrics collection tool which helps you time, count, gauge your code

What is dimensional first?

Suppose we wanted to measure the metric for http requests. Earlier we had to create a hierarchical measurement like: http.request.count. But now we want to count them on the basis of the protocols ex: count of POST , GET requests. In hierarchical measurements, we had to make http.request.count.post, http.request.count.get two such measurements.

But by using micrometer we make only one measurement and add tags [post,get] to the measurement. We can add any number of tags to a measurement i.e we add dimensions to the measurement.