Spring Cloud Sleuth 和 OpenTelemetry 是一个强大的组合,可以利用您现有的 Spring Boot 应用程序并为您提供超越日志和指标的洞察力。
此处显示的所有示例均可在https://github.com/xsreality/spring-boot-tracing-demo
在即将推出的 Spring Cloud 2021.0 中支持 Spring Cloud Sleuth 的集成。
OpenTelemetry 提供了一个代理 (JAR) 来附加 Java 应用程序以生成跟踪。但谁真的想和代理商打交道呢?感谢 Spring Cloud Sleuth 抽象,它将检测委托给 OpenTelemetry 并允许我们快速启动和运行。
启用它的方法:
<properties> <spring-cloud.version>2021.0.0-M2</spring-cloud.version> <spring-cloud-sleuth-otel.version>1.1.0-M3</spring-cloud-sleuth-otel.version> <opentelemetry-exporter-otlp>1.7.0</opentelemetry-exporter-otlp> <grpc-netty-shaded>1.41.0</grpc-netty-shaded> </properties>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency>
<!-- Spring Cloud Sleuth OTel requires a Spring Cloud Sleuth OTel BOM --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-otel-dependencies</artifactId> <version>${spring-cloud-sleuth-otel.version}</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement>
<!-- Sleuth with Brave tracer implementation --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> <exclusions> <!-- Exclude Brave (the default) --> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-brave</artifactId> </exclusion> </exclusions> </dependency> <!-- Add OpenTelemetry tracer --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-sleuth-otel-autoconfigure</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/io.opentelemetry/opentelemetry-exporter-otlp --> <dependency> <groupId>io.opentelemetry</groupId> <artifactId>opentelemetry-exporter-otlp</artifactId> <version>${opentelemetry-exporter-otlp}</version> </dependency> <!-- https://mvnrepository.com/artifact/io.grpc/grpc-netty --> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty-shaded</artifactId> <version>${grpc-netty-shaded}</version> </dependency>
<repositories> <repository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <url>https://repo.spring.io/snapshot</url> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <url>https://repo.spring.io/milestone</url> </pluginRepository> </pluginRepositories>
|
源代码可在https://github.com/xsreality/spring-boot-tracing-demo 获得。
运行应用程序会创建通过Logback MDC包含在日志行中的 Trace 和 Span ID 。这些将如下所示:
2021-11-06 00:10:02.278 INFO [http-service2,8a45749d445c4e5c4846c931d7f488c5,36a8075ff900ddd4] 9068 --- [nio-8081-exec-1] com.example.HomeController : Another bug bites the dust
|
如果您的应用程序使用自定义日志模式并且您没有在日志中看到跟踪 ID,请切换到 Spring 使用的默认模式或至少使用该LOG_LEVEL_PATTERN变量:
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
|
任何支持 OTEL 协议 (OTLP) 的系统均可用于导出跟踪。越来越多的系统正在添加对 OTEL 的支持, 3 个工具——Honeycomb、Grafana Tempo 和 Elastic APM。
导出跨度跟踪很容易。只需在 application.yaml 中添加以下属性
spring.sleuth.otel.exporter.otlp.endpoint: https://api.honeycomb.io spring.sleuth.otel.exporter.otlp.headers.x-honeycomb-team: xxx spring.sleuth.otel.exporter.otlp.headers.x-honeycomb-dataset: dataset
|