【问题标题】:Can I add a new dimension/tag to the existing metric http_server_requests_seconds?我可以向现有指标 http_server_requests_seconds 添加新维度/标签吗?
【发布时间】:2021-08-25 15:38:47
【问题描述】:

我有一个使用 Prometheus 收集指标的 Java Spring Boot 应用程序。它是一个 REST API,一切正常。

Spring 默认提供一些指标。例如,具有以下维度的度量标准http_server_requests_seconds_count

http_server_requests_seconds_count{application="metrics-demo-app", exception="None", instance="host.docker.internal:8080", job="metrics-demo-app", method="GET", outcome="SUCCESS", status="200", uri="/actuator/prometheus"}

维度是指上面示例中的键/值对(application="metrics-demo-app"、exception="None"、 等)标签。

我可以向这些指标添加新维度(键/值对)吗?我的想法是当创建用户的调用失败时,在该指标中插入更多信息。

我知道我可以创建一个新的自定义指标,但我想知道是否有某种方式可以在现有指标中添加内容。

这可能吗?该怎么做?

提前致谢:D

【问题讨论】:

    标签: java spring-boot prometheus metrics rest


    【解决方案1】:

    这是可以做到的,文档中的说明如下: https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html#actuator.metrics.supported.system

    我的代码是这样结束的:

    @Configuration
    public class MetricsConfig implements WebMvcTagsProvider {
    
      private final WebMvcTagsProvider delegate = new DefaultWebMvcTagsProvider();
    
      @Override
      public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response, Object handler, Throwable exception) {
        final String channelId = ServiceContextHolder.getContext().getXItauChannelId();
    
        return Tags.of(this.delegate.getTags(request, response, handler, exception)).and("channelId", channelId);
      }
    
      @Override
      public Iterable<Tag> getLongRequestTags(HttpServletRequest request, Object handler) {
        return null;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      相关资源
      最近更新 更多