use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.
the class AWSXRayRecorder method beginSegmentWithSampling.
/**
* Begins a new segment after applying the configured sampling strategy. This method only uses the segment name and origin
* (if defined) to compute a sampling decision.
*
* @param name the segment name, to be used for the sampling decision
* @return Returns a proper segment if a sampled decision is made, and a no-op segment otherwise.
*/
public Segment beginSegmentWithSampling(String name) {
final SamplingRequest samplingRequest = new SamplingRequest(name, null, null, null, this.origin);
final SamplingResponse samplingResponse = this.getSamplingStrategy().shouldTrace(samplingRequest);
if (samplingResponse.isSampled()) {
Segment segment = beginSegment(name);
if (samplingResponse.getRuleName().isPresent()) {
segment.setRuleName(samplingResponse.getRuleName().get());
}
return segment;
} else if (this.getSamplingStrategy().isForcedSamplingSupported()) {
Segment segment = beginSegment(name);
segment.setSampled(false);
if (samplingResponse.getRuleName().isPresent()) {
segment.setRuleName(samplingResponse.getRuleName().get());
}
return segment;
}
return beginNoOpSegment();
}
use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.
the class CentralizedRuleTest method testExpiredReservoirPositiveBernoulliSample.
@Test
public void testExpiredReservoirPositiveBernoulliSample() {
Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
SamplingRule input = createInput("r1", 300, 0, 0.5);
CentralizedRule rule = new CentralizedRule(input, rand);
SamplingTargetDocument target = createTarget(0, 0.5, 1499999999);
rule.update(target, clock.instant());
Mockito.when(rand.next()).thenReturn(0.2);
// BernoulliSample() from expired reservoir
SamplingResponse response = rule.sample(clock.instant());
Mockito.verify(rand).next();
Assert.assertTrue(response.isSampled());
Assert.assertEquals("r1", response.getRuleName().get());
Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class);
Assert.assertEquals(1, s.getSampled());
Assert.assertEquals(1, s.getRequests());
Assert.assertEquals(0, s.getBorrowed());
}
use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.
the class CentralizedRuleTest method testNegativeSample.
@Test
public void testNegativeSample() {
Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
SamplingRule input = createInput("r1", 300, 10, 0.0);
CentralizedRule rule = new CentralizedRule(input, rand);
SamplingTargetDocument target = createTarget(0, 0.0, 1500000010);
rule.update(target, clock.instant());
SamplingResponse response = rule.sample(clock.instant());
Assert.assertFalse(response.isSampled());
Assert.assertEquals("r1", response.getRuleName().get());
Statistics s = Whitebox.getInternalState(rule, "statistics", CentralizedRule.class);
Assert.assertEquals(0, s.getSampled());
Assert.assertEquals(1, s.getRequests());
Assert.assertEquals(0, s.getBorrowed());
}
use of com.amazonaws.xray.strategy.sampling.SamplingResponse 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.strategy.sampling.SamplingResponse 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;
}
Aggregations