简体   繁体   中英

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

@Bean
public WebMvcTagsProvider webMvcTagsProvider() {
    return new CustomWebMvcTagsProvider();
}

    public class CustomWebMvcTagsProvider extends DefaultWebMvcTagsProvider {
      public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
        return Tags.of(super.getTags(request, response, handler, exception)).and(getTenantTag(request));
      }

      private Tag getTenantTag(HttpServletRequest request) {
        String tenant = ((Map<String, String>)request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE)).get("tenant");
        if(tenant == null){
            tenant = "na";
        }
        return Tag.of("tenant", tenant);
      }
    }

Use custom WebMvcTagsProvider , eg:

@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<>();
        }
    };
}

Since Spring Boot 2.3.0 if you want to add additional tags to the default ones for some requests, a better approach is to add a @Bean of class WebMvcTagsContributor. This way your code doesn't have to worry about putting in the default tags.

Implemented in https://github.com/spring-projects/spring-boot/issues/20175

The code would look like this:

@Bean
public WebMvcTagsContributor webMvcTagsContributor() {
    return new WebMvcTagsContributor() {
        @Override
        public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
            Tags tags = Tags.empty();
            tags = tags.and(Tag.of("my_tag", "somevalue"));
            return tags;
        }

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

An approach to add path variables to default tags:

import io.micrometer.core.instrument.Tag;
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsContributor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class WebMvcTagsProviderConfig {

    @Bean
    public WebMvcTagsContributor webMvcTagsContributor() {
        return new WebMvcTagsContributor() {
            @Override
            public Iterable<Tag> getTags(
                    HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception
            ) {
                Map<String, String> pathVariables = ((Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE));
                return pathVariables == null
                        ? null
                        : pathVariables
                                .entrySet()
                                .stream()
                                .map(entry -> Tag.of(entry.getKey(), entry.getValue()))
                                .collect(Collectors.toList());
            }

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM