Search in sources :

Example 1 with AWSXRayRecorder

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

the class AWSXRayServletFilter method fromSamplingStrategy.

private SamplingResponse fromSamplingStrategy(HttpServletRequest httpServletRequest) {
    AWSXRayRecorder recorder = getRecorder();
    SamplingRequest samplingRequest = new SamplingRequest(getSegmentName(httpServletRequest), getHost(httpServletRequest).orElse(null), httpServletRequest.getRequestURI(), httpServletRequest.getMethod(), recorder.getOrigin());
    SamplingResponse sample = recorder.getSamplingStrategy().shouldTrace(samplingRequest);
    return sample;
}
Also used : SamplingRequest(com.amazonaws.xray.strategy.sampling.SamplingRequest) AWSXRayRecorder(com.amazonaws.xray.AWSXRayRecorder) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse)

Example 2 with AWSXRayRecorder

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

the class AWSXRayServletFilter method preFilter.

public Segment preFilter(ServletRequest request, ServletResponse response) {
    AWSXRayRecorder recorder = getRecorder();
    HttpServletRequest httpServletRequest = castServletRequest(request);
    if (httpServletRequest == null) {
        logger.warn("Null value for incoming HttpServletRequest. Beginning NoOpSegment.");
        return recorder.beginNoOpSegment();
    }
    Optional<TraceHeader> incomingHeader = getTraceHeader(httpServletRequest);
    SamplingStrategy samplingStrategy = recorder.getSamplingStrategy();
    if (logger.isDebugEnabled() && incomingHeader.isPresent()) {
        logger.debug("Incoming trace header received: " + incomingHeader.get().toString());
    }
    SamplingResponse samplingResponse = fromSamplingStrategy(httpServletRequest);
    SampleDecision sampleDecision = incomingHeader.isPresent() ? incomingHeader.get().getSampled() : getSampleDecision(samplingResponse);
    if (SampleDecision.REQUESTED.equals(sampleDecision) || SampleDecision.UNKNOWN.equals(sampleDecision)) {
        sampleDecision = getSampleDecision(samplingResponse);
    }
    TraceID traceId = null;
    String parentId = null;
    if (incomingHeader.isPresent()) {
        TraceHeader header = incomingHeader.get();
        traceId = header.getRootTraceId();
        parentId = header.getParentId();
    }
    final Segment created;
    if (SampleDecision.SAMPLED.equals(sampleDecision)) {
        String segmentName = getSegmentName(httpServletRequest);
        created = traceId != null ? recorder.beginSegment(segmentName, traceId, parentId) : recorder.beginSegment(segmentName);
        if (samplingResponse.getRuleName().isPresent()) {
            logger.debug("Sampling strategy decided to use rule named: " + samplingResponse.getRuleName().get() + ".");
            created.setRuleName(samplingResponse.getRuleName().get());
        }
    } else {
        // NOT_SAMPLED
        String segmentName = getSegmentName(httpServletRequest);
        if (samplingStrategy.isForcedSamplingSupported()) {
            created = traceId != null ? recorder.beginSegment(segmentName, traceId, parentId) : recorder.beginSegment(segmentName);
            created.setSampled(false);
        } else {
            logger.debug("Creating Dummy Segment");
            created = traceId != null ? recorder.beginNoOpSegment(traceId) : recorder.beginNoOpSegment();
        }
    }
    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 (httpServletResponse == null) {
        return created;
    }
    final TraceHeader responseHeader;
    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) AWSXRayRecorder(com.amazonaws.xray.AWSXRayRecorder) SampleDecision(com.amazonaws.xray.entities.TraceHeader.SampleDecision) Segment(com.amazonaws.xray.entities.Segment) HttpServletRequest(javax.servlet.http.HttpServletRequest) TraceID(com.amazonaws.xray.entities.TraceID) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse)

Example 3 with AWSXRayRecorder

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

the class AWSXRayServletFilter method postFilter.

public void postFilter(ServletRequest request, ServletResponse response) {
    AWSXRayRecorder recorder = getRecorder();
    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) AWSXRayRecorder(com.amazonaws.xray.AWSXRayRecorder) Segment(com.amazonaws.xray.entities.Segment)

Example 4 with AWSXRayRecorder

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

the class AWSXRayServletFilterTest method testServletLazilyLoadsRecorder.

