springboot 3 可观察性,使用 micrometer 和 datadog 用于 http 服务和 kafka 消费者

问题描述 投票:0回答:1

我正在尝试在我的应用程序中利用 SpringBoot 3 的可观察性 API 进行跟踪和指标,但对必要的设置感到困惑,我应该获得适当的可跟踪性和指标详细信息。

我在 Springboot 应用程序中添加了以下依赖项,并使用 2 种方法编写了一个测试控制器。

<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>

application.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

控制器类-

@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");
    }
}

当服务 1 被调用时(http://localhost:8090/method1?input=testdata),它在内部调用 service2 并生成 TraceId 和 SpanId,但对于每个服务,它会生成不同的 TraceId,如下所示 -

. INFO [datadog-sample,652553b7e89ee89b58c1c37b35cb6102,58c1c37b35cb6102] - Inside the method1 with data = testdata
. INFO [datadog-sample,652553b7ec4d43c0f0e090c94225d91c,f0e090c94225d91c] - Inside the method2

这不应该是单个TraceId搭配多个SpanId吗,这样就可以轻松追踪流向了。我是否需要在任何地方使用 @Obeserved 注释,因为我不想自定义任何行为?

要将指标/可观察性详细信息发送到 datadog,以便除了包含 datadog 特定依赖项并与我的应用程序一起运行代理之外,我还需要在代码或配置中执行任何特定操作?

微米可观测性对于 springcloud-kafka-binder 应用程序来说是开箱即用的还是需要任何特定配置?有这方面的参考例子吗?

java spring-boot spring-cloud datadog spring-micrometer
1个回答
0
投票

SpringBoot 3 的可观察性 API

我假设您正在谈论 Spring Cloud Sleuth,尽管我在

pom.xml
的片段中没有看到这样的依赖关系。

如果是这样,那就不足为奇了。您正在

method1
中发出新的 HTTP 请求。因此,您也会有不同的
traceId
spanId
,因为默认情况下,新的跟踪和跨度附加到传入的 http 请求线程(当然有很多条件等,但一般情况就是这样)。这样你就有了一个新的轨迹和跨度。

因此,为了保留跟踪,您可以简单地在同一线程中进行操作,只需调用方法而不是发出 http 请求。

PS:如果需要,Trace 还可以传播到新创建的线程,例如从

ThreadPool
@Async
创建的线程。但这是另一个话题了。我建议您熟悉文档的传播部分

© www.soinside.com 2019 - 2024. All rights reserved.