如何将 X-Ray 支持用于 Java V2 的 AWS 开发工具包和 EventBridgeClient

How to use X-Ray support for the AWS SDK for Java V2 with EventBridgeClient

根据文档 here,它只是添加了一个 Maven 模块 aws-xray-recorder-sdk-aws-sdk-v2。然后,个别客户将能够使用 TracingInterceptor.

进行 X 射线追踪

我确实在我的 pom.xml 中添加了 sdk-coresdk-v2,但是我的 EventBridgeClient 而不是 显示 addExecutionInterceptor 添加 TracingInterceptor.

已添加依赖项。

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-aws-sdk-v2</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency> 
  <groupId>com.amazonaws</groupId> 
  <artifactId>aws-xray-recorder-sdk-core</artifactId> 
  <version>2.8.0</version> 
</dependency> 

EventBridge 客户端

return EventBridgeClient.builder()
    .region(Region.of(System.getenv("AWS_REGION")))
    .credentialsProvider(EnvironmentVariableCredentialsProvider.create())
    .overrideConfiguration(
        ClientOverrideConfiguration.builder()
            .apiCallAttemptTimeout(Duration.ofSeconds(1))
            .retryPolicy(RetryPolicy.builder().numRetries(10).build())
            .build())
    .httpClientBuilder(UrlConnectionHttpClient.builder())
    .build();

addExecutionInterceptor() 方法不属于 EventBridgeClient 服务客户端。它属于 ClientOverrideConfiguration 对象,然后在我们创建 EventBridgeClient Service Client 时使用。这是 Java 代码:

package com.example.xray;

import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.eventbridge.EventBridgeClient;
import software.amazon.awssdk.services.eventbridge.model.PutRuleRequest;
import software.amazon.awssdk.services.eventbridge.model.PutRuleResponse;
import software.amazon.awssdk.services.eventbridge.model.EventBridgeException;
import com.amazonaws.xray.interceptors.TracingInterceptor;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;

public class EventClientExample {

    public static void main(String[] args) {


        ClientOverrideConfiguration conf=  ClientOverrideConfiguration.builder()
                .addExecutionInterceptor(new TracingInterceptor())
                .build();

        Region region = Region.US_WEST_2;
        EventBridgeClient eventBrClient = EventBridgeClient.builder()
                .region(region)
                .overrideConfiguration(conf)
                .build();


        eventBrClient.close();
    }

}

POM 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>XRayProjectV2</groupId>
    <artifactId>XRayProjectV2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
            </plugin>
        </plugins>
    </build>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>2.15.14</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.4.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-commons</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>eventbridge</artifactId>
        </dependency>
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-launcher</artifactId>
            <version>1.4.0</version>
            <scope>test</scope>
        </dependency>
         <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>xray</artifactId>
         </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-xray-recorder-sdk-aws-sdk-v2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-xray-recorder-sdk-core</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>
</project>