如何将 Java Kinesis 客户端库与 X-Ray 一起使用? [英] How can use the Java Kinesis Client Library together with X-Ray?

查看:23
本文介绍了如何将 Java Kinesis 客户端库与 X-Ray 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When adding aws-xray-recorder-sdk-aws-sdk-instrumentor the KCL raises a SegmentNotFoundException.

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

As I understand this is because the KCL is starting their own threads so my calls to AWSXRay.beginSegment() do not apply to the instrumented requests that are run on those threads but I have not direct control over those threads so I cannot set the context / segment.

有什么解决方法吗?

推荐答案

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

The KCL allows to provide the your own instantiated AmazonKinesis client, AmazonDynamoDb client and AmazonCloudWatch client.

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

You can instantiate your own clients (using AmazonKinesisClientBuilder,etc) , add a withRequestHandler() and provide a IRequestHandler2 instance that calls AWSXRay.beginSegment() on the beforeRequest() and calls AWSXRay.endSegment() on afterResponse(). In that way the request handler gives the opportunity to run your own user code on the threads that the different KCL ExecutorService instances are creating.

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

An example of such request handler:

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

这篇关于如何将 Java Kinesis 客户端库与 X-Ray 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