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

问题描述

使用微服务体系结构,我编写了一个通用的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)时仅将 / v1 / 作为度量标准中的端点/ 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.status(response));
        }
    };
}

仍然可以解析到指标中的 / v1 / URL。我尝试了很多谷歌搜索,但没有找到任何解决方案。预先感谢。

解决方法

基于Spring Boot RequestMapping的构建指标与注释匹配,并将其添加为标签。

这是为了避免标签爆炸。想象一个@RequestMappinguser/{userId}这样的路径,您希望将所有这些调用组合在一起(用户/ 1,用户/ 2,用户/ 3)。

您将要在post方法中创建自己的Timer,在其中设置该url标签,等等。

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