Search in sources :

Example 1 with SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument in project aws-xray-sdk-java by aws.

the class CentralizedRuleTest method testReservoirReset.

@Test
public void testReservoirReset() {
    Mockito.when(clock.instant()).thenReturn(Instant.ofEpochSecond(1500000000));
    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());
    rule.sample(clock.instant());
    CentralizedReservoir reservoir = Whitebox.getInternalState(rule, "centralizedReservoir", CentralizedRule.class);
    Assert.assertEquals(1, reservoir.getUsed());
    Mockito.when(clock.instant()).thenReturn(Instant.ofEpochSecond(1500000001));
    rule.sample(clock.instant());
    // Assert to ensure reservoir was reset before being updated
    Assert.assertEquals(1, reservoir.getUsed());
}
Also used : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) CentralizedReservoir(com.amazonaws.xray.strategy.sampling.reservoir.CentralizedReservoir) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument) Test(org.junit.Test)

Example 2 with SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument 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 SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument 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 SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument in project aws-xray-sdk-java by aws.

the class CentralizedManifest method putTargets.

public void putTargets(List<SamplingTargetDocument> targets, Instant now) {
    Map<String, CentralizedRule> rules = this.rules;
    for (SamplingTargetDocument t : targets) {
        CentralizedRule r = null;
        if (rules.containsKey(t.getRuleName())) {
            r = rules.get(t.getRuleName());
        } else if (t.getRuleName().equals(CentralizedRule.DEFAULT_RULE_NAME)) {
            r = defaultRule;
        }
        if (r == null) {
            continue;
        }
        r.update(t, now);
    }
}
Also used : CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument)

Example 5 with SamplingTargetDocument

use of com.amazonaws.services.xray.model.SamplingTargetDocument 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)

Aggregations

SamplingTargetDocument (com.amazonaws.services.xray.model.SamplingTargetDocument)9 SamplingRule (com.amazonaws.services.xray.model.SamplingRule)8 Test (org.junit.Test)8 Clock (java.time.Clock)6 SamplingResponse (com.amazonaws.xray.strategy.sampling.SamplingResponse)5 RandImpl (com.amazonaws.xray.strategy.sampling.rand.RandImpl)4 SamplingStatisticsDocument (com.amazonaws.services.xray.model.SamplingStatisticsDocument)1 CentralizedReservoir (com.amazonaws.xray.strategy.sampling.reservoir.CentralizedReservoir)1 CentralizedRule (com.amazonaws.xray.strategy.sampling.rule.CentralizedRule)1