Search in sources :

Example 6 with CentralizedRule

use of com.amazonaws.xray.strategy.sampling.rule.CentralizedRule 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 7 with CentralizedRule

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

the class CentralizedManifestTest method testSnapshotsWithDefaultRule.

@Test
void testSnapshotsWithDefaultRule() {
    Instant now = Instant.ofEpochSecond(1500000000);
    CentralizedManifest m = new CentralizedManifest();
    m.putRules(Arrays.asList(rule("r1"), rule("r2"), rule(CentralizedRule.DEFAULT_RULE_NAME)), now);
    Map<String, CentralizedRule> rules = Whitebox.getInternalState(m, "rules", CentralizedManifest.class);
    CentralizedRule defaultRule = Whitebox.getInternalState(m, "defaultRule", CentralizedManifest.class);
    rules.forEach((key, r) -> r.sample(now));
    defaultRule.sample(now);
    List<SamplingStatisticsDocument> snapshots = m.snapshots(now);
    Assertions.assertEquals(3, snapshots.size());
}
Also used : SamplingStatisticsDocument(com.amazonaws.services.xray.model.SamplingStatisticsDocument) Instant(java.time.Instant) CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) Test(org.junit.jupiter.api.Test)

Example 8 with CentralizedRule

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

the class CentralizedManifestTest method testRebuildOnPriorityChange.

@Test
void testRebuildOnPriorityChange() {
    CentralizedManifest manifest = new CentralizedManifest();
    manifest.putRules(Arrays.asList(rule("r1")), Instant.now());
    Map<String, CentralizedRule> rules1 = Whitebox.getInternalState(manifest, "rules", CentralizedManifest.class);
    SamplingRule r = rule("r1").withPriority(200);
    manifest.putRules(Arrays.asList(r), Instant.now());
    Map<String, CentralizedRule> rules2 = Whitebox.getInternalState(manifest, "rules", CentralizedManifest.class);
    // The map of rules should be rebuilt, resulting in a new object
    Assertions.assertFalse(rules1 == rules2);
    Assertions.assertEquals(1, rules1.size());
    Assertions.assertEquals(1, rules2.size());
}
Also used : CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Test(org.junit.jupiter.api.Test)

Example 9 with CentralizedRule

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

the class CentralizedManifestTest method testPutRulesWithoutRebuild.

@Test
void testPutRulesWithoutRebuild() {
    CentralizedManifest manifest = new CentralizedManifest();
    manifest.putRules(Arrays.asList(rule("r1")), Instant.now());
    Map<String, CentralizedRule> rules1 = Whitebox.getInternalState(manifest, "rules", CentralizedManifest.class);
    SamplingRule r = rule("r1").withResourceARN("arn3");
    manifest.putRules(Arrays.asList(r), Instant.now());
    Map<String, CentralizedRule> rules2 = Whitebox.getInternalState(manifest, "rules", CentralizedManifest.class);
    // The map of rules should not have been rebuilt
    Assertions.assertTrue(rules1 == rules2);
}
Also used : CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) Test(org.junit.jupiter.api.Test)

Example 10 with CentralizedRule

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

the class CentralizedManifest method snapshots.

public List<SamplingStatisticsDocument> snapshots(Instant now) {
    List<SamplingStatisticsDocument> snapshots = new ArrayList<>(rules.size() + 1);
    Date date = Date.from(now);
    for (CentralizedRule rule : rules.values()) {
        if (!rule.isStale(now)) {
            continue;
        }
        SamplingStatisticsDocument snapshot = rule.snapshot(date);
        snapshot.withClientID(CentralizedSamplingStrategy.getClientID());
        snapshots.add(snapshot);
    }
    if (defaultRule != null && defaultRule.isStale(now)) {
        SamplingStatisticsDocument snapshot = defaultRule.snapshot(date);
        snapshot.withClientID(CentralizedSamplingStrategy.getClientID());
        snapshots.add(snapshot);
    }
    return snapshots;
}
Also used : SamplingStatisticsDocument(com.amazonaws.services.xray.model.SamplingStatisticsDocument) ArrayList(java.util.ArrayList) CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) Date(java.util.Date)

Aggregations

CentralizedRule (com.amazonaws.xray.strategy.sampling.rule.CentralizedRule)10 SamplingRule (com.amazonaws.services.xray.model.SamplingRule)5 Test (org.junit.jupiter.api.Test)5 ArrayList (java.util.ArrayList)4 SamplingStatisticsDocument (com.amazonaws.services.xray.model.SamplingStatisticsDocument)3 RandImpl (com.amazonaws.xray.strategy.sampling.rand.RandImpl)2 Instant (java.time.Instant)2 SamplingTargetDocument (com.amazonaws.services.xray.model.SamplingTargetDocument)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1