use of com.amazonaws.xray.strategy.sampling.rule.CentralizedRule in project aws-xray-sdk-java by aws.
the class CentralizedManifestTest method testSnapshotsWithoutDefaultRule.
@Test
void testSnapshotsWithoutDefaultRule() {
Instant now = Instant.ofEpochSecond(1500000000);
CentralizedManifest m = new CentralizedManifest();
m.putRules(Arrays.asList(rule("r1"), rule("r2")), now);
Map<String, CentralizedRule> rules = Whitebox.getInternalState(m, "rules", CentralizedManifest.class);
rules.forEach((key, r) -> r.sample(now));
List<SamplingStatisticsDocument> snapshots = m.snapshots(now);
Assertions.assertEquals(2, snapshots.size());
}
use of com.amazonaws.xray.strategy.sampling.rule.CentralizedRule 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]);
}
use of com.amazonaws.xray.strategy.sampling.rule.CentralizedRule 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);
}
}
use of com.amazonaws.xray.strategy.sampling.rule.CentralizedRule in project aws-xray-sdk-java by aws.
the class CentralizedSamplingStrategy method shouldTrace.
@Override
public SamplingResponse shouldTrace(SamplingRequest samplingRequest) {
if (!isStarted) {
startPoller();
}
SamplingResponse sampleResponse;
if (logger.isDebugEnabled()) {
logger.debug("Determining shouldTrace decision for:\n\tserviceName: " + samplingRequest.getService().orElse("") + "\n\thost: " + samplingRequest.getHost().orElse("") + "\n\tpath: " + samplingRequest.getUrl().orElse("") + "\n\tmethod: " + samplingRequest.getMethod().orElse("") + "\n\tserviceType: " + samplingRequest.getServiceType().orElse(""));
}
if (manifest.isExpired(Instant.now())) {
logger.debug("Centralized sampling data expired. Using fallback sampling strategy.");
return fallback.shouldTrace(samplingRequest);
}
for (CentralizedRule rule : manifest.getRules().values()) {
boolean applicable = rule.match(samplingRequest);
if (!applicable) {
continue;
}
if (logger.isDebugEnabled()) {
logger.debug("Applicable rule:" + rule.getName());
}
SamplingResponse response = rule.sample(Instant.now());
if (logger.isDebugEnabled()) {
logger.debug("Segment " + samplingRequest.getService().orElse("") + " has" + (response.isSampled() ? " " : " NOT ") + "been sampled.");
}
return response;
}
// Match against default rule
CentralizedRule dRule = manifest.getDefaultRule();
if (dRule != null) {
logger.debug("Applicable default rule: " + dRule.getName());
return dRule.sample(Instant.now());
}
logger.debug("Centralized default sampling rule unavailable. Using fallback sampling strategy.");
sampleResponse = fallback.shouldTrace(samplingRequest);
return sampleResponse;
}
use of com.amazonaws.xray.strategy.sampling.rule.CentralizedRule 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;
}
Aggregations