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;
}
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;
}
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();
}
}
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());
}
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());
}
Aggregations