包括 spring 引导端点路径变量作为度量维度

Including spring boot endpoint path variable as a metric dimension

我有 api 个端点:/user/{tenant}/create

我正在使用 spring 带测微计的引导 2。

默认情况下,spring boot 2 端点的 @Timer 注释包括以下标签:exception、method、uri、status

我想添加 api 参数 "tenant" 的传递值作为端点的额外标记

如何使用 spring 引导 2 和千分尺

使用自定义 WebMvcTagsProvider,例如:

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

将路径变量添加到默认标签的方法:

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;
            }
        };
    }

}

从Spring Boot 2.3.0开始,如果你想为某些请求添加额外的标签到默认标签,更好的方法是添加一个@Bean of class WebMvcTagsContributor。这样您的代码就不必担心放入默认标签。

已在 https://github.com/spring-projects/spring-boot/issues/20175

中实施

代码如下所示:

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