我们正在将XRAY添加到我们的Spring Boot应用程序中,但无法解决以下错误:
Failed to begin subsegment named 'Amazon S3': segment cannot be found.
这是我们代码的相关部分:
pom.xml:
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-core</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-aws-sdk-instrumentor</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-apache-http</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-spring</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-xray-recorder-sdk-sql-postgres</artifactId>
<version>1.2.1</version>
</dependency>
SpringApplication.java
@Bean
public Filter TracingFilter() {
return new AWSXRayServletFilter("myService");
}
打电话给S3的班级
@PostConstruct
public void runOnStartup(){
String fileName = "myFileName";
String bucketName = "myBucketName";
amazonS3Client = AmazonS3ClientBuilder.standard()
.withCredentials(new ProfileCredentialsProvider("MyCredentials"))
.withClientConfiguration(getClientConfiguration())
.withRegion(region)
.build();
Segment segment = AWSXRay.beginSegment("do-startup-operation");
S3Object s3Object = amazonS3Client.getObject(bucketName, fileName);
AWSXRay.endSegment();
//Do stuff with S3Object
}
到目前为止,我已经尝试过:
1)我尝试了是否使用sdk-aws-sdk-instrumentor导入,并按this question和this documentation.所述添加了TracingHandler配置
.withRequestHandlers(new TracingHandler(AWSXRay.getGlobalRecorder()))
2)我发现this thread似乎建议使用AWSXRAY.createSegment(),但是我不确定lambda中会发生什么或者与我的场景是否相关
我已阅读并发现相关的其他文档/代码:
https://docs.aws.amazon.com/xray/latest/devguide/scorekeep-startup.html
https://docs.aws.amazon.com/xray/latest/devguide/scorekeep-sdkclients.html
https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-multithreading.html
https://docs.aws.amazon.com/xray/latest/devguide/scorekeep-workerthreads.html
https://github.com/aws/aws-xray-sdk-java/blob/master/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/contexts/ThreadLocalSegmentContext.java#L23
附言我简化了代码,并省略了一些错误处理,以使那些查看此问题的人更容易阅读
最佳答案
X-Ray Servlet过滤器在收到请求时将打开一个段,并在返回响应之前将其关闭。它创建的段代表完整的请求/响应生命周期。作为服务此请求的一部分而捕获的任何内容(在这种情况下,可能是一些AWS服务调用)都称为子细分。如您所见,子段需要跟踪上下文(此段用于该段)。
问题在于,在服务器启动时,工具会尝试捕获S3调用,但是由于没有请求进入,因此它找不到上下文。一种选择是将环境变量AWS_XRAY_CONTEXT_MISSING
设置为LOG_ERROR
,因此没有例外,只有一个日志条目。在https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-configuration.html#xray-sdk-java-configuration-envvars上查看更多详细信息。
为了在lambda上运行,lambda容器将为每次调用创建一个段。它将跟踪上下文设置为环境变量。因此,只要捕获的代码在处理程序类中,上下文就应该始终存在。
关于amazon-web-services - AWS XRAY SDK问题:无法开始名为“Amazon S3”的子分段:找不到分段,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53841672/