我是第一次使用 Spring Boot,并且正在为我的应用程序设置分布式跟踪。我已将 Spring Cloud Slueth 添加到我的应用程序中,我可以看到在调用我的端点时生成的跨度和跟踪,但是我很难让它与 Aws Sdk 2.x 集成(使用 Dynamo 异步客户端)。我有几个关于集成的问题:
- 通过 aws sdk 跟踪 http 调用的最佳方法是什么。我能够做到的唯一方法是实现 ExecutionInterceptor 并手动创建一个跨度:
@Component
@Log4j2
public class AwsExecutionInterceptor implements ExecutionInterceptor {
public static final String CLIENT_CALL_SPAN = "ClientCallSpan";
public static final ExecutionAttribute<Span> ATTRIBUTE = new ExecutionAttribute<>(
CLIENT_CALL_SPAN);
private final Tracer tracer;
@Autowired
public AwsExecutionInterceptor(Tracer tracer) {
this.tracer = tracer;
}
@Override
public void beforeExecution(Context.BeforeExecution context,ExecutionAttributes executionAttributes) {
Span clientCallSpan = this.tracer.nextSpan().name("clientCall").start();
log.info("Starting new span");
executionAttributes.putAttribute(ATTRIBUTE,clientCallSpan);
}
@Override
public void afterExecution(Context.AfterExecution context,ExecutionAttributes executionAttributes) {
Span span = executionAttributes.getattribute(ATTRIBUTE);
span.end();
log.info("Span closed");
}
}
- 有没有办法让跟踪 ID 从通过 aws sdk 记录的 netty 线程池传播到日志?我通过高级选项提供了一个执行程序,但跟踪不会从 netty 线程传播到日志。下面是一个例子:
public class AwsExecutorConfig {
@Bean("AwsClientExecutor")
public Executor executor() {
ThreadPoolExecutor executor = new ThreadPoolExecutor(50,50,10,TimeUnit.SECONDS,new LinkedBlockingQueue<>(10_000));
executor.allowCoreThreadTimeOut(true);
return executor;
}
}
@Autowired
public DynamoConfig(AwsExecutionInterceptor executionInterceptor,@Qualifier("AwsClientExecutor") Executor executor) {
@Bean(destroyMethod = "close")
public DynamoDbAsyncClient dynamoDbAsyncClient() {
var builder = DynamoDbAsyncClient.builder();
builder.overrideConfiguration(
ClientOverrideConfiguration.builder()
.addExecutionInterceptor(executionInterceptor).build());
builder.asyncConfiguration(
b -> b.advancedOption(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR,executor));
return builder.build();
}
}
这样,我可以从 sdk 中看到如下日志,
2021-07-20 10:26:04.369 DEBUG [,c9ae653fd2254266,c9ae653fd2254266] 2873 --- [pool-2-thread-5] s.a.a.m.internal.DefaultMetricCollector : Collected metrics records: MetricCollection(...)
但其他日志没有任何痕迹,例如:
2021-07-20 10:26:04.360 DEBUG [,] 2873 --- [tyEventLoop-0-2] software.amazon.awssdk.request : Received successful response: 200
2021-07-20 10:26:04.361 TRACE [,] 2873 --- [tyEventLoop-0-2] software.amazon.awssdk.request : Parsing service response JSON.
2021-07-20 10:26:04.361 DEBUG [,] 2873 --- [tyEventLoop-0-2] software.amazon.awssdk.requestId : x-amzn-RequestId : af0d9968-3202-480f-9e3c-8312ae6963c8
2021-07-20 10:26:04.361 DEBUG [,] 2873 --- [tyEventLoop-0-2] software.amazon.awssdk.requestId : x-amz-id-2 : not available
2021-07-20 10:26:04.367 TRACE [,] 2873 --- [tyEventLoop-0-2] software.amazon.awssdk.request : Done parsing service response.
spring cloud sleuth 文档提到了与 Netty Http 客户端的集成,但我不确定如何使用 AWS SDK 做到这一点。对此的任何帮助将不胜感激