Search in sources :

Example 16 with Subsegment

use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.

the class LambdaSegmentContext method endSubsegment.

@Override
public void endSubsegment(AWSXRayRecorder recorder) {
    Entity current = getTraceEntity();
    if (current instanceof Subsegment) {
        if (logger.isDebugEnabled()) {
            logger.debug("Ending subsegment named: " + current.getName());
        }
        Subsegment currentSubsegment = (Subsegment) current;
        currentSubsegment.end();
        if (recorder.getStreamingStrategy().requiresStreaming(currentSubsegment.getParentSegment())) {
            recorder.getStreamingStrategy().streamSome(currentSubsegment.getParentSegment(), recorder.getEmitter());
        }
        Entity parentEntity = current.getParent();
        if (parentEntity instanceof FacadeSegment) {
            if (((FacadeSegment) parentEntity).isSampled()) {
                current.getCreator().getEmitter().sendSubsegment((Subsegment) current);
            }
            clearTraceEntity();
        } else {
            setTraceEntity(current.getParent());
        }
    } else {
        throw new SubsegmentNotFoundException("Failed to end a subsegment: subsegment cannot be found.");
    }
}
Also used : Entity(com.amazonaws.xray.entities.Entity) SubsegmentNotFoundException(com.amazonaws.xray.exceptions.SubsegmentNotFoundException) Subsegment(com.amazonaws.xray.entities.Subsegment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment)

Example 17 with Subsegment

use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.

the class AWSXRayRecorderTest method testGetTraceEntityReturnsCurrentSubsegment.

@Test
public void testGetTraceEntityReturnsCurrentSubsegment() {
    AWSXRay.beginSegment("test");
    Subsegment subsegment = AWSXRay.beginSubsegment("test");
    Assert.assertEquals(subsegment, AWSXRay.getTraceEntity());
    AWSXRay.endSubsegment();
    AWSXRay.endSegment();
}
Also used : Subsegment(com.amazonaws.xray.entities.Subsegment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 18 with Subsegment

use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.

the class AWSXRayRecorderTest method testSubsegmentWithChildEmittedTogetherInLambdaContext.

@Test
public void testSubsegmentWithChildEmittedTogetherInLambdaContext() {
    TraceHeader header = TraceHeader.fromString(TRACE_HEADER);
    PowerMockito.stub(PowerMockito.method(LambdaSegmentContext.class, "getTraceHeaderFromEnvironment")).toReturn(header);
    PowerMockito.stub(PowerMockito.method(LambdaSegmentContextResolver.class, "getLambdaTaskRoot")).toReturn("/var/task");
    Emitter mockEmitter = Mockito.mock(UDPEmitter.class);
    AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard().withEmitter(mockEmitter).build();
    recorder.createSubsegment("testTogether", () -> {
        recorder.createSubsegment("testTogether2", () -> {
        });
    });
    ArgumentCaptor<Subsegment> emittedSubsegment = ArgumentCaptor.forClass(Subsegment.class);
    Mockito.verify(mockEmitter, Mockito.times(1)).sendSubsegment(emittedSubsegment.capture());
    Subsegment captured = emittedSubsegment.getValue();
    Assert.assertEquals(1, captured.getSubsegments().size());
}
Also used : TraceHeader(com.amazonaws.xray.entities.TraceHeader) UDPEmitter(com.amazonaws.xray.emitters.UDPEmitter) Emitter(com.amazonaws.xray.emitters.Emitter) Subsegment(com.amazonaws.xray.entities.Subsegment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 19 with Subsegment

use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.

the class AWSXRayRecorderTest method testSubsequentSubsegmentBranchesEmittedInLambdaContext.

@Test
public void testSubsequentSubsegmentBranchesEmittedInLambdaContext() {
    TraceHeader header = TraceHeader.fromString(TRACE_HEADER);
    PowerMockito.stub(PowerMockito.method(LambdaSegmentContext.class, "getTraceHeaderFromEnvironment")).toReturn(header);
    PowerMockito.stub(PowerMockito.method(LambdaSegmentContextResolver.class, "getLambdaTaskRoot")).toReturn("/var/task");
    Emitter mockEmitter = Mockito.mock(UDPEmitter.class);
    AWSXRayRecorder recorder = AWSXRayRecorderBuilder.standard().withEmitter(mockEmitter).build();
    recorder.createSubsegment("testTogether", () -> {
        recorder.createSubsegment("testTogether2", () -> {
        });
    });
    recorder.createSubsegment("testTogether3", () -> {
        recorder.createSubsegment("testTogether4", () -> {
        });
    });
    ArgumentCaptor<Subsegment> emittedSubsegments = ArgumentCaptor.forClass(Subsegment.class);
    Mockito.verify(mockEmitter, Mockito.times(2)).sendSubsegment(emittedSubsegments.capture());
    List<Subsegment> captured = emittedSubsegments.getAllValues();
    captured.forEach((capturedSubsegment) -> {
        Assert.assertEquals(1, capturedSubsegment.getSubsegments().size());
    });
}
Also used : TraceHeader(com.amazonaws.xray.entities.TraceHeader) UDPEmitter(com.amazonaws.xray.emitters.UDPEmitter) Emitter(com.amazonaws.xray.emitters.Emitter) Subsegment(com.amazonaws.xray.entities.Subsegment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 20 with Subsegment

use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.

the class AWSXRayRecorderTest method testGetThreadLocalReturnsCurrentSubsegment.

@Test
public void testGetThreadLocalReturnsCurrentSubsegment() {
    AWSXRay.beginSegment("test");
    Subsegment subsegment = AWSXRay.beginSubsegment("test");
    Assert.assertEquals(subsegment, AWSXRay.getThreadLocal());
    AWSXRay.endSubsegment();
    AWSXRay.endSegment();
}
Also used : Subsegment(com.amazonaws.xray.entities.Subsegment) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

Subsegment (com.amazonaws.xray.entities.Subsegment)27 Segment (com.amazonaws.xray.entities.Segment)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)8 Test (org.junit.Test)7 TraceHeader (com.amazonaws.xray.entities.TraceHeader)4 Emitter (com.amazonaws.xray.emitters.Emitter)3 UDPEmitter (com.amazonaws.xray.emitters.UDPEmitter)3 Entity (com.amazonaws.xray.entities.Entity)3 FacadeSegment (com.amazonaws.xray.entities.FacadeSegment)3 SubsegmentImpl (com.amazonaws.xray.entities.SubsegmentImpl)3 TraceID (com.amazonaws.xray.entities.TraceID)2 AmazonServiceException (com.amazonaws.AmazonServiceException)1 SegmentImpl (com.amazonaws.xray.entities.SegmentImpl)1 SegmentNotFoundException (com.amazonaws.xray.exceptions.SegmentNotFoundException)1 SubsegmentNotFoundException (com.amazonaws.xray.exceptions.SubsegmentNotFoundException)1 Around (org.aspectj.lang.annotation.Around)1