禁用 spring 引导 2 默认指标

disable spring boot 2 default metrics

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

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

现在可以directly/indirectly吗?

感谢您的意见和建议!

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

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

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

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

@SpringBootApplication(exclude = MetricsAutoConfiguration.class)
@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

我在代码中解决这个问题的第一件事是启用 'Conditions Evaluation Report'。在 application.properties 文件中设置 属性 debug=true 就足够了。您应该会在日志中看到如下内容。只需在此块中搜索 'Metric' 即可找到与指标相关的 classes.

2021-05-04T11:35:29.001-0300|DEBUG tionEvaluationReportLoggingListener[ 126] - 


============================
CONDITIONS EVALUATION REPORT
============================


Positive matches:
-----------------

   AopAutoConfiguration matched:
      - @ConditionalOnProperty (spring.aop.auto=true) matched (OnPropertyCondition)

   ...

接下来,您需要禁用任何对您无用的自动配置指标 class。您可以通过向 @SpringBootApplication 注释添加一些属性值来实现。

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

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

最后,如果有一个自动配置的指标 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 文件执行相同的操作。它只会更冗长。

还支持编写自定义 'filter' classes,与上面的 properties/yaml 文件方法相比,它为您提供更细粒度的控制。我没有使用过它,但为了完整起见,我在这里提到它。