Skip to content

Commit

Permalink
Ensure all XRay Sampler functionality is under ParentBased logic (#1488)
Browse files Browse the repository at this point in the history
  • Loading branch information
jj22ee authored Oct 7, 2024
1 parent a0d3e68 commit 7bb0a73
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public final class AwsXrayRemoteSampler implements Sampler, Closeable {
@Nullable private volatile ScheduledFuture<?> pollFuture;
@Nullable private volatile ScheduledFuture<?> fetchTargetsFuture;
@Nullable private volatile GetSamplingRulesResponse previousRulesResponse;
@Nullable private volatile XrayRulesSampler internalXrayRulesSampler;
private volatile Sampler sampler;

/**
Expand Down Expand Up @@ -125,15 +126,16 @@ private void getAndUpdateSampler() {
GetSamplingRulesResponse response =
client.getSamplingRules(GetSamplingRulesRequest.create(null));
if (!response.equals(previousRulesResponse)) {
sampler =
updateInternalSamplers(
new XrayRulesSampler(
clientId,
resource,
clock,
initialSampler,
response.getSamplingRules().stream()
.map(SamplingRuleRecord::getRule)
.collect(Collectors.toList()));
.collect(Collectors.toList())));

previousRulesResponse = response;
ScheduledFuture<?> existingFetchTargetsFuture = fetchTargetsFuture;
if (existingFetchTargetsFuture != null) {
Expand Down Expand Up @@ -170,11 +172,11 @@ Duration getNextSamplerUpdateScheduledDuration() {
}

private void fetchTargets() {
if (!(sampler instanceof XrayRulesSampler)) {
if (this.internalXrayRulesSampler == null) {
throw new IllegalStateException("Programming bug.");
}

XrayRulesSampler xrayRulesSampler = (XrayRulesSampler) sampler;
XrayRulesSampler xrayRulesSampler = this.internalXrayRulesSampler;
try {
Date now = Date.from(Instant.ofEpochSecond(0, clock.now()));
List<SamplingStatisticsDocument> statistics = xrayRulesSampler.snapshot(now);
Expand All @@ -188,8 +190,7 @@ private void fetchTargets() {
Map<String, SamplingTargetDocument> targets =
response.getDocuments().stream()
.collect(Collectors.toMap(SamplingTargetDocument::getRuleName, Function.identity()));
sampler =
xrayRulesSampler = xrayRulesSampler.withTargets(targets, requestedTargetRuleNames, now);
updateInternalSamplers(xrayRulesSampler.withTargets(targets, requestedTargetRuleNames, now));
} catch (Throwable t) {
// Might be a transient API failure, try again after a default interval.
fetchTargetsFuture =
Expand Down Expand Up @@ -225,6 +226,11 @@ private static String generateClientId() {
return new String(clientIdChars);
}

private void updateInternalSamplers(XrayRulesSampler xrayRulesSampler) {
this.internalXrayRulesSampler = xrayRulesSampler;
this.sampler = Sampler.parentBased(internalXrayRulesSampler);
}

// Visible for testing
XraySamplerClient getClient() {
return client;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,11 +464,11 @@ public String toString() {
}

private Sampler createRateLimited(int numPerSecond) {
return Sampler.parentBased(new RateLimitingSampler(numPerSecond, clock));
return new RateLimitingSampler(numPerSecond, clock);
}

private static Sampler createFixedRate(double rate) {
return Sampler.parentBased(Sampler.traceIdRatioBased(rate));
return Sampler.traceIdRatioBased(rate);
}

// We keep track of sampling requests and decisions to report to X-Ray to allow it to allocate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ void defaultInitialSampler() {
}
}

@Test
void parentBasedXraySamplerAfterDefaultSampler() {
rulesResponse.set(RULE_RESPONSE_1);
try (AwsXrayRemoteSampler samplerWithLongerPollingInterval =
AwsXrayRemoteSampler.newBuilder(Resource.empty())
.setInitialSampler(Sampler.alwaysOn())
.setEndpoint(server.httpUri().toString())
.setPollingInterval(Duration.ofMillis(5))
.build()) {
await()
.pollDelay(Duration.ofMillis(10))
.untilAsserted(
() -> {
assertThat(sampler.getDescription())
.startsWith("AwsXrayRemoteSampler{ParentBased{root:XrayRulesSampler{[");
});
}
}

// https://github.com/open-telemetry/opentelemetry-java-contrib/issues/376
@Test
void testJitterTruncation() {
Expand Down

0 comments on commit 7bb0a73

Please sign in to comment.