Search in sources :

Example 11 with SamplingRule

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

the class MatchersTest method testPartialGlobMatch.

@Test
void testPartialGlobMatch() {
    Map<String, String> ruleAttributes = new HashMap<>();
    ruleAttributes.put("ip", "127.*.1");
    ruleAttributes.put("compression", "*");
    Map<String, String> reqAttributes = new HashMap<>();
    reqAttributes.put("ip", "127.0.0.1");
    reqAttributes.put("compression", "gzip");
    reqAttributes.put("encoding", "json");
    SamplingRule rule = new SamplingRule().withAttributes(ruleAttributes).withHost("*").withServiceName("*.foo.*").withHTTPMethod("*").withResourceARN("*").withURLPath("/bar/*").withServiceType("AWS::EC2::Instance");
    SamplingRequest req = new SamplingRequest("role-arn", "arn:aws:service:us-east-1:111111111111:resource", "www.foo.com", "192.168.1.1", "GET", "/bar/baz", "AWS::EC2::Instance", reqAttributes);
    Matchers m = new Matchers(rule);
    Assertions.assertTrue(m.match(req));
}
Also used : HashMap(java.util.HashMap) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) SamplingRequest(com.amazonaws.xray.strategy.sampling.SamplingRequest) Test(org.junit.jupiter.api.Test)

Example 12 with SamplingRule

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

the class MatchersTest method testFullGlobMatch.

@Test
void testFullGlobMatch() {
    Map<String, String> ruleAttributes = new HashMap<>();
    ruleAttributes.put("ip", "*");
    ruleAttributes.put("compression", "*");
    Map<String, String> reqAttributes = new HashMap<>();
    reqAttributes.put("ip", "127.0.0.1");
    reqAttributes.put("compression", "gzip");
    reqAttributes.put("encoding", "json");
    SamplingRule rule = new SamplingRule().withAttributes(ruleAttributes).withHost("*").withServiceName("*").withHTTPMethod("*").withResourceARN("*").withURLPath("*").withServiceType("*");
    SamplingRequest req = new SamplingRequest("role-arn", "arn:aws:service:us-east-1:111111111111:resource", "www.foo.com", "192.168.1.1", "GET", "/baz/bar", "AWS::EC2::Instance", reqAttributes);
    Matchers m = new Matchers(rule);
    Assertions.assertTrue(m.match(req));
}
Also used : HashMap(java.util.HashMap) SamplingRule(com.amazonaws.services.xray.model.SamplingRule) SamplingRequest(com.amazonaws.xray.strategy.sampling.SamplingRequest) Test(org.junit.jupiter.api.Test)

Example 13 with SamplingRule

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

the class CentralizedManifest method putRules.

public void putRules(List<SamplingRule> inputs, Instant now) {
    // Set to true if we see a new or deleted rule or a change in the priority of an existing rule.
    boolean invalidate = false;
    Map<String, CentralizedRule> rules = this.rules;
    List<String> inputNames = new ArrayList<>(inputs.size());
    for (SamplingRule i : inputs) {
        if (i.getRuleName().equals(CentralizedRule.DEFAULT_RULE_NAME)) {
            putDefaultRule(i);
        } else {
            inputNames.add(i.getRuleName());
            invalidate = putCustomRule(rules, i);
        }
    }
    // Check if any rule was removed
    if (!invalidate) {
        for (CentralizedRule rule : rules.values()) {
            if (!inputNames.contains(rule.getName())) {
                invalidate = true;
                break;
            }
        }
    }
    if (invalidate) {
        this.rules = rebuild(rules, inputs);
    }
    this.refreshedAt = now;
}
Also used : ArrayList(java.util.ArrayList) CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) SamplingRule(com.amazonaws.services.xray.model.SamplingRule)

Example 14 with SamplingRule

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

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

SamplingRule (com.amazonaws.services.xray.model.SamplingRule)30 Test (org.junit.jupiter.api.Test)17 SamplingRequest (com.amazonaws.xray.strategy.sampling.SamplingRequest)10 Test (org.junit.Test)10 SamplingTargetDocument (com.amazonaws.services.xray.model.SamplingTargetDocument)8 RandImpl (com.amazonaws.xray.strategy.sampling.rand.RandImpl)8 Clock (java.time.Clock)6 Instant (java.time.Instant)6 SamplingResponse (com.amazonaws.xray.strategy.sampling.SamplingResponse)5 CentralizedRule (com.amazonaws.xray.strategy.sampling.rule.CentralizedRule)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 GetSamplingRulesRequest (com.amazonaws.services.xray.model.GetSamplingRulesRequest)1 GetSamplingRulesResult (com.amazonaws.services.xray.model.GetSamplingRulesResult)1 SamplingRuleRecord (com.amazonaws.services.xray.model.SamplingRuleRecord)1 SamplingStatisticsDocument (com.amazonaws.services.xray.model.SamplingStatisticsDocument)1 CentralizedReservoir (com.amazonaws.xray.strategy.sampling.reservoir.CentralizedReservoir)1 LinkedHashMap (java.util.LinkedHashMap)1