use of com.amazonaws.xray.entities.TraceHeader.SampleDecision 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.entities.TraceHeader.SampleDecision 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;
}
Aggregations