Spring Boot 2 Web MVC Tags Contributor

Spring Boot 2: How To Override Tags in Server Requests Metrics

Understand how we can override tags in server requests metrics using Spring Web MVC Tags Contributor. This is useful for customizing the application endpoint metrics. This will override the default tags that are provided by the framework.

Spring Boot 2 Web MVC Tags Contributor
Spring Boot 2 Web MVC Tags Contributor

Context

When we add Spring Boot Starter Actuator dependency in our Spring Boot application, we can access the metrics for our application endpoints via the actuator metrics endpoint. In order to access the metrics data, we need to send the GET request specifying the name of metrics we are interested in i.e. in our case server requests metrics.

curl --request GET 'http://localhost:8080/actuator/metrics/http.server.requests'
ShellScript

By default, metrics are available with the name ‘http.server.requests’. This name can be customized by setting one property in the application.properties file.

When we send the request to the above endpoint, by default, we get the following tags in the output:

  • exception: any exceptions that were encountered during the request processing
  • method: HTTP methods
  • uri: endpoints for which we received the requests
  • outcome: whether the request was processed successfully or not
  • status: HTTP status code

Advantages

When we add our custom tags, we can utilize this information to create charts and dashboards displaying information at a more granular level.

Example: For each request, we can add a location tag highlighting the region from where we have received a request. Based on this information, we can create graphs and charts and see from what all regions we are receiving major traffic.



Adding Custom Tags

Note:

Below mentioned steps works only in case of Spring Boot 2.x. For Spring Boot 3.x, kindly refer to article Spring Boot 3: How To Override Tags in Server Requests Metrics.

If we want to add our own tags in addition to tags that are provided by Spring Framework then we can do it with the help of Web MVC Tags Contributor.

Steps:

  1. Create a class that implements the WebMvcTagsContributor interface.
  2. Provide implementation for the getTags method wherein we can add additional tags.
  3. Register this class as Spring bean using @Component annotation.
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsContributor;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * The type Bootcamp to prod web mvc tags contributor.
 * Useful for adding tags in controller metrics in addition to default tags.
 */
@Component
public class BootcampToProdWebMvcTagsContributor implements WebMvcTagsContributor {
    
    @Override
    public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
        var tags = Tags.empty();

        // Optional tag which will be present in metrics only when the condition is evaluated to true
        if (request.getParameter("user") != null) {
            tags = tags.and(Tag.of("user", request.getParameter("user")));
        }

        // Custom tag which will be present in all the controller metrics
        tags = tags.and(Tag.of("tag", "value"));

        return tags;
    }

    @Override
    public Iterable<Tag> getLongRequestTags(HttpServletRequest request, Object handler) {
        return null;
    }
}
Java

We have control over a request, response, and exception object. Based on this, we can add our own tags.

In the above example, we have added two tags “user” and “tag” wherein the “user” tag is optional and will be added to metrics only if we are receiving requests with the query parameter “user” whereas the “tag” tag will be added to metrics for the all the requests that we have received.



Output:

Custom Tags Using Web MVC Tags Contributor
Custom Tags Using Web MVC Tags Contributor

Source Code

The source code for this example can be found on GitHub. Link: click here



Learn More

#

Curious to Learn Something New?

Check out how we can extend the default tags in Spring Boot 2.

Add a Comment

Your email address will not be published.