【问题标题】:Resolving POST /** request URL to full request URL using micrometer使用千分尺将 POST /** 请求 URL 解析为完整请求 URL
【发布时间】:2020-12-06 14:17:43
【问题描述】:

通过微服务架构,我编写了一个通用的 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/ 作为指标中的端点,而我正在发送像 /v1/demo 这样的完整 URL /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。我尝试了很多谷歌搜索,但没有找到任何解决方案。提前致谢。

【问题讨论】:

    标签: spring spring-boot prometheus micrometer spring-micrometer


    【解决方案1】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-07-27
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多