Search in sources :

Example 1 with RandImpl

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

the class CentralizedManifestTest method testRebuild.

@Test
void testRebuild() {
    Map<String, CentralizedRule> rules = new HashMap<>();
    rules.put("r1", new CentralizedRule(rule("r1").withPriority(11), new RandImpl()));
    rules.put("r2", new CentralizedRule(rule("r2"), new RandImpl()));
    List<SamplingRule> inputs = new ArrayList<>();
    inputs.add(rule("r2"));
    inputs.add(rule("r1"));
    inputs.add(rule("r3"));
    CentralizedManifest m = new CentralizedManifest();
    Map<String, CentralizedRule> rebuiltRules = m.rebuild(rules, inputs);
    Assertions.assertEquals(3, rebuiltRules.size());
    String[] orderedList = new String[3];
    rebuiltRules.keySet().toArray(orderedList);
    Assertions.assertEquals("r2", orderedList[0]);
    Assertions.assertEquals("r3", orderedList[1]);
    Assertions.assertEquals("r1", orderedList[2]);
}
Also used : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Test(org.junit.jupiter.api.Test)

Example 2 with RandImpl

use of com.amazonaws.xray.strategy.sampling.rand.RandImpl 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 3 with RandImpl

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

the class CentralizedManifest method rebuild.

LinkedHashMap<String, CentralizedRule> rebuild(Map<String, CentralizedRule> old, List<SamplingRule> inputs) {
    List<CentralizedRule> rules = new ArrayList<>(inputs.size() - 1);
    for (SamplingRule i : inputs) {
        if (i.getRuleName().equals(CentralizedRule.DEFAULT_RULE_NAME)) {
            continue;
        }
        CentralizedRule r = old.get(i.getRuleName());
        if (r == null) {
            r = new CentralizedRule(i, new RandImpl());
        }
        rules.add(r);
    }
    Collections.sort(rules);
    LinkedHashMap<String, CentralizedRule> current = new LinkedHashMap<>(rules.size());
    for (CentralizedRule r : rules) {
        current.put(r.getName(), r);
    }
    return current;
}
Also used : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) ArrayList(java.util.ArrayList) CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) LinkedHashMap(java.util.LinkedHashMap)

Example 4 with RandImpl

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

the class CentralizedRuleTest method testTargetUpdate.

@Test
public void testTargetUpdate() {
    SamplingRule input = createInput("r1", 300, 10, 0.0);
    CentralizedRule r = new CentralizedRule(input, new RandImpl());
    SamplingTargetDocument update = new SamplingTargetDocument().withRuleName("r1").withFixedRate(0.5).withInterval(20);
    r.update(update, Instant.now());
    double fixedRate = Whitebox.getInternalState(r, "fixedRate", CentralizedRule.class);
    Assert.assertEquals(0.5, fixedRate, 0);
}
Also used : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument) Test(org.junit.Test)

Example 5 with RandImpl

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

the class CentralizedRuleTest method testRuleUpdateWithInvalidation.

@Test
public void testRuleUpdateWithInvalidation() {
    SamplingRule input = createInput("r1", 300, 10, 0.0).withHTTPMethod("POST").withServiceName("s1").withURLPath("/foo/bar");
    CentralizedRule r = new CentralizedRule(input, new RandImpl());
    SamplingRule update = createInput("r1", 301, 5, 0.5).withHTTPMethod("GET").withServiceName("s2").withURLPath("/bar/foo");
    boolean invalidate = r.update(update);
    Matchers m = Whitebox.getInternalState(r, "matchers", CentralizedRule.class);
    Assert.assertEquals("GET", Whitebox.getInternalState(m, "method", Matchers.class));
    Assert.assertEquals("s2", Whitebox.getInternalState(m, "service", Matchers.class));
    Assert.assertEquals("/bar/foo", Whitebox.getInternalState(m, "url", Matchers.class));
    Assert.assertTrue(invalidate);
}
Also used : RandImpl(com.amazonaws.xray.strategy.sampling.rand.RandImpl) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Test(org.junit.Test)

Aggregations

SamplingRule (com.amazonaws.services.xray.model.SamplingRule)8 RandImpl (com.amazonaws.xray.strategy.sampling.rand.RandImpl)8 Test (org.junit.Test)6 SamplingTargetDocument (com.amazonaws.services.xray.model.SamplingTargetDocument)4 CentralizedRule (com.amazonaws.xray.strategy.sampling.rule.CentralizedRule)2 Clock (java.time.Clock)2 ArrayList (java.util.ArrayList)2 SamplingStatisticsDocument (com.amazonaws.services.xray.model.SamplingStatisticsDocument)1 SamplingResponse (com.amazonaws.xray.strategy.sampling.SamplingResponse)1 CentralizedReservoir (com.amazonaws.xray.strategy.sampling.reservoir.CentralizedReservoir)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Test (org.junit.jupiter.api.Test)1