【问题标题】:Add custom labels to default spring boot actuator metrics将自定义标签添加到默认的 Spring Boot 执行器指标
【发布时间】:2022-01-28 15:00:18
【问题描述】:

我目前正在开发一个 Spring Boot (webflux) 项目,我们在 /actuator/prometheus 端点上使用 Spring Boot 执行器依赖项公开了我们的应用程序的指标,默认情况下这给了我们例如: http_server_requests_seconds_bucket 指标。默认情况下它有 {exception="None",method="POST",outcome="SUCCESS",status="200",uri="/test",le="0.268435456",}标签。我想添加另一个标签,说“挑战”,这取决于请求有效负载。例如,我的请求负载如下所示,

{
 "type" : "basic",
 "method" : "someMethod"
}

我想在该指标中添加一个标签,即http_server_requests_seconds_bucket{exception="None",...,challenge="basic"},它基于请求有效负载“类型”参数。有人可以告诉我如何将自定义标签添加到 spring-boot-actuator 提供的现有默认指标

注意:我们只有有限数量的“类型”值,所以它不会爆炸

【问题讨论】:

    标签: java spring spring-boot prometheus metrics


    【解决方案1】:

    经过一番研究,我发现我们可以通过扩展一个名为 DefaultWebMvcTagsProvider(用于 Spring MVC)的类并添加自定义标签来做到这一点。

    @Component
    public class CustomTagsAdder extends DefaultWebMvcTagsProvider {
    
    @Override
    public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
    
        List<Tag> tags = new ArrayList<>();
        tags.add(Tag.of("CUSTOM_TAG_KEY", "CUSTOM_TAG_VALUE")); // Repeat this line for each of the tags you want
    
    
        for(Tag tag : super.getTags(request, response, handler, exception))
            tags.add(tag);
    
        return tags;
    }
    }
    

    类似地,对于 webflux 项目,您可以扩展 DefaultWebFluxTagsProvider 并添加您的自定义标签。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-04
      • 2018-11-29
      • 1970-01-01
      • 2015-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多