包括Spring Boot端点路径变量作为度量维度

时间:2018-10-19 13:33:14

标签: java spring-boot micrometer spring-micrometer

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

我正在使用带有测微计的Spring Boot 2进行度量。

Spring Boot 2端点的默认@Timer注释包含以下标记:exception,method,uri,status

我想将api参数“ tenant”的传递值添加为端点的额外标记

我该如何使用弹簧靴2和测微计

4 个答案:

答案 0 :(得分:3)

使用自定义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<>();
        }
    };
}

答案 1 :(得分:0)

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

}

答案 2 :(得分:0)

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

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

}

答案 3 :(得分:0)

从Spring Boot 2.3.0开始,如果要为某些请求在默认标签中添加其他标签,更好的方法是添加WebMvcTagsContributor类的@Bean。这样,您的代码就不必担心放入默认标记。

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