Search in sources :

Example 11 with Segment

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

the class TracedResponseHandlerTest method testHandleResponse429SetsErrorAndThrottleFlag.

@Test
public void testHandleResponse429SetsErrorAndThrottleFlag() {
    Segment segment = segmentInResponseToCode(429);
    Subsegment subsegment = segment.getSubsegments().get(0);
    Assert.assertFalse(subsegment.isFault());
    Assert.assertTrue(subsegment.isError());
    Assert.assertTrue(subsegment.isThrottle());
}
Also used : Subsegment(com.amazonaws.xray.entities.Subsegment) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 12 with Segment

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

the class TracedResponseHandlerTest method segmentInResponseToCode.

private Segment segmentInResponseToCode(int code) {
    NoOpResponseHandler responseHandler = new NoOpResponseHandler();
    TracedResponseHandler<String> tracedResponseHandler = new TracedResponseHandler<>(responseHandler);
    HttpResponse httpResponse = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, code, ""));
    Segment segment = AWSXRay.beginSegment("test");
    AWSXRay.beginSubsegment("someHttpCall");
    try {
        tracedResponseHandler.handleResponse(httpResponse);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    AWSXRay.endSubsegment();
    AWSXRay.endSegment();
    return segment;
}
Also used : BasicHttpResponse(org.apache.http.message.BasicHttpResponse) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) Segment(com.amazonaws.xray.entities.Segment) BasicStatusLine(org.apache.http.message.BasicStatusLine)

Example 13 with Segment

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

the class TracingHandlerTest method testS3CopyObjectSubsegmentContainsBucketName.

@Test
public void testS3CopyObjectSubsegmentContainsBucketName() {
    // Setup test
    final String copyResponse = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<CopyObjectResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">" + "<LastModified>2018-01-21T10:09:54.000Z</LastModified><ETag>&quot;31748afd7b576119d3c2471f39fc7a55&quot;</ETag>" + "</CopyObjectResult>";
    AmazonS3 s3 = AmazonS3ClientBuilder.standard().withRequestHandlers(new TracingHandler()).withRegion(Regions.US_EAST_1).withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials("fake", "fake"))).build();
    mockHttpClient(s3, copyResponse);
    final String BUCKET = "source-bucket", KEY = "source-key", DST_BUCKET = "dest-bucket", DST_KEY = "dest-key";
    // Test logic
    Segment segment = AWSXRay.beginSegment("test");
    s3.copyObject(BUCKET, KEY, DST_BUCKET, DST_KEY);
    Assert.assertEquals(1, segment.getSubsegments().size());
    Assert.assertEquals("CopyObject", segment.getSubsegments().get(0).getAws().get("operation"));
    System.out.println(segment.getSubsegments().get(0).getAws());
    Assert.assertEquals(BUCKET, segment.getSubsegments().get(0).getAws().get("source_bucket_name"));
    Assert.assertEquals(KEY, segment.getSubsegments().get(0).getAws().get("source_key"));
    Assert.assertEquals(DST_BUCKET, segment.getSubsegments().get(0).getAws().get("destination_bucket_name"));
    Assert.assertEquals(DST_KEY, segment.getSubsegments().get(0).getAws().get("destination_key"));
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) Segment(com.amazonaws.xray.entities.Segment) Test(org.junit.Test)

Example 14 with Segment

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

the class AWSXRayServletAsyncListener method preFilter.

