Search code examples
javaspring-bootprometheusmetricsrest

Can I add a new dimension/tag to the existing metric http_server_requests_seconds?


I have a Java Spring Boot Application that is collecting metrics with Prometheus. It is a REST API and everything is working.

Spring, by default, provide some metrics. For example, the metric http_server_requests_seconds_count that has the dimensions:

http_server_requests_seconds_count{application="metrics-demo-app", exception="None", instance="host.docker.internal:8080", job="metrics-demo-app", method="GET", outcome="SUCCESS", status="200", uri="/actuator/prometheus"}

By dimension I mean the key/value pairs (application="metrics-demo-app", exception="None", etc...) tags in the example above.

Can I add a new dimension (key/value pair) to those metrics? My idea is when a call to create a user fails, to insert some more info into that metric.

I know that I could create a new custom metric, but I wonder if there are some manner to just add things in an existing one.

Is that possible? How to do that?

Thanks in advance :D


Solution

  • This can be done and how is in the documentation, here: https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.metrics.supported.system

    My code ended like this:

    @Configuration
    public class MetricsConfig implements WebMvcTagsProvider {
    
      private final WebMvcTagsProvider delegate = new DefaultWebMvcTagsProvider();
    
      @Override
      public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
        final String channelId = ServiceContextHolder.getContext().getXItauChannelId();
    
        return Tags.of(this.delegate.getTags(request, response, handler, exception)).and("channelId", channelId);
      }
    
      @Override
      public Iterable<Tag> getLongRequestTags(HttpServletRequest request, Object handler) {
        return null;
      }
    }