Search code examples
javaspring-bootmicrometerspring-micrometer

Including spring boot endpoint path variable as a metric dimension


I have api endpoint : /user/{tenant}/create

I am using spring boot 2 with micrometer for metrics.

By default @Timer annotation for spring boot 2 endpoint includes the following tags: exception,method, uri, status

I want to add the passed value for api parameter "tenant" as an extra tag for the endpoint

How do I do that with spring boot 2 and micrometer


Solution

  • Use custom WebMvcTagsProvider, e.g.:

    @Bean
    public WebMvcTagsProvider webMvcTagsProvider() {
        return new WebMvcTagsProvider() {
            @Override
            public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
                return ((Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE))
                        .entrySet()
                        .stream()
                        .map(entry -> new ImmutableTag(entry.getKey(), entry.getValue()))
                        .collect(Collectors.toList());
            }
    
            @Override
            public Iterable<Tag> getLongRequestTags(HttpServletRequest request, Object handler) {
                return new ArrayList<>();
            }
        };
    }