spr*_*ast 3 java spring spring-boot datadog spring-micrometer
我试图在我的应用程序中利用 Spring Boot 3.x 中的 Observability API 进行跟踪和指标,但我对如何获取正确的可跟踪性和指标详细信息的必要设置感到困惑。
我创建了一个示例 Spring Boot 应用程序用于测试。
我在pom.xml中添加了这些依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-datadog</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing</artifactId>
</dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)
应用程序.yml:
spring:
application:
name: datadog-sample
server:
port: 8090
management:
metrics:
distribution:
percentiles-histogram:
http:
server:
requests: true
endpoint:
health:
cache:
time-to-live: 6s
show-details: always
metrics:
enabled: true
endpoints:
web:
exposure:
include: health,info,metrics
health:
jmx:
metrics:
export:
enabled: true
step: 1m
info:
env:
enabled: true
datadog:
metrics:
export:
apiKey: test
tracing:
sampling:
probability: 1.0
propagation:
type: W3C
logging:
pattern:
console: .%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]
- %msg%n
Run Code Online (Sandbox Code Playgroud)
测试控制器:
@RestController
@Slf4j
public class TestController {
@GetMapping(value = "/method1")
public ResponseEntity<String> method1(@RequestParam String input) throws IOException, InterruptedException {
log.info("Inside the method1 with data = {}",input);
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("http://localhost:8090/method2")).build();
HttpResponse<String> response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
return ResponseEntity.ok(response.body());
}
@GetMapping(value = "/method2")
public ResponseEntity<String> method2() {
log.info("Inside the method2");
return ResponseEntity.ok("Called method2 successfully");
}
}
Run Code Online (Sandbox Code Playgroud)
问题:当服务 1被调用 ( http://localhost:8090/method1?input=testdata) 时,它在内部调用服务 2并生成跟踪 Id和跨度 Id,但对于每个服务,它会生成日志中下面给出的不同跟踪 Id:
. INFO [datadog-sample,652553b7e89ee89b58c1c37b35cb6102,58c1c37b35cb6102] - Inside the method1 with data = testdata
. INFO [datadog-sample,652553b7ec4d43c0f0e090c94225d91c,f0e090c94225d91c] - Inside the method2
Run Code Online (Sandbox Code Playgroud)
问题:
难道这不应该是单个Trace Id和多个Span Id以便可以轻松跟踪流吗?
我是否需要在任何地方使用@Obesrved注释以便不需要自定义任何行为?
要将指标/可观察性详细信息发送到 Datadog:
微米可观测性对于 spring cloud-kafka-binder 应用程序来说是开箱即用的还是需要任何特定的配置?如果是的话,有人可以提供参考示例吗?
我在你的代码中发现了为什么你得到不同的traceIds的问题。
这不应该是单个TraceId搭配多个SpanId吗,这样就可以轻松追踪流向了。由于我不想自定义任何行为,我是否需要在任何地方使用 @Observed 注释?
答案:使用测微计跟踪上下文传播通过java.net.http.HttpRequest/java.net.http.HttpResponse测试无法正常工作。
因此,我猜想它并没有将对 method2 的调用视为同一跟踪上下文传播的一部分,尽管它应该如此。
请使用RestTemplate方式发送请求并获取响应。跟踪的传播肯定可以与RestTemplate一起使用。
阅读此处的文档:https ://spring.io/blog/2022/10/12/observability-with-spring-boot-3
您需要在pom.xml中添加OpenTelemetry ( io.micrometer:micrometer-tracing-bridge-otel) 依赖项,以使用 Micrometer Tracing 跟踪上下文传播( RestTemplate需要)。
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
对于Micrometer 与 Datadog 的集成,您需要io.micrometer:micrometer-registry-datadog在 pom.xml 中添加依赖项:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-datadog</artifactId>
<scope>runtime</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
我使用的pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-datadog</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-datadog</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-otel</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-datadog</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Run Code Online (Sandbox Code Playgroud)
自定义配置类:
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import io.micrometer.observation.ObservationRegistry;
import io.micrometer.observation.aop.ObservedAspect;
@Configuration
public class ExampleConfiguration {
@Bean
ObservedAspect observedAspect(ObservationRegistry observationRegistry) {
return new ObservedAspect(observationRegistry);
}
@Bean
RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}
}
Run Code Online (Sandbox Code Playgroud)
您必须创建ObservedAspect bean 才能通过AOP启用可观察性。
将 TestController 更新为:
import java.net.URI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import io.micrometer.observation.annotation.Observed;
import lombok.extern.slf4j.Slf4j;
@RestController
@Slf4j
public class TestController {
@Autowired
private RestTemplate restTemplate;
@Observed(name = "method1", contextualName = "method1")
@GetMapping(value = "/method1")
public ResponseEntity<String> method1(@RequestParam String input) {
log.info("Inside the method1 with data = {}", input);
ResponseEntity<String> response = restTemplate.getForEntity(URI.create("http://localhost:8090/method3"),
String.class);
return ResponseEntity.ok(response.getBody());
}
@Observed(name = "method2", contextualName = "method2")
@GetMapping(value = "/method3")
public ResponseEntity<String> method2() {
log.info("Inside the method2");
return ResponseEntity.ok("Called method2 successfully");
}
}
Run Code Online (Sandbox Code Playgroud)
注意:您需要使用@Observed将数据放入 Datadog 指标中。
应用程序属性:
spring.application.name=datadog-sample
server.port=8090
management.datadog.metrics.export.api-key=2196881cdea46553c0e65ab7aa8af0de
management.datadog.metrics.export.application-key=8df3b2e284a626127f5b05a4f40fde064036fcef
management.datadog.metrics.export.uri=https://us5.datadoghq.com
management.metrics.distribution.percentiles-histogram.http.server.requests=true
management.endpoints.web.exposure.include=health,info,metrics
management.tracing.sampling.probability=1.0
management.tracing.propagation.type=w3c
management.info.env.enabled=true
management.jmx.metrics.export.enabled=true
management.jmx.metrics.export.step=1m
management.endpoint.health.cache.time-to-live=6s
management.endpoint.health.show-details=always
management.endpoint.metrics.enabled=true
logging.pattern.console=.%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}] - %msg%n
Run Code Online (Sandbox Code Playgroud)
对于 datadog,您需要设置这些属性才能开始使用 micrometer-registry-datadog 和 spring-boot:
management.datadog.metrics.export.api-keymanagement.datadog.metrics.export.application-keymanagement.datadog.metrics.export.uri如何设置 Datadog 的步骤:
对于 macOS,我可以通过以下命令安装 Agent 7:
DD_AGENT_MAJOR_VERSION=7 DD_API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX DD_SITE="us5.datadoghq.com" bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_mac_os.sh)"
Run Code Online (Sandbox Code Playgroud)
该代理在后台运行。
验证 datadog 代理是否正在运行。输入这个命令 -
datadog-agent status
Run Code Online (Sandbox Code Playgroud)
api-key,请参阅下面给出的屏幕截图。application-key,请参阅下面给出的屏幕截图。https://us5.datadoghq.com/。您可以从浏览器地址本身复制。就这样。
当我点击请求时,我能够在控制台中获得具有多个跨度 ID 的相同跟踪 ID。
输出:
具有相同traceIds的Spring日志:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
[32m :: Spring Boot :: [39m [2m (v3.1.4)[0;39m
. INFO [datadog-sample,,] - Starting SpringBootDatadogApplication using Java 17.0.1 with PID 19939 (/Users/anish/Downloads/spring-boot-datadog/target/classes started by anish in /Users/anish/Downloads/spring-boot-datadog)
. INFO [datadog-sample,,] - No active profile set, falling back to 1 default profile: "default"
. INFO [datadog-sample,,] - Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
. INFO [datadog-sample,,] - For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
. INFO [datadog-sample,,] - Tomcat initialized with port(s): 8090 (http)
. INFO [datadog-sample,,] - Starting service [Tomcat]
. INFO [datadog-sample,,] - Starting Servlet engine: [Apache Tomcat/10.1.13]
. INFO [datadog-sample,,] - Initializing Spring embedded WebApplicationContext
. INFO [datadog-sample,,] - Root WebApplicationContext: initialization completed in 1448 ms
. INFO [datadog-sample,,] - publishing metrics for DatadogMeterRegistry every 1m
. INFO [datadog-sample,,] - LiveReload server is running on port 35729
. INFO [datadog-sample,,] - Exposing 3 endpoint(s) beneath base path '/actuator'
. INFO [datadog-sample,,] - Tomcat started on port(s): 8090 (http) with context path ''
. INFO [datadog-sample,,] - Started SpringBootDatadogApplication in 2.738 seconds (process running for 3.802)
. INFO [datadog-sample,,] - Initializing Spring DispatcherServlet 'dispatcherServlet'
. INFO [datadog-sample,,] - Initializing Servlet 'dispatcherServlet'
. INFO [datadog-sample,,] - Completed initialization in 1 ms
. INFO [datadog-sample,666c0ee462f732fe199562808d56e40b,75defc5f90043736] - Inside the method1 with data = testdata
. INFO [datadog-sample,666c0ee462f732fe199562808d56e40b,95b4fc1dda6635e9] - Inside the method2
Run Code Online (Sandbox Code Playgroud)
最后,我能够让它发挥作用。:)
用于验证它是否已获取指标的屏幕截图:
转到Datadog 仪表板上的“指标” -> “资源管理器”菜单。
微米可观测性对于 springcloud-kafka-binder 应用程序来说是开箱即用的还是需要任何特定的配置?有这方面的参考例子吗?
是的。下面给出了文档链接。请参考这个:
| 归档时间: |
|
| 查看次数: |
1640 次 |
| 最近记录: |