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));
}
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));
}
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;
}
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;
}
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());
}
Aggregations