【问题标题】:disable spring boot 2 default metrics禁用 Spring Boot 2 默认指标
【发布时间】:2018-12-07 18:27:11
【问题描述】:

我有 spring boot 2 REST 应用程序,启用了 spring actuator。默认情况下,spring 在/metrics 端点中生成许多指标(jvm、cpu、内存等)。除此之外,我还使用 Micrometer API 来创建自定义指标。到目前为止,它运行得非常好。

现在,我需要只生成我的自定义指标,但禁用 spring 提供的所有默认指标。请注意,我想禁用 /metrics 端点,但我只想禁用默认指标。

现在可以直接/间接实现吗?

感谢您的任何意见和建议!

【问题讨论】:

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


    【解决方案1】:
    @SpringBootApplication(
        exclude = { 
            CompositeMeterRegistryAutoConfiguration.class,
            DataSourcePoolMetricsAutoConfiguration.class, 
            TomcatMetricsAutoConfiguration.class,
            SimpleMetricsExportAutoConfiguration.class, 
            SystemMetricsAutoConfiguration.class 
        }
    )
    

    您需要排除所有负责 Spring Boot 中默认指标的指标自动配置类。

    您可以在此处找到所有指标自动配置类:

    https://github.com/spring-projects/spring-boot/blob/v2.1.1.RELEASE/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring.factories

    【讨论】:

      【解决方案2】:

      与 Spring Boot 中的大多数内容一样,默认指标是通过各种自动配置类进行配置的。要禁用默认指标,请使用 @SpringBootApplication 上的 exclude 属性排除它们的自动配置类。要查看涉及哪些自动配置,您可以使用--debug 启动您的应用程序或查看source code

      【讨论】:

        【解决方案3】:

        排除由MetricsAutoConfiguration.class 连接的指标,如下所示:

        @SpringBootApplication(exclude = MetricsAutoConfiguration.class)
        

        【讨论】:

          【解决方案4】:

          我在代码中解决此问题的第一件事是启用“条件评估报告”。在application.properties 文件中设置属性debug=true 就足够了。您应该在日志中看到类似下面的内容。只需在此块中搜索“指标”即可找到与指标相关的类的提及。

          2021-05-04T11:35:29.001-0300|DEBUG tionEvaluationReportLoggingListener[ 126] - 
          
          
          ============================
          CONDITIONS EVALUATION REPORT
          ============================
          
          
          Positive matches:
          -----------------
          
             AopAutoConfiguration matched:
                - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)
          
             ...
          

          接下来,您需要禁用任何对您无用的自动配置指标类。您可以通过在 @SpringBootApplication 注释中添加一些属性值来做到这一点。

          这是我在应用程序代码中所做的:

          @SpringBootApplication(
              exclude = {
                      CompositeMeterRegistryAutoConfiguration.class,
                      JvmMetricsAutoConfiguration.class,
                      DataSourcePoolMetricsAutoConfiguration.class,
                      LogbackMetricsAutoConfiguration.class,
                      HttpClientMetricsAutoConfiguration.class
              }
          )
          

          最后,如果有一个自动配置的指标类发布了一些您想要保留的指标值,但也发布了您不想要的其他指标值,那么您可以禁用单个指标。如果您使用的是application.yaml 文件,这很容易。以下是我的代码示例:

          management:
            metrics:
              # Disable Spring Boot auto-configuration of metrics meter registries
              use-global-registry: false
              enable:
                jvm:
                  # garbage collection, buffer, threads and classes metrics are not useful to us
                  gc: false
                  buffer: false
                  classes: false
                  threads: false
                process:
                  # process start time is useless
                  start:
                    time: false
                # system CPU metrics are useless for ECS tasks (we only care about 'process' metrics)
                system:
                  cpu: false
              export:
                # Disable default in-memory metrics meter registry
                simple:
                  enabled: false
                # Publish metrics to AWS Cloudwatch, using the largest submission batch size that AWS supports
                cloudwatch:
                  namespace: /my/metrics/namespace
                  batchSize: 20
          

          您可以对application.properties 文件执行相同的操作。它只会更冗长。

          还支持编写自定义“过滤器”类,与上面的属性/yaml 文件方法相比,这些类为您提供更细粒度的控制。我没用过,但为了完整起见,我在这里提一下。

          【讨论】:

            【解决方案5】:

            理论上,应该可以扩展 org.springframework.boot.actuate.metrics.MetricsEndpoint,只公开你想公开的信息。

            但是,我建议始终定义一个新端点来处理您的特定用例。 在这种情况下,您将不会太紧地绑定到 Spring 库。

            【讨论】:

              猜你喜欢
              • 2018-04-26
              • 1970-01-01
              • 1970-01-01
              • 2019-03-20
              • 1970-01-01
              • 2019-04-19
              • 1970-01-01
              • 2018-11-29
              相关资源
              最近更新 更多