@Test
public void testServletLazilyLoadsRecorder() throws IOException, ServletException {
    AWSXRayServletFilter servletFilter = new AWSXRayServletFilter("test");
    AsyncContext asyncContext = mock(AsyncContext.class);
    AWSXRayRecorder customRecorder = Mockito.spy(getMockRecorder());
    AWSXRay.setGlobalRecorder(customRecorder);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURL()).thenReturn(new StringBuffer("test_url"));
    when(request.getMethod()).thenReturn("TEST_METHOD");
    when(request.isAsyncStarted()).thenReturn(true);
    when(request.getAsyncContext()).thenReturn(asyncContext);
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mockChain(request, response);
    AsyncEvent event = mock(AsyncEvent.class);
    when(event.getSuppliedRequest()).thenReturn(request);
    when(event.getSuppliedResponse()).thenReturn(response);
    servletFilter.doFilter(request, response, chain);
    Assert.assertNull(AWSXRay.getTraceEntity());
    AWSXRayServletAsyncListener listener = (AWSXRayServletAsyncListener) Whitebox.getInternalState(servletFilter, "listener");
    listener.onComplete(event);
    verify(customRecorder.getEmitter(), Mockito.times(1)).sendSegment(Mockito.any());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AWSXRayRecorder(com.amazonaws.xray.AWSXRayRecorder) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Example 5 with AWSXRayRecorder

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

the class AWSXRayServletFilterTest method testServletUsesPassedInRecorder.

@Test
public void testServletUsesPassedInRecorder() throws IOException, ServletException {
    AWSXRayRecorder customRecorder = Mockito.spy(getMockRecorder());
    AWSXRayServletFilter servletFilter = new AWSXRayServletFilter(new FixedSegmentNamingStrategy("test"), customRecorder);
    AsyncContext asyncContext = mock(AsyncContext.class);
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getRequestURL()).thenReturn(new StringBuffer("test_url"));
    when(request.getMethod()).thenReturn("TEST_METHOD");
    when(request.isAsyncStarted()).thenReturn(true);
    when(request.getAsyncContext()).thenReturn(asyncContext);
    HttpServletResponse response = mock(HttpServletResponse.class);
    FilterChain chain = mockChain(request, response);
    AsyncEvent event = mock(AsyncEvent.class);
    when(event.getSuppliedRequest()).thenReturn(request);
    when(event.getSuppliedResponse()).thenReturn(response);
    servletFilter.doFilter(request, response, chain);
    Assert.assertNull(AWSXRay.getTraceEntity());
    AWSXRayServletAsyncListener listener = (AWSXRayServletAsyncListener) Whitebox.getInternalState(servletFilter, "listener");
    listener.onComplete(event);
    verify(customRecorder.getEmitter(), Mockito.times(1)).sendSegment(Mockito.any());
}
Also used : FixedSegmentNamingStrategy(com.amazonaws.xray.strategy.FixedSegmentNamingStrategy) HttpServletRequest(javax.servlet.http.HttpServletRequest) FilterChain(javax.servlet.FilterChain) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) AWSXRayRecorder(com.amazonaws.xray.AWSXRayRecorder) AsyncEvent(javax.servlet.AsyncEvent) Test(org.junit.Test)

Aggregations

AWSXRayRecorder (com.amazonaws.xray.AWSXRayRecorder)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Segment (com.amazonaws.xray.entities.Segment)2 SamplingResponse (com.amazonaws.xray.strategy.sampling.SamplingResponse)2 HashMap (java.util.HashMap)2 AsyncContext (javax.servlet.AsyncContext)2 AsyncEvent (javax.servlet.AsyncEvent)2 FilterChain (javax.servlet.FilterChain)2 Test (org.junit.Test)2 Test (org.junit.jupiter.api.Test)2 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 AWSLambda (com.amazonaws.services.lambda.AWSLambda)1 InvokeRequest (com.amazonaws.services.lambda.model.InvokeRequest)1 Entity (com.amazonaws.xray.entities.Entity)1 Subsegment (com.amazonaws.xray.entities.Subsegment)1 TraceHeader (com.amazonaws.xray.entities.TraceHeader)1 SampleDecision (com.amazonaws.xray.entities.TraceHeader.SampleDecision)1 TraceID (com.amazonaws.xray.entities.TraceID)1