我正在尝试在新的(ish)Spring Boot应用程序中设置AWS XRay。

我想在将其投入生产之前在本地进行测试。该应用程序在本地与dynamoDB客户端和STS一起运行,以承担调用dynamo的角色。

服务启动正常,加载了所有bean,但是当我调用该服务时,它失败了

Failed to begin subsegment named 'AWSSecurityTokenService': segment cannot be found


尝试调用dynamoDB保存项目时。我以为,如果您添加了aws-xray-recorder-sdk-aws-sdy依赖项,那么像AWSSecurityTokenService这样的软件包会在请求期间自动获取上下文子段?我想念什么?

我已经将以下内容添加到pom.xml中:

    <!-- AWS XRay -->
      <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.amazonaws</groupId>
        <artifactId>aws-xray-recorder-sdk-bom</artifactId>
        <version>1.3.1</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

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


以及以下方面:

import java.util.HashMap;
import java.util.Map;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import com.amazonaws.xray.AWSXRay;
import com.amazonaws.xray.entities.Subsegment;

@Aspect
@Component
@Profile("!test")
public class XRayInspector {

    private static final Logger LOG = LoggerFactory.getLogger(XRayInspector.class);

    @Around("!execution(* com.things.service.config..*(..)) && " +
            "execution(* com.things.service.controller..*(..)) && " +
            "execution(* com.things.service.factory..*(..)) && " +
            "execution(* com.things.service.exceptions..*(..)) && " +
            "execution(* com.things.service.security..*(..)) && " +
            "execution(* com.things.service.model..*(..)) && " +
            "execution(* com.things.service.service..*(..))")
    public Object logExecutionTime(final ProceedingJoinPoint joinPoint) throws Throwable {
        return this.processXRayTrace(joinPoint);
    }

    protected Object processXRayTrace(final ProceedingJoinPoint pjp) throws Throwable {
        try {
            final Subsegment subsegment = AWSXRay.beginSubsegment(pjp.getSignature().getName());
            subsegment.setMetadata(generateMetadata(pjp, subsegment));
            return conditionalProceed(pjp);
        } catch (final Exception e) {
            AWSXRay.getCurrentSegment().addException(e);
            throw e;
        } finally {
            LOG.trace("Ending Subsegment");
            AWSXRay.endSubsegment();
        }
    }

    public static Object conditionalProceed(final ProceedingJoinPoint pjp) throws Throwable {
        if (pjp.getArgs().length == 0) {
            return pjp.proceed();
        } else {
            return pjp.proceed(pjp.getArgs());
        }
    }

    public static Map<String, Map<String, Object>> generateMetadata(final ProceedingJoinPoint pjp, final Subsegment subsegment) {
        final Map<String, Map<String, Object>> metadata = new HashMap<>();
        final Map<String, Object> classInfo = new HashMap<>();
        classInfo.put("Class", pjp.getTarget().getClass().getSimpleName());
        metadata.put("ClassInfo", classInfo);
        return metadata;
    }

}

最佳答案

您有初始化代码吗?如果在请求真正进入启用X-Ray的服务之前(或超出请求的范围)进行STS调用,这将导致丢失上下文异常。我们建议您忽略所有不在请求路径之外的调用(使用未跟踪的AWS客户端),或者将AWS_XRAY_CONTEXT_MISSING env变量设置为LOG_ERROR:

https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-java-configuration.html#xray-sdk-java-configuration-envvars

08-07 14:12