【问题标题】:How to modify prometheus exposed metric names using Actuator in Spring-boot 2如何在 Spring-boot 2 中使用 Actuator 修改 prometheus 公开的指标名称
【发布时间】:2018-04-02 15:36:46
【问题描述】:

我在 springboot 2 中使用 Actuator 来公开 /actuator/prometheus 端点,prometheus 实例将从该端点提取指标。

一切都很完美,除了因为我需要调整指标名称。我的意思不是对 Prometheus 有意义的后缀 (_count, _total, _bucket, ...),而是类似:

http_server_requests_seconds_count -> http_server_requests_count http_server_requests_seconds_max->latency_seconds_max http_server_requests_seconds_sum->latency_seconds_sum http_server_requests_seconds_bucket->latency_seconds_bucket

有没有更好的方法来解决这个问题?

附言

我知道我可以使用

management.metrics.web.server.requests-metric-name=different

得到

different_seconds_count
different_seconds_max 
different_seconds_sum 
different_seconds_bucket

但很难做到:

1º 去掉 _seconds 后缀

2º 仅对其中一个使用不同的基本名称

我猜我可以写一个替代的PrometheusRenameFilter,但不确定如何将它配置为默认注册表。

【问题讨论】:

    标签: spring-boot prometheus spring-boot-actuator


    【解决方案1】:

    您可以覆盖此方法并更新命名约定:

    @Configuration
    public class MetricsConfiga {
    @Bean
    MeterRegistryCustomizer<MeterRegistry> configurer(String applicationName) {
        return (registry) -> registry.config().namingConvention(new NamingConvention() {
            @Override
            public String name(String name, Meter.Type type, String baseUnit) {
                return "PREFIX" + Arrays.stream(name1.split("\\."))
                    .filter(Objects::nonNull)
                    .collect(Collectors.joining("_"));
            }
           });
        }
    }
    

    【讨论】:

      【解决方案2】:

      现在我知道如何自定义全局注册表了:

      例如设置自定义仪表过滤器:

      @Configuration
      public class MetricsConfig {
          @Bean
          MeterRegistryCustomizer<MeterRegistry> metricsConfig() {
              return registry -> registry.config().meterFilter(new CustomRenameFilter());
          }
      }
      

      但是,在注册表中设置自定义重命名过滤器仅允许重命名基本指标名称。 它不作用于后缀,也不允许作用于属于一组的特定指标,例如由摘要生成。

      使用自定义NamingConvention 我可以为约定基本名称添加后缀...我什至可以更改现有后缀或替换约定基本名称。

      最后请注意,Histogram prometheus 度量类型期望创建

      <basename>_bucket
      <basename>_sum
      <basename>_count
      

      使用这些特定名称,因此以我想要的方式调整组件可能是不正确的,因为那将是一个不同的组件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-11-30
        • 2021-04-23
        • 2021-06-16
        • 2018-09-16
        • 2020-06-01
        • 1970-01-01
        • 2019-02-08
        • 2019-10-01
        相关资源
        最近更新 更多