diff --git a/aws-xray-recorder-sdk-aws-sdk/src/test/java/com/amazonaws/xray/handlers/TracingHandlerLambdaTest.java b/aws-xray-recorder-sdk-aws-sdk/src/test/java/com/amazonaws/xray/handlers/TracingHandlerLambdaTest.java index c56a02fe..1ca05b71 100644 --- a/aws-xray-recorder-sdk-aws-sdk/src/test/java/com/amazonaws/xray/handlers/TracingHandlerLambdaTest.java +++ b/aws-xray-recorder-sdk-aws-sdk/src/test/java/com/amazonaws/xray/handlers/TracingHandlerLambdaTest.java @@ -73,11 +73,6 @@ public void testSamplingOverrideFalseInLambda() throws Exception { public void testSamplingOverrideTrueInLambda() { Emitter mockedEmitted = Mockito.mock(DefaultEmitter.class); - AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard() - .withSamplingStrategy(new NoSamplingStrategy()) - .withEmitter(mockedEmitted) - .build(); - TraceHeader header = TraceHeader.fromString(TRACE_HEADER); PowerMockito.stub(PowerMockito.method( @@ -85,6 +80,11 @@ public void testSamplingOverrideTrueInLambda() { PowerMockito.stub(PowerMockito.method( LambdaSegmentContextResolver.class, "getLambdaTaskRoot")).toReturn("/var/task"); + AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard() + .withSamplingStrategy(new NoSamplingStrategy()) + .withEmitter(mockedEmitted) + .build(); + Mockito.doAnswer(invocation -> { return true; }).when(mockedEmitted).sendSubsegment(any()); lambdaTestHelper(recorder, "testTrue", true); @@ -95,11 +95,6 @@ public void testSamplingOverrideTrueInLambda() { public void testSamplingOverrideMixedInLambda() { Emitter mockedEmitted = Mockito.mock(DefaultEmitter.class); - AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard() - .withSamplingStrategy(new NoSamplingStrategy()) - .withEmitter(mockedEmitted) - .build(); - TraceHeader header = TraceHeader.fromString(TRACE_HEADER); PowerMockito.stub(PowerMockito.method( @@ -107,6 +102,11 @@ public void testSamplingOverrideMixedInLambda() { PowerMockito.stub(PowerMockito.method( LambdaSegmentContextResolver.class, "getLambdaTaskRoot")).toReturn("/var/task"); + AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard() + .withSamplingStrategy(new NoSamplingStrategy()) + .withEmitter(mockedEmitted) + .build(); + Mockito.doAnswer(invocation -> { return true; }).when(mockedEmitted).sendSubsegment(any()); lambdaTestHelper(recorder, "test1", true); diff --git a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayRecorder.java b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayRecorder.java index 045a5d9e..2a6b0213 100644 --- a/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayRecorder.java +++ b/aws-xray-recorder-sdk-core/src/main/java/com/amazonaws/xray/AWSXRayRecorder.java @@ -164,9 +164,13 @@ public AWSXRayRecorder() { } segmentContextResolverChain = new SegmentContextResolverChain(); - segmentContextResolverChain.addResolver(new LambdaSegmentContextResolver()); - segmentContextResolverChain.addResolver(new ThreadLocalSegmentContextResolver()); - + LambdaSegmentContextResolver lambdaSegmentContextResolver = new LambdaSegmentContextResolver(); + if (lambdaSegmentContextResolver.resolve() != null) { + segmentContextResolverChain.addResolver(lambdaSegmentContextResolver); + } else { + segmentContextResolverChain.addResolver(new ThreadLocalSegmentContextResolver()); + } + segmentListeners = new ArrayList<>(); awsRuntimeContext = new ConcurrentHashMap<>(); diff --git a/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/AWSXRayRecorderTest.java b/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/AWSXRayRecorderTest.java index 08680b7a..812c03a5 100644 --- a/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/AWSXRayRecorderTest.java +++ b/aws-xray-recorder-sdk-core/src/test/java/com/amazonaws/xray/AWSXRayRecorderTest.java @@ -967,10 +967,6 @@ public void testDefaultContextMissingBehaviorEnd() { @Test public void testMalformedTraceId() { - AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard() - .withSamplingStrategy(new NoSamplingStrategy()) - .build(); - TraceHeader malformedHeader = TraceHeader.fromString("malformedTraceID"); PowerMockito.stub(PowerMockito.method( @@ -981,6 +977,10 @@ public void testMalformedTraceId() { LambdaSegmentContextResolver.class, "getLambdaTaskRoot")) .toReturn("/var/task"); + AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard() + .withSamplingStrategy(new NoSamplingStrategy()) + .build(); + recorder.beginSubsegment("Test"); // Sanity checks that this is a NoOpSubsegment. (We cannot compare the instance of the private class directly)