3

添加aws-xray-recorder-sdk-aws-sdk-instrumentorKCL 时会引发SegmentNotFoundException.

据我了解,这是因为 KCL 正在启动自己的线程,因此我的调用AWSXRay.beginSegment()不适用于在这些线程上运行的检测请求,但我无法直接控制这些线程,因此我无法设置上下文/段。

有什么解决方法吗?

4

1 回答 1

3

KCL 允许提供您自己的实例化AmazonKinesis客户端、AmazonDynamoDb客户端和AmazonCloudWatch客户端。

您可以实例化您自己的客户端(使用AmazonKinesisClientBuilder等),添加一个并withRequestHandler()提供一个调用和调用的IRequestHandler2实例。这样,请求处理程序就有机会在不同 KCL ExecutorService 实例正在创建的线程上运行您自己的用户代码。AWSXRay.beginSegment()beforeRequest()AWSXRay.endSegment()afterResponse()

此类请求处理程序的示例:

class XRayTracingHandler extends TracingHandler {
    private final String name;

    XRayTracingHandler(String name) {
        super(AWSXRay.getGlobalRecorder());
        this.name = name;
    }

    @Override
    public void beforeRequest(Request<?> request) {
        AWSXRay.beginSegment(name);
        super.beforeRequest(request);
    }

    @Override
    public void afterResponse(Request<?> request, Response<?> response) {
        super.afterResponse(request, response);
        AWSXRay.endSegment();
    }
}

你可以这样使用:

  final KinesisClientLibConfiguration config = new KinesisClientLibConfiguration(
    "my-app-name",
    "my-kinesis-stream",
    new DefaultAWSCredentialsProviderChain(), UUID.randomUUID());


  final IRecordProcessorFactory recordProcessorFactory = ...


  final AmazonCloudWatch cloudWatchClient = AmazonCloudWatchClientBuilder
    .standard()
    .withRequestHandlers(new XRayTracingHandler("my-segment-listener"))
    .build();

  final AmazonKinesis kinesisClient = AmazonKinesisClientBuilder
    .standard()
    .withRequestHandlers(new XRayTracingHandler("my-segment-listener"))
    .build();

  final AmazonDynamoDB dynamoDBClient = AmazonDynamoDBClientBuilder
    .standard()
    .withRequestHandlers(new XRayTracingHandler("my-segment-listener"))
    .build();

  final Worker worker = new Worker.Builder()
    .recordProcessorFactory(recordProcessorFactory)
    .config(config)
    .kinesisClient(kinesisClient)
    .dynamoDBClient(dynamoDBClient)
    .cloudWatchClient(cloudWatchClient)
    .build();
于 2018-03-14T14:25:57.830 回答