使用千分尺将 POST /** 请求 URL 解析为完整请求 URL

Resolving POST /** request URL to full request URL using micrometer

对于微服务架构,我编写了一个通用的 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 () {}
}

现在,当我使用千分尺使用此端点的指标时,我在发送完整 URL 就像来自调用服务的 /v1/demo/foo。我尝试了很多组合,但没有用。我还在列出请求和解析 POST api 调用的地方添加了 WebMvcTagsProvider。

@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 中的构建基于 Boot RequestMapping 的指标匹配注释并将其添加为标签。

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

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

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