此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.4.0spring-doc.cadn.net.cn

可观察性

可观察性是从外部观察正在运行的系统的内部状态的能力。 它由三个支柱组成:日志记录、指标和跟踪。spring-doc.cadn.net.cn

对于 metrics 和 traces, Spring Boot 使用 Micrometer Observation。 要创建自己的观察值(这将导致指标和跟踪),您可以注入ObservationRegistry.spring-doc.cadn.net.cn

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;

import org.springframework.stereotype.Component;

@Component
public class MyCustomObservation {

	private final ObservationRegistry observationRegistry;

	public MyCustomObservation(ObservationRegistry observationRegistry) {
		this.observationRegistry = observationRegistry;
	}

	public void doSomething() {
		Observation.createNotStarted("doSomething", this.observationRegistry)
			.lowCardinalityKeyValue("locale", "en-US")
			.highCardinalityKeyValue("userId", "42")
			.observe(() -> {
				// Execute business logic here
			});
	}

}
低基数标签将添加到指标和跟踪中,而高基数标签将仅添加到跟踪中。

类型的 beanObservationPredicate,GlobalObservationConvention,ObservationFilterObservationHandler将自动注册在ObservationRegistry. 您还可以注册任意数量的ObservationRegistryCustomizerbean 来进一步配置注册表。spring-doc.cadn.net.cn

可观测性支持依赖于 Context Propagation 库,用于跨线程和反应式管道转发当前观测值。 默认情况下,ThreadLocal值不会在 Reactive 运算符中自动恢复。 此行为由spring.reactor.context-propagation属性,该属性可设置为auto以启用自动传播。spring-doc.cadn.net.cn

有关观测的更多详细信息,请参阅千分尺观测文档spring-doc.cadn.net.cn

JDBC 的可观测性可以使用单独的项目进行配置。 Datasource Micrometer 项目提供了一个 Spring Boot Starters,它可以在调用 JDBC作时自动创建观察值。 在参考文档中阅读更多相关信息。
R2DBC 的可观察性内置于 Spring Boot 中。 要启用它,请添加io.r2dbc:r2dbc-proxy依赖项。

常用标签

常用标签通常用于对运行环境进行维度下钻,例如主机、实例、区域、堆栈等。 通用标签作为低基数标签应用于所有观测值,并且可以进行配置,如下例所示:spring-doc.cadn.net.cn

management.observations.key-values.region=us-east-1
management.observations.key-values.stack=prod
management:
  observations:
    key-values:
      region: "us-east-1"
      stack: "prod"

前面的示例将regionstack标记添加到值为us-east-1prod分别。spring-doc.cadn.net.cn

阻止观察

如果您想阻止某些观察结果被报告,您可以使用management.observations.enable性能:spring-doc.cadn.net.cn

management.observations.enable.denied.prefix=false
management.observations.enable.another.denied.prefix=false
management:
  observations:
    enable:
      denied:
        prefix: false
      another:
        denied:
          prefix: false

前面的示例将阻止名称以denied.prefixanother.denied.prefix.spring-doc.cadn.net.cn

如果要阻止 Spring Security 报告观察结果,请将属性management.observations.enable.spring.securityfalse.

如果需要更好地控制 Prevent of Observation,则可以注册ObservationPredicate. 仅当所有ObservationPredicatebean 返回true对于那个观察。spring-doc.cadn.net.cn

import io.micrometer.observation.Observation.Context;
import io.micrometer.observation.ObservationPredicate;

import org.springframework.stereotype.Component;

@Component
class MyObservationPredicate implements ObservationPredicate {

	@Override
	public boolean test(String name, Context context) {
		return !name.contains("denied");
	}

}

前面的示例将阻止名称中包含 “denied” 的所有观察。spring-doc.cadn.net.cn

OpenTelemetry 支持

有几种方法可以在您的应用程序中支持 OpenTelemetry。 您可以使用 OpenTelemetry Java 代理OpenTelemetry Spring Boot Starter。 由 OTel 社区支持的;指标和跟踪使用 OTel 库定义的语义约定。 本文档描述了 Spring 团队使用 Micrometer 和 OTLP 导出器正式支持的 OpenTelemetry; 指标和跟踪使用 Spring Projects 文档中描述的语义约定,例如 Spring Framework

Spring Boot 的 actuator 模块包括对 OpenTelemetry 的基本支持。spring-doc.cadn.net.cn

它提供了一个OpenTelemetry,如果存在SdkTracerProvider,ContextPropagators,SdkLoggerProviderSdkMeterProvider在 Application 上下文中,它们会自动注册。 此外,它还提供了一个Resource豆。 auto-configured 的属性Resource可以通过management.opentelemetry.resource-attributesconfiguration 属性。 如果您定义了自己的Resourcebean,情况将不再如此。spring-doc.cadn.net.cn

Spring Boot 不提供 OpenTelemetry 指标或日志记录的自动配置。 OpenTelemetry 跟踪仅在与 Micrometer Tracing 一起使用时自动配置。

接下来的部分将提供有关日志记录、指标和跟踪的更多详细信息。spring-doc.cadn.net.cn

千分尺观测注释支持

要启用指标扫描和跟踪注释,例如@Timed,@Counted,@MeterTag@NewSpanannotations 中,您需要设置management.observations.annotations.enabledproperty 设置为true. 此功能直接支持 Micrometer。请参阅 MicrometerMicrometer Tracing 参考文档。spring-doc.cadn.net.cn