Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear trace entities from asynchronous thread. #57

Merged
merged 3 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
request.setAttribute(AWSXRayServletAsyncListener.ENTITY_ATTRIBUTE_KEY, segment);
try {
request.getAsyncContext().addListener(listener);
recorder.clearTraceEntity();
} catch (IllegalStateException ise) { // race condition that occurs when async processing finishes before adding the listener
postFilter(request, response);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.amazonaws.xray.javax.servlet;

import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
Expand Down Expand Up @@ -53,6 +55,19 @@ private AWSXRayRecorder getMockRecorder() {
return AWSXRayRecorderBuilder.standard().withEmitter(blankEmitter).withSamplingStrategy(defaultSamplingStrategy).build();
}

private FilterChain mockChain(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
FilterChain chain = Mockito.mock(FilterChain.class);
AtomicReference<Entity> capturedEntity = new AtomicReference<>();
Mockito.doAnswer(a -> {
capturedEntity.set(AWSXRay.getTraceEntity());
return null;
}).when(chain).doFilter(request, response);

Mockito.when(request.getAttribute("com.amazonaws.xray.entities.Entity"))
.thenAnswer(a -> Objects.requireNonNull(capturedEntity.get()));
return chain;
}

steven-aerts marked this conversation as resolved.
Show resolved Hide resolved
@Test
public void testAsyncServletRequestHasListenerAdded() throws IOException, ServletException {
AWSXRayServletFilter servletFilter = new AWSXRayServletFilter("test");
Expand Down Expand Up @@ -91,16 +106,14 @@ public void testServletLazilyLoadsRecorder() throws IOException, ServletExceptio

HttpServletResponse response = Mockito.mock(HttpServletResponse.class);

FilterChain chain = Mockito.mock(FilterChain.class);
FilterChain chain = mockChain(request, response);

AsyncEvent event = Mockito.mock(AsyncEvent.class);
Mockito.when(event.getSuppliedRequest()).thenReturn(request);
Mockito.when(event.getSuppliedResponse()).thenReturn(response);

servletFilter.doFilter(request, response, chain);

Entity currentEntity = AWSXRay.getTraceEntity();
Mockito.when(request.getAttribute("com.amazonaws.xray.entities.Entity")).thenReturn(currentEntity);
Assert.assertNull(AWSXRay.getTraceEntity());

AWSXRayServletAsyncListener listener = (AWSXRayServletAsyncListener) Whitebox.getInternalState(servletFilter, "listener");
listener.onComplete(event);
Expand All @@ -124,16 +137,14 @@ public void testServletUsesPassedInRecorder() throws IOException, ServletExcepti

HttpServletResponse response = Mockito.mock(HttpServletResponse.class);

FilterChain chain = Mockito.mock(FilterChain.class);
FilterChain chain = mockChain(request, response);

AsyncEvent event = Mockito.mock(AsyncEvent.class);
Mockito.when(event.getSuppliedRequest()).thenReturn(request);
Mockito.when(event.getSuppliedResponse()).thenReturn(response);

servletFilter.doFilter(request, response, chain);

Entity currentEntity = AWSXRay.getTraceEntity();
Mockito.when(request.getAttribute("com.amazonaws.xray.entities.Entity")).thenReturn(currentEntity);
Assert.assertNull(AWSXRay.getTraceEntity());

AWSXRayServletAsyncListener listener = (AWSXRayServletAsyncListener) Whitebox.getInternalState(servletFilter, "listener");
listener.onComplete(event);
Expand All @@ -155,16 +166,14 @@ public void testAWSXRayServletAsyncListenerEmitsSegmentWhenProcessingEvent() thr

HttpServletResponse response = Mockito.mock(HttpServletResponse.class);

FilterChain chain = Mockito.mock(FilterChain.class);
FilterChain chain = mockChain(request, response);

AsyncEvent event = Mockito.mock(AsyncEvent.class);
Mockito.when(event.getSuppliedRequest()).thenReturn(request);
Mockito.when(event.getSuppliedResponse()).thenReturn(response);

servletFilter.doFilter(request, response, chain);

Entity currentEntity = AWSXRay.getTraceEntity();
Mockito.when(request.getAttribute("com.amazonaws.xray.entities.Entity")).thenReturn(currentEntity);
Assert.assertNull(AWSXRay.getTraceEntity());

AWSXRayServletAsyncListener listener = (AWSXRayServletAsyncListener) Whitebox.getInternalState(servletFilter, "listener");
listener.onComplete(event);
Expand Down