【问题标题】:Unable to configure XRay with Spring boot app无法使用 Spring Boot 应用程序配置 XRay
【发布时间】:2021-08-03 03:25:03
【问题描述】:

我有一个 springboot 应用程序,它在 AWS Lambda 中部署为 docker 映像。 该应用程序运行良好,但我想在我的应用程序中引入 X-Ray Traces 以进行调试。 我尝试关注官方 AWS 文档 - https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-aop-spring.html。但无济于事。

POM.XML:-

<dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-xray-recorder-sdk-spring</artifactId>
        </dependency>
        <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>

WebConfig.java

@Configuration
public class WebConfig {
    @Bean
    public Filter TracingFilter(){
        return new AWSXRayServletFilter ("add-participant");
    }
}

XRayInspector.java

@Aspect
@Component
public class XRayInspector extends AbstractXRayInterceptor {

    @Override
    protected Map<String, Map<String, Object>> generateMetadata(ProceedingJoinPoint proceedingJoinPoint, Subsegment subsegment) {
        return super.generateMetadata(proceedingJoinPoint, subsegment);
    }

    @Override
    @Pointcut("@within(com.amazonaws.xray.spring.aop.XRayEnabled) && bean(*)")
    public void xrayEnabledClasses() {
        System.out.println ("I am inside the class");
    }
}

ParticipantService.java

@Service
@XRayEnabled
public class ParticipantService {
public void save (ParticipantRequest participantRequest) {
        Entity mySegment = AWSXRay.beginSegment("save-participant");
        AWSXRay.getGlobalRecorder().setTraceEntity(mySegment);
        ParticipantCreatedEvent participantCreatedEvent = new ParticipantCreatedEvent
                (participantRequest.getEventId (), participantRequest.getTeamId (),
                        participantRequest.getFirstName (), participantRequest.getLastName (),
                        participantRequest.getEmail (), participantRequest.getContactNumber (),
                        participantRequest.getOrganization (), participantJpaRepository.findMaxDisplayOrder (participantRequest.getEventId ()) + 1);
        participant.setEventId (participantRequest.getEventId ());
        participant.save(createParticipantCommand, participantCreatedEvent);
        AWSXRay.endSegment();
    }
}

我收到以下错误:-

 Caused by: com.amazonaws.xray.exceptions.SubsegmentNotFoundException: Failed to end a subsegment: subsegment cannot be found.

at com.amazonaws.xray.contexts.LambdaSegmentContext.endSubsegment(LambdaSegmentContext.java:94) ~[aws-xray-recorder-sdk-core-1.2.1.jar!/:na]

谁能告诉我我做错了什么!

【问题讨论】:

  • 我会记录您的 XRayInspector 类使用的任何内容作为子段,因为该错误是不言自明的。登录后,我会尝试了解它为什么要尝试使用不存在的东西。

标签: java amazon-web-services spring-boot aws-xray


【解决方案1】:

该错误意味着 Spring Boot 应用程序中的某处缺少 X-Ray SDK 尝试关闭的子段。您可以按照异常堆栈找出并确保在关闭它之前始终创建一个子段。另外,我注意到您在服务中创建了一个细分。在 Lambda 中,检测 X-Ray SDK 与检测托管在其他服务上的应用程序略有不同,该段将始终由 Lambda 运行时创建,您需要创建的唯一跟踪上下文是子段。

见:https://docs.aws.amazon.com/lambda/latest/dg/java-tracing.html

【讨论】:

    猜你喜欢
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 2013-10-03
    • 2016-06-12
    • 2018-11-07
    • 1970-01-01
    • 1970-01-01
    • 2018-05-13
    相关资源
    最近更新 更多