use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class TracingInterceptorTest method testAsyncThrottledException.
@Test
public void testAsyncThrottledException() {
SdkAsyncHttpClient mockClient = mockSdkAsyncHttpClient(generateLambdaInvokeResponse(429));
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();
try {
client.invoke(InvokeRequest.builder().functionName("testFunctionName").build()).get();
} catch (Exception e) {
// ignore exceptions
} finally {
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");
Cause cause = subsegment.getCause();
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("us-west-42", awsStats.get("region"));
Assert.assertEquals(2L, httpResponseStats.get("content_length"));
Assert.assertEquals(429, httpResponseStats.get("status"));
Assert.assertEquals(true, subsegment.isError());
Assert.assertEquals(true, subsegment.isThrottle());
Assert.assertEquals(false, subsegment.isFault());
Assert.assertEquals(1, cause.getExceptions().size());
Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
}
}
use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class TracingInterceptorTest method testResponseDescriptors.
@Test
public void testResponseDescriptors() throws Exception {
String responseBody = "{\"LastEvaluatedTableName\":\"baz\",\"TableNames\":[\"foo\",\"bar\",\"baz\"]}";
SdkHttpResponse mockResponse = SdkHttpResponse.builder().statusCode(200).putHeader("x-amzn-requestid", "1111-2222-3333-4444").putHeader("Content-Length", "84").putHeader("Content-Type", "application/x-amz-json-1.0").build();
SdkHttpClient mockClient = mockSdkHttpClient(mockResponse, responseBody);
DynamoDbClient client = DynamoDbClient.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.listTables(ListTablesRequest.builder().limit(3).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("ListTables", awsStats.get("operation"));
Assert.assertEquals(3, awsStats.get("limit"));
Assert.assertEquals("1111-2222-3333-4444", awsStats.get("request_id"));
Assert.assertEquals(3, awsStats.get("table_count"));
Assert.assertEquals("us-west-42", awsStats.get("region"));
Assert.assertEquals(0, awsStats.get("retries"));
Assert.assertEquals(84L, 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 AWSXRayRecorder method getCurrentSegmentOptional.
/**
* @return the current segment, or {@code Optional.empty()} if there is no segment
*/
public Optional<Segment> getCurrentSegmentOptional() {
// explicitly do not throw context missing exceptions from optional-returning methods
SegmentContext context = segmentContextResolverChain.resolve();
if (null == context) {
return Optional.empty();
}
Entity current = context.getTraceEntity();
if (current instanceof Segment) {
return Optional.of((Segment) current);
} else if (current instanceof Subsegment) {
return Optional.of(current.getParentSegment());
} else {
return Optional.empty();
}
}
use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class AWSXRayRecorder method getCurrentSubsegment.
/**
* @throws SegmentNotFoundException
* if {@code contextMissingStrategy} throws exceptions and the segment context cannot be found
* @throws SubsegmentNotFoundException
* if {@code contextMissingStrategy} throws exceptions and the current segment has no subsegments in progress
* @return the current subsegment, or {@code null} if {@code contextMissingStrategy} suppresses exceptions and the segment
* context cannot be found or the segment has no subsegments in progress
*/
@Nullable
public Subsegment getCurrentSubsegment() {
SegmentContext context = getSegmentContext();
if (context == null) {
return null;
}
Entity current = context.getTraceEntity();
if (current == null) {
contextMissingStrategy.contextMissing("No segment in progress.", SegmentNotFoundException.class);
} else if (current instanceof Subsegment) {
return (Subsegment) current;
} else {
contextMissingStrategy.contextMissing("No subsegment in progress.", SubsegmentNotFoundException.class);
}
return null;
}
use of com.amazonaws.xray.entities.Subsegment in project aws-xray-sdk-java by aws.
the class TracingInterceptor method beforeExecution.
@Override
public void beforeExecution(Context.BeforeExecution context, ExecutionAttributes executionAttributes) {
AWSXRayRecorder recorder = getRecorder();
Entity origin = recorder.getTraceEntity();
Subsegment subsegment = recorder.beginSubsegment(executionAttributes.getAttribute(SdkExecutionAttribute.SERVICE_NAME));
subsegment.setNamespace(Namespace.AWS.toString());
subsegment.putAws(EntityDataKeys.AWS.OPERATION_KEY, executionAttributes.getAttribute(SdkExecutionAttribute.OPERATION_NAME));
Region region = executionAttributes.getAttribute(AwsExecutionAttribute.AWS_REGION);
if (region != null) {
subsegment.putAws(EntityDataKeys.AWS.REGION_KEY, region.id());
}
subsegment.putAllAws(extractRequestParameters(context, executionAttributes));
if (accountId != null) {
subsegment.putAws(EntityDataKeys.AWS.ACCOUNT_ID_SUBSEGMENT_KEY, accountId);
}
recorder.setTraceEntity(origin);
// store the subsegment in the AWS SDK's executionAttributes so it can be accessed across threads
executionAttributes.putAttribute(entityKey, subsegment);
}
Aggregations