Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure all XRay Sampler functionality is under ParentBased logic #1488

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading