Search in sources :

Example 1 with CentralizedRule

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());
}
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 2 with CentralizedRule

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]);
}
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 3 with CentralizedRule

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);
    }
}
Also used : CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) SamplingTargetDocument(com.amazonaws.services.xray.model.SamplingTargetDocument)

Example 4 with CentralizedRule

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;
}
Also used : CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule)

Example 5 with CentralizedRule

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;
}
Also used : ArrayList(java.util.ArrayList) CentralizedRule(com.amazonaws.xray.strategy.sampling.rule.CentralizedRule) SamplingRule(com.amazonaws.services.xray.model.SamplingRule)

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