如何将Java Kinesis Client Library与X-Ray一起使用?

时间:2018-03-14 14:25:58

标签: amazon-kinesis aws-xray

添加aws-xray-recorder-sdk-aws-sdk-instrumentor时,KCL会引发SegmentNotFoundException

据我所知,这是因为KCL正在启动their research paper所以我对AWSXRay.beginSegment()的调用不适用于在这些线程上运行的已检测请求,但我没有直接控制这些线程所以我无法设置上下文/段。

有没有解决方法呢?

1 个答案:

答案 0 :(得分:2)

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

您可以实例化自己的客户(使用own threads等),添加AmazonKinesisClientBuilder并在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(); } } 。通过这种方式,请求处理程序提供了在不同KCL ExecutorService实例创建的线程上运行您自己的用户代码的机会。

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

  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();

你可以像这样使用:

{{1}}