use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class DefaultStreamingStrategyTest method testStreamSomeChildrenRemovedFromParent.
// test to see if the correct actions are being taken in streamSome (children get removed from parent)
@Test
public void testStreamSomeChildrenRemovedFromParent() {
TraceID traceId = new TraceID();
DefaultStreamingStrategy defaultStreamingStrategy = new DefaultStreamingStrategy(1);
Segment bigSegment = new SegmentImpl(AWSXRay.getGlobalRecorder(), "big", traceId);
bigSegment.setStartTime(1.0);
for (int i = 0; i < 5; i++) {
Subsegment subsegment = new SubsegmentImpl(AWSXRay.getGlobalRecorder(), "child" + i, bigSegment);
subsegment.setStartTime(1.0);
bigSegment.addSubsegment(subsegment);
subsegment.end();
}
Assert.assertTrue(defaultStreamingStrategy.requiresStreaming(bigSegment));
defaultStreamingStrategy.streamSome(bigSegment, AWSXRay.getGlobalRecorder().getEmitter());
Assert.assertTrue(bigSegment.getTotalSize().intValue() == 0);
}
use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class TracingInterceptorTest method testAsyncLambdaInvokeSubsegmentContainsFunctionName.
@Test
public void testAsyncLambdaInvokeSubsegmentContainsFunctionName() {
SdkAsyncHttpClient mockClient = mockSdkAsyncHttpClient(generateLambdaInvokeResponse(200));
LambdaAsyncClient client = LambdaAsyncClient.builder().httpClient(mockClient).endpointOverride(URI.create("http://example.com")).region(Region.of("us-west-42")).credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create("key", "secret", "session"))).overrideConfiguration(ClientOverrideConfiguration.builder().addExecutionInterceptor(new TracingInterceptor()).build()).build();
Segment segment = AWSXRay.getCurrentSegment();
client.invoke(InvokeRequest.builder().functionName("testFunctionName").build()).join();
Assert.assertEquals(1, segment.getSubsegments().size());
Subsegment subsegment = segment.getSubsegments().get(0);
Map<String, Object> awsStats = subsegment.getAws();
@SuppressWarnings("unchecked") Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
Assert.assertEquals("Invoke", awsStats.get("operation"));
Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
Assert.assertEquals("extended", awsStats.get("id_2"));
Assert.assertEquals("Failure", awsStats.get("function_error"));
Assert.assertEquals("us-west-42", awsStats.get("region"));
Assert.assertEquals(0, awsStats.get("retries"));
Assert.assertEquals(2L, httpResponseStats.get("content_length"));
Assert.assertEquals(200, httpResponseStats.get("status"));
Assert.assertEquals(false, subsegment.isInProgress());
}
use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class LambdaSegmentContext method beginSubsegment.
@Override
public Subsegment beginSubsegment(AWSXRayRecorder recorder, String name) {
if (logger.isDebugEnabled()) {
logger.debug("Beginning subsegment named: " + name);
}
Entity entity = getTraceEntity();
if (entity == null) {
// First subsgment of a subsegment branch.
Segment parentSegment = newFacadeSegment(recorder, name);
Subsegment subsegment = parentSegment.isRecording() ? new SubsegmentImpl(recorder, name, parentSegment) : Subsegment.noOp(parentSegment, recorder);
subsegment.setParent(parentSegment);
// Enable FacadeSegment to keep track of its subsegments for subtree streaming
parentSegment.addSubsegment(subsegment);
setTraceEntity(subsegment);
return subsegment;
} else {
// Continuation of a subsegment branch.
Subsegment parentSubsegment = (Subsegment) entity;
// Ensure customers have not leaked subsegments across invocations
TraceID environmentRootTraceId = LambdaSegmentContext.getTraceHeaderFromEnvironment().getRootTraceId();
if (environmentRootTraceId != null && !environmentRootTraceId.equals(parentSubsegment.getParentSegment().getTraceId())) {
clearTraceEntity();
return beginSubsegment(recorder, name);
}
Segment parentSegment = parentSubsegment.getParentSegment();
Subsegment subsegment = parentSegment.isRecording() ? new SubsegmentImpl(recorder, name, parentSegment) : Subsegment.noOp(parentSegment, recorder);
subsegment.setParent(parentSubsegment);
parentSubsegment.addSubsegment(subsegment);
setTraceEntity(subsegment);
List<SegmentListener> segmentListeners = recorder.getSegmentListeners();
segmentListeners.stream().filter(Objects::nonNull).forEach(listener -> listener.onBeginSubsegment(subsegment));
return subsegment;
}
}
use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class TracingInterceptorTest method testLambdaInvokeSubsegmentContainsFunctionName.
@Test
public void testLambdaInvokeSubsegmentContainsFunctionName() throws Exception {
SdkHttpClient mockClient = mockSdkHttpClient(generateLambdaInvokeResponse(200));
LambdaClient client = LambdaClient.builder().httpClient(mockClient).endpointOverride(URI.create("http://example.com")).region(Region.of("us-west-42")).credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create("key", "secret", "session"))).overrideConfiguration(ClientOverrideConfiguration.builder().addExecutionInterceptor(new TracingInterceptor()).build()).build();
Segment segment = AWSXRay.getCurrentSegment();
client.invoke(InvokeRequest.builder().functionName("testFunctionName").build());
Assert.assertEquals(1, segment.getSubsegments().size());
Subsegment subsegment = segment.getSubsegments().get(0);
Map<String, Object> awsStats = subsegment.getAws();
@SuppressWarnings("unchecked") Map<String, Object> httpResponseStats = (Map<String, Object>) subsegment.getHttp().get("response");
Assert.assertEquals("Invoke", awsStats.get("operation"));
Assert.assertEquals("testFunctionName", awsStats.get("function_name"));
Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
Assert.assertEquals("extended", awsStats.get("id_2"));
Assert.assertEquals("Failure", awsStats.get("function_error"));
Assert.assertEquals("us-west-42", awsStats.get("region"));
Assert.assertEquals(0, awsStats.get("retries"));
Assert.assertEquals(2L, httpResponseStats.get("content_length"));
Assert.assertEquals(200, httpResponseStats.get("status"));
Assert.assertEquals(false, subsegment.isInProgress());
}
use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class TracingInterceptorTest method testNoHeaderAddedWhenPropagationOff.
@Test
public void testNoHeaderAddedWhenPropagationOff() {
Subsegment subsegment = Subsegment.noOp(AWSXRay.getGlobalRecorder(), false);
TracingInterceptor interceptor = new TracingInterceptor();
Context.ModifyHttpRequest context = Mockito.mock(Context.ModifyHttpRequest.class);
SdkHttpRequest mockRequest = Mockito.mock(SdkHttpRequest.class);
SdkHttpRequest.Builder mockRequestBuilder = Mockito.mock(SdkHttpRequest.Builder.class);
when(context.httpRequest()).thenReturn(mockRequest);
Mockito.lenient().when(context.httpRequest().toBuilder()).thenReturn(mockRequestBuilder);
ExecutionAttributes attributes = new ExecutionAttributes();
attributes.putAttribute(TracingInterceptor.entityKey, subsegment);
interceptor.modifyHttpRequest(context, attributes);
verify(mockRequest.toBuilder(), never()).appendHeader(anyString(), anyString());
}
Aggregations