首页 > 解决方案 > 使用千分尺将 POST /** 请求 URL 解析为完整请求 URL

问题描述

使用微服务架构,我编写了一个通用的 POST 请求处理程序,它被所有微服务使用。spring 中的 post 映射如下所示:

@RestController
@RequestMapping(value = "/v1/", consumes = {MediaType.APPLICATION_JSON_VALUE}, produces = {MediaType.APPLICATION_JSON_VALUE})
public class V1Controller {
    @PostMapping(path = "/**")
    public @ResponseBody Json post () {}
}

现在,当我使用千分尺使用此端点的指标时,我只将/v1/作为指标中的端点,而我正在从调用服务发送完整的 URL,例如/v1/demo/foo 。我尝试了很多组合,但它不起作用。我还添加了我列出的 WebMvcTagsProvider 以请求和解决 POST api 调用。

@Bean
@SuppressWarnings("unchecked")
public WebMvcTagsProvider webMvcTagsProvider(ObjectMapper objectMapper) {
    return new DefaultWebMvcTagsProvider() {
        public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
            if ("POST".equals(request.getMethod())) {
                Tag uriTag = Tag.of("uri", String.valueOf(request.getRequestURI()));

                return Tags.of(WebMvcTags.method(request), uriTag, WebMvcTags.exception(exception), WebMvcTags.status(response));
            }

            return Tags.of(WebMvcTags.method(request), WebMvcTags.uri(request, response), WebMvcTags.exception(exception), WebMvcTags.status(response));
        }
    };
}

它仍然解析为指标中的/v1/ URL。我尝试了很多谷歌搜索,但没有找到任何解决方案。提前致谢。

标签: springspring-bootprometheusmicrometerspring-micrometer

解决方案


基于 Spring Boot RequestMapping 的构建在注释上匹配并将它们添加为标签。

这是为了避免标签爆炸。想象一下,@RequestMapping对于类似的路径user/{userId},您希望将所有这些调用组合在一起(user/1、user/2、user/3)。

您需要Timer在您的 post 方法中创建您自己的设置 URL 标签等。

如果您决定重用与内置 Spring Boot 指标相同的指标名称,您也需要禁用该指标名称,因此您不会重复计算这些请求。


推荐阅读