Search in sources :

Example 1 with SamplingResponse

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();
}
Also used : SamplingRequest(com.amazonaws.xray.strategy.sampling.SamplingRequest) Segment(com.amazonaws.xray.entities.Segment) FacadeSegment(com.amazonaws.xray.entities.FacadeSegment) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse)

Example 2 with SamplingResponse

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());
}
Also used : SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Clock(java.time.Clock) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse) Test(org.junit.Test)

Example 3 with SamplingResponse

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());
}
Also used : SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Clock(java.time.Clock) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse) Test(org.junit.Test)

Example 4 with SamplingResponse

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;
}
Also used : SamplingRequest(com.amazonaws.xray.strategy.sampling.SamplingRequest) AWSXRayRecorder(com.amazonaws.xray.AWSXRayRecorder) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse)

Example 5 with SamplingResponse

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

Aggregations

SamplingResponse (com.amazonaws.xray.strategy.sampling.SamplingResponse)12 Test (org.junit.Test)8 SamplingRule (com.amazonaws.services.xray.model.SamplingRule)5 SamplingTargetDocument (com.amazonaws.services.xray.model.SamplingTargetDocument)5 Segment (com.amazonaws.xray.entities.Segment)5 Clock (java.time.Clock)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 AWSXRayRecorder (com.amazonaws.xray.AWSXRayRecorder)2 SamplingRequest (com.amazonaws.xray.strategy.sampling.SamplingRequest)2 FacadeSegment (com.amazonaws.xray.entities.FacadeSegment)1 TraceHeader (com.amazonaws.xray.entities.TraceHeader)1 SampleDecision (com.amazonaws.xray.entities.TraceHeader.SampleDecision)1 TraceID (com.amazonaws.xray.entities.TraceID)1 SamplingStrategy (com.amazonaws.xray.strategy.sampling.SamplingStrategy)1 RandImpl (com.amazonaws.xray.strategy.sampling.rand.RandImpl)1 HashMap (java.util.HashMap)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1