Observability
可观察性是指从外部观察运行系统内部状态的能力。它由日志、指标和跟踪这三大支柱组成。
Observability is the ability to observe the internal state of a running system from the outside. It consists of the three pillars logging, metrics and traces.
对于指标和跟踪,Spring Boot 使用 Micrometer Observation。若要创建你自己的观察(将生成指标和跟踪),可以注入 ObservationRegistry
。
For metrics and traces, Spring Boot uses Micrometer Observation.
To create your own observations (which will lead to metrics and traces), you can inject an ObservationRegistry
.
include-code::MyCustomObservation[]
低基数标签将添加到指标和跟踪中,而高基数标签将仅添加到跟踪中。 |
Low cardinality tags will be added to metrics and traces, while high cardinality tags will only be added to traces. |
类型为 ObservationPredicate
、GlobalObservationConvention
、ObservationFilter
和 ObservationHandler
的 Bean 将自动在 ObservationRegistry
中注册。此外,您还可以注册任意数量的 ObservationRegistryCustomizer
Bean 以进一步配置注册表。
Beans of type ObservationPredicate
, GlobalObservationConvention
, ObservationFilter
and ObservationHandler
will be automatically registered on the ObservationRegistry
.
You can additionally register any number of ObservationRegistryCustomizer
beans to further configure the registry.
可观测性支持依靠 Context Propagation library 来跨线程和响应式管道转发当前观测值。默认情况下,ThreadLocal
值不会自动重新恢复在响应式操作符中。此行为受 configprop:spring.reactor.context-propagation[] 属性控制,可以将其设置为 auto
以启用自动传播。
Observability support relies on the Context Propagation library for forwarding the current observation across threads and reactive pipelines.
By default, ThreadLocal
values are not automatically reinstated in reactive operators.
This behavior is controlled with the configprop:spring.reactor.context-propagation[] property, which can be set to auto
to enable automatic propagation.
有关观测值的更多详细信息,请参阅 Micrometer Observation documentation。
For more details about observations please see the Micrometer Observation documentation.
可以使用单独的项目来配置 JDBC 的可观测性。 Datasource Micrometer project 提供 Spring Boot 启动程序,在调用 JDBC 操作时自动创建观测值。在 in the reference documentation 了解更多相关信息。 |
Observability for JDBC can be configured using a separate project. The Datasource Micrometer project provides a Spring Boot starter which automatically creates observations when JDBC operations are invoked. Read more about it in the reference documentation. |
Observability for R2D2 已内置到 Spring Boot。要启用它,请将 |
Observability for R2DBC is built into Spring Boot.
To enable it, add the |
Common tags
普通标记通常用于对操作系统环境中的维度进行深入分析,例如主机、实例、区域、堆栈和其他。普通标记作为低基数标记应用于所有观测,并且可以进行配置,如下面的示例所示:
Common tags are generally used for dimensional drill-down on the operating environment, such as host, instance, region, stack, and others. Common tags are applied to all observations as low cardinality tags and can be configured, as the following example shows:
management: observations: key-values: region: "us-east-1" stack: "prod"
前面的示例向所有观测分别添加了 region
和 stack
标记,其值为 us-east-1
和 prod
。
The preceding example adds region
and stack
tags to all observations with a value of us-east-1
and prod
, respectively.
Preventing Observations
如果你想防止报告某些观测结果,可以使用 configprop:management.observations.enable[] 属性:
If you’d like to prevent some observations from being reported, you can use the configprop:management.observations.enable[] properties:
management: observations: enable: denied: prefix: false another: denied: prefix: false
前面的示例将防止名称以 denied.prefix
或 another.denied.prefix
开头的所有观测。
The preceding example will prevent all observations with a name starting with denied.prefix
or another.denied.prefix
.
如果你想阻止 Spring Security 报告观测结果,请将属性 configprop:management.observations.enable.spring.security[] 设置为 |
If you want to prevent Spring Security from reporting observations, set the property configprop:management.observations.enable.spring.security[] to |
如果你需要更大程度地控制观测阻止,可以注册类型为 ObservationPredicate
的 Bean。仅当所有 ObservationPredicate
Bean 都为该观测返回 true
时,才会报告观测。
If you need greater control over the prevention of observations, you can register beans of type ObservationPredicate
.
Observations are only reported if all the ObservationPredicate
beans return true
for that observation.
前面的示例将阻止名称中包含“denied”的所有观测。
The preceding example will prevent all observations whose name contains "denied".
OpenTelemetry Support
Spring Boot 的执行器模块包括对 OpenTelemetry 的基本支持。
Spring Boot’s actuator module includes basic support for OpenTelemetry.
它提供了一个类型为 OpenTelemetry
的 Bean,并且如果应用程序上下文中存在类型为 SdkTracerProvider
、ContextPropagators
、SdkLoggerProvider
或 SdkMeterProvider
的 Bean,它们将自动注册。此外,它还提供了一个 Resource
Bean。自动配置的 Resource
的属性可以通过 configprop:management.opentelemetry.resource-attributes[] 配置属性进行配置。如果你已经定义了自己的 Resource
Bean,则不再是这种情况下。
It provides a bean of type OpenTelemetry
, and if there are beans of type SdkTracerProvider
, ContextPropagators
, SdkLoggerProvider
or SdkMeterProvider
in the application context, they automatically get registered.
Additionally, it provides a Resource
bean.
The attributes of the auto-configured Resource
can be configured via the configprop:management.opentelemetry.resource-attributes[] configuration property.
If you have defined your own Resource
bean, this will no longer be the case.
Spring Boot 不提供 OpenTelemetry 指标或日志记录的自动配置。OpenTelemetry 跟踪仅在与 Micrometer Tracing 一起使用时自动配置。 |
Spring Boot does not provide auto-configuration for OpenTelemetry metrics or logging. OpenTelemetry tracing is only auto-configured when used together with Micrometer Tracing. |
接下来的部分将提供有关日志记录、指标和跟踪的更多详细信息。
The next sections will provide more details about logging, metrics and traces.
Micrometer Observation Annotations support
要启用诸如 @Timed
、@Counted
、@MeterTag
和 @NewSpan
注释的指标和跟踪注释的扫描,你需要将 configprop:management.observations.annotations.enabled[] 属性设置为 true
。此功能直接受 Micrometer 支持,请参阅 {url-micrometer-docs-concepts}#_the_timed_annotation[Micrometer] 和 {url-micrometer-tracing-docs}/api.html#_aspect_oriented_programming[Micrometer Tracing] 参考文档。
To enable scanning of metrics and tracing annotations like @Timed
, @Counted
, @MeterTag
and @NewSpan
annotations, you will need to set the configprop:management.observations.annotations.enabled[] property to true
.
This feature is supported Micrometer directly, please refer to the {url-micrometer-docs-concepts}#_the_timed_annotation[Micrometer] and {url-micrometer-tracing-docs}/api.html#_aspect_oriented_programming[Micrometer Tracing] reference docs.