Search in sources :

Example 1 with SdkHttpClient

use of software.amazon.awssdk.http.SdkHttpClient in project aws-xray-sdk-java by aws.

the class TracingInterceptorTest method testThrottledException.

@Test
public void testThrottledException() throws Exception {
    SdkHttpClient mockClient = mockSdkHttpClient(generateLambdaInvokeResponse(429));
    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();
    try {
        client.invoke(InvokeRequest.builder().functionName("testFunctionName").build());
    } catch (Exception e) {
    // ignore SDK errors
    } 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());
    }
}
Also used : LambdaClient(software.amazon.awssdk.services.lambda.LambdaClient) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) Cause(com.amazonaws.xray.entities.Cause) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Subsegment(com.amazonaws.xray.entities.Subsegment) Map(java.util.Map) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 2 with SdkHttpClient

use of software.amazon.awssdk.http.SdkHttpClient in project aws-xray-sdk-java by aws.

the class TracingInterceptorTest method mockSdkHttpClient.

private SdkHttpClient mockSdkHttpClient(SdkHttpResponse response, String body) throws Exception {
    ExecutableHttpRequest abortableCallable = Mockito.mock(ExecutableHttpRequest.class);
    SdkHttpClient mockClient = Mockito.mock(SdkHttpClient.class);
    when(mockClient.prepareRequest(Mockito.any())).thenReturn(abortableCallable);
    when(abortableCallable.call()).thenReturn(HttpExecuteResponse.builder().response(response).responseBody(AbortableInputStream.create(new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)))).build());
    return mockClient;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) ExecutableHttpRequest(software.amazon.awssdk.http.ExecutableHttpRequest)

Example 3 with SdkHttpClient

use of software.amazon.awssdk.http.SdkHttpClient in project aws-xray-sdk-java by aws.

the class TracingInterceptorTest method test500Exception.

@Test
public void test500Exception() throws Exception {
    SdkHttpClient mockClient = mockSdkHttpClient(generateLambdaInvokeResponse(500));
    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();
    try {
        client.invoke(InvokeRequest.builder().functionName("testFunctionName").build());
    } catch (Exception e) {
    // ignore SDK errors
    } 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(500, httpResponseStats.get("status"));
        Assert.assertEquals(false, subsegment.isError());
        Assert.assertEquals(false, subsegment.isThrottle());
        Assert.assertEquals(true, subsegment.isFault());
        Assert.assertEquals(1, cause.getExceptions().size());
        Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
    }
}
Also used : LambdaClient(software.amazon.awssdk.services.lambda.LambdaClient) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) Cause(com.amazonaws.xray.entities.Cause) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Subsegment(com.amazonaws.xray.entities.Subsegment) Map(java.util.Map) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 4 with SdkHttpClient

use of software.amazon.awssdk.http.SdkHttpClient in project aws-xray-sdk-java by aws.

the class TracingInterceptorTest method test400Exception.

@Test
public void test400Exception() throws Exception {
    SdkHttpClient mockClient = mockSdkHttpClient(generateLambdaInvokeResponse(400));
    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();
    try {
        client.invoke(InvokeRequest.builder().functionName("testFunctionName").build());
    } catch (Exception e) {
    // ignore SDK errors
    } 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(0, awsStats.get("retries"));
        Assert.assertEquals(2L, httpResponseStats.get("content_length"));
        Assert.assertEquals(400, httpResponseStats.get("status"));
        Assert.assertEquals(false, subsegment.isInProgress());
        Assert.assertEquals(true, subsegment.isError());
        Assert.assertEquals(false, subsegment.isThrottle());
        Assert.assertEquals(false, subsegment.isFault());
        Assert.assertEquals(1, cause.getExceptions().size());
        Assert.assertEquals(true, cause.getExceptions().get(0).isRemote());
    }
}
Also used : LambdaClient(software.amazon.awssdk.services.lambda.LambdaClient) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) Cause(com.amazonaws.xray.entities.Cause) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Subsegment(com.amazonaws.xray.entities.Subsegment) Map(java.util.Map) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 5 with SdkHttpClient

use of software.amazon.awssdk.http.SdkHttpClient 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());
}
Also used : LambdaClient(software.amazon.awssdk.services.lambda.LambdaClient) SdkHttpClient(software.amazon.awssdk.http.SdkHttpClient) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Subsegment(com.amazonaws.xray.entities.Subsegment) Map(java.util.Map) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Aggregations

SdkHttpClient (software.amazon.awssdk.http.SdkHttpClient)6 Segment (com.amazonaws.xray.entities.Segment)5 Subsegment (com.amazonaws.xray.entities.Subsegment)5 Map (java.util.Map)5 Test (org.junit.Test)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 LambdaClient (software.amazon.awssdk.services.lambda.LambdaClient)4 Cause (com.amazonaws.xray.entities.Cause)3 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ExecutableHttpRequest (software.amazon.awssdk.http.ExecutableHttpRequest)1 SdkHttpResponse (software.amazon.awssdk.http.SdkHttpResponse)1 DynamoDbClient (software.amazon.awssdk.services.dynamodb.DynamoDbClient)1