public Segment preFilter(ServletRequest request, ServletResponse response) {
    Segment created = null;
    HttpServletRequest httpServletRequest = castServletRequest(request);
    if (null == httpServletRequest) {
        logger.warn("Null value for incoming HttpServletRequest. Beginning DummySegment.");
        return recorder.beginDummySegment(new TraceID());
    }
    Optional<TraceHeader> incomingHeader = getTraceHeader(httpServletRequest);
    SamplingStrategy samplingStrategy = recorder.getSamplingStrategy();
    if (logger.isDebugEnabled() && incomingHeader.isPresent()) {
        logger.debug("Incoming trace header received: " + incomingHeader.get().toString());
    }
    SampleDecision sampleDecision = incomingHeader.isPresent() ? incomingHeader.get().getSampled() : fromSamplingStrategy(httpServletRequest);
    if (SampleDecision.REQUESTED.equals(sampleDecision) || SampleDecision.UNKNOWN.equals(sampleDecision)) {
        sampleDecision = fromSamplingStrategy(httpServletRequest);
    }
    TraceID traceId = incomingHeader.isPresent() ? incomingHeader.get().getRootTraceId() : null;
    if (null == traceId) {
        traceId = new TraceID();
    }
    String parentId = incomingHeader.isPresent() ? incomingHeader.get().getParentId() : null;
    if (SampleDecision.SAMPLED.equals(sampleDecision)) {
        created = recorder.beginSegment(getSegmentName(httpServletRequest), traceId, parentId);
    } else {
        // NOT_SAMPLED
        if (samplingStrategy.isForcedSamplingSupported()) {
            created = recorder.beginSegment(getSegmentName(httpServletRequest), traceId, parentId);
            created.setSampled(false);
        } else {
            created = recorder.beginDummySegment(traceId);
        }
    }
    Map<String, Object> requestAttributes = new HashMap<String, Object>();
    requestAttributes.put("url", httpServletRequest.getRequestURL().toString());
    requestAttributes.put("method", httpServletRequest.getMethod());
    Optional<String> userAgent = getUserAgent(httpServletRequest);
    if (userAgent.isPresent()) {
        requestAttributes.put("user_agent", userAgent.get());
    }
    Optional<String> xForwardedFor = getXForwardedFor(httpServletRequest);
    if (xForwardedFor.isPresent()) {
        requestAttributes.put("client_ip", xForwardedFor.get());
        requestAttributes.put("x_forwarded_for", true);
    } else {
        Optional<String> clientIp = getClientIp(httpServletRequest);
        if (clientIp.isPresent()) {
            requestAttributes.put("client_ip", clientIp.get());
        }
    }
    created.putHttp("request", requestAttributes);
    HttpServletResponse httpServletResponse = castServletResponse(response);
    if (null == response) {
        return created;
    }
    TraceHeader responseHeader = null;
    if (incomingHeader.isPresent()) {
        // create a new header, and use the incoming header so we know what to do in regards to sending back the sampling decision.
        responseHeader = new TraceHeader(created.getTraceId());
        if (SampleDecision.REQUESTED == incomingHeader.get().getSampled()) {
            responseHeader.setSampled(created.isSampled() ? SampleDecision.SAMPLED : SampleDecision.NOT_SAMPLED);
        }
    } else {
        // Create a new header, we're the tracing root. We wont return the sampling decision.
        responseHeader = new TraceHeader(created.getTraceId());
    }
    httpServletResponse.addHeader(TraceHeader.HEADER_KEY, responseHeader.toString());
    return created;
}
Also used : TraceHeader(com.amazonaws.xray.entities.TraceHeader) HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) SamplingStrategy(com.amazonaws.xray.strategy.sampling.SamplingStrategy) SampleDecision(com.amazonaws.xray.entities.TraceHeader.SampleDecision) Segment(com.amazonaws.xray.entities.Segment) HttpServletRequest(javax.servlet.http.HttpServletRequest) TraceID(com.amazonaws.xray.entities.TraceID)

Example 15 with Segment

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

the class AWSXRayServletAsyncListener method postFilter.

public void postFilter(ServletRequest request, ServletResponse response) {
    Segment segment = recorder.getCurrentSegment();
    if (null != segment) {
        HttpServletResponse httpServletResponse = castServletResponse(response);
        if (null != httpServletResponse) {
            Map<String, Object> responseAttributes = new HashMap<String, Object>();
            int responseCode = httpServletResponse.getStatus();
            switch(responseCode / 100) {
                case 4:
                    segment.setError(true);
                    if (responseCode == 429) {
                        segment.setThrottle(true);
                    }
                    break;
                case 5:
                    segment.setFault(true);
                    break;
                default:
                    break;
            }
            responseAttributes.put("status", responseCode);
            Optional<Integer> contentLength = getContentLength(httpServletResponse);
            if (contentLength.isPresent()) {
                responseAttributes.put("content_length", contentLength.get());
            }
            segment.putHttp("response", responseAttributes);
        }
        recorder.endSegment();
    }
}
Also used : HashMap(java.util.HashMap) HttpServletResponse(javax.servlet.http.HttpServletResponse) Segment(com.amazonaws.xray.entities.Segment)

Aggregations

Segment (com.amazonaws.xray.entities.Segment)29 Test (org.junit.Test)16 Subsegment (com.amazonaws.xray.entities.Subsegment)8 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)6 TraceID (com.amazonaws.xray.entities.TraceID)5 HttpServletResponse (javax.servlet.http.HttpServletResponse)5 SegmentImpl (com.amazonaws.xray.entities.SegmentImpl)4 SubsegmentImpl (com.amazonaws.xray.entities.SubsegmentImpl)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)3 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)3 HashMap (java.util.HashMap)3 FilterChain (javax.servlet.FilterChain)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 TraceHeader (com.amazonaws.xray.entities.TraceHeader)2 AWSLambda (com.amazonaws.services.lambda.AWSLambda)1 InvokeRequest (com.amazonaws.services.lambda.model.InvokeRequest)1 InvokeResult (com.amazonaws.services.lambda.model.InvokeResult)1 Emitter (com.amazonaws.xray.emitters.Emitter)1 UDPEmitter (com.amazonaws.xray.emitters.UDPEmitter)1