Search in sources :

Example 6 with SamplingResponse

use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.

the class CentralizedRule method sample.

@Override
public SamplingResponse sample(Instant now) {
    SamplingResponse res = new SamplingResponse(name);
    double rn = rand.next();
    lock.writeLock().lock();
    try {
        return doSample(now, res, rn);
    } finally {
        lock.writeLock().unlock();
    }
}
Also used : SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse)

Example 7 with SamplingResponse

use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.

the class CentralizedRuleTest method testPositiveBernoulliSample.

@Test
public void testPositiveBernoulliSample() {
    Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
    SamplingRule input = createInput("r1", 300, 10, 0.0);
    CentralizedRule rule = new CentralizedRule(input, rand);
    Mockito.when(rand.next()).thenReturn(0.01);
    SamplingTargetDocument target = createTarget(0, 0.05, 1500000010);
    rule.update(target, clock.instant());
    // Sample using bernoulli sampling
    SamplingResponse response = rule.sample(clock.instant());
    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 8 with SamplingResponse

use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.

the class CentralizedRuleTest method testExpiredReservoirNegativeBernoulliSample.

@Test
public void testExpiredReservoirNegativeBernoulliSample() {
    Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
    SamplingRule input = createInput("r1", 300, 0, 0.2);
    CentralizedRule rule = new CentralizedRule(input, rand);
    SamplingTargetDocument target = createTarget(0, 0.2, 1499999999);
    rule.update(target, clock.instant());
    Mockito.when(rand.next()).thenReturn(0.4);
    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 9 with SamplingResponse

use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.

the class CentralizedRuleTest method testPositiveSampleTake.

@Test
public void testPositiveSampleTake() {
    Clock clock = Clock.fixed(Instant.ofEpochSecond(1500000000), ZoneId.systemDefault());
    SamplingRule input = createInput("r1", 300, 10, 0.0);
    CentralizedRule rule = new CentralizedRule(input, new RandImpl());
    SamplingTargetDocument target = createTarget(2, 0.0, 1500000010);
    rule.update(target, clock.instant());
    SamplingResponse response = rule.sample(clock.instant());
    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 : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) 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 10 with SamplingResponse

use of com.amazonaws.xray.strategy.sampling.SamplingResponse in project aws-xray-sdk-java by aws.

the class AWSXRayRecorderTest method testBeginSegmentWithSamplingDoesSample.

@Test
public void testBeginSegmentWithSamplingDoesSample() {
    SamplingResponse response = new SamplingResponse(true, "rule");
    when(mockSamplingStrategy.shouldTrace(any())).thenReturn(response);
    AWSXRay.getGlobalRecorder().setSamplingStrategy(mockSamplingStrategy);
    Segment segment = AWSXRay.beginSegmentWithSampling("test");
    assertThat(segment.isSampled()).isTrue();
    assertThat(segment.getAws().get("xray")).isInstanceOfSatisfying(Map.class, xray -> assertThat(xray.get("rule_name")).isEqualTo("rule"));
}
Also used : Segment(com.amazonaws.xray.entities.Segment) SamplingResponse(com.amazonaws.xray.strategy.sampling.SamplingResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

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