Skip to content

Commit 7bb0a73

Browse files
authored
Ensure all XRay Sampler functionality is under ParentBased logic (open-telemetry#1488)
1 parent a0d3e68 commit 7bb0a73

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSampler.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public final class AwsXrayRemoteSampler implements Sampler, Closeable {
5656
@Nullable private volatile ScheduledFuture<?> pollFuture;
5757
@Nullable private volatile ScheduledFuture<?> fetchTargetsFuture;
5858
@Nullable private volatile GetSamplingRulesResponse previousRulesResponse;
59+
@Nullable private volatile XrayRulesSampler internalXrayRulesSampler;
5960
private volatile Sampler sampler;
6061

6162
/**
@@ -125,15 +126,16 @@ private void getAndUpdateSampler() {
125126
GetSamplingRulesResponse response =
126127
client.getSamplingRules(GetSamplingRulesRequest.create(null));
127128
if (!response.equals(previousRulesResponse)) {
128-
sampler =
129+
updateInternalSamplers(
129130
new XrayRulesSampler(
130131
clientId,
131132
resource,
132133
clock,
133134
initialSampler,
134135
response.getSamplingRules().stream()
135136
.map(SamplingRuleRecord::getRule)
136-
.collect(Collectors.toList()));
137+
.collect(Collectors.toList())));
138+
137139
previousRulesResponse = response;
138140
ScheduledFuture<?> existingFetchTargetsFuture = fetchTargetsFuture;
139141
if (existingFetchTargetsFuture != null) {
@@ -170,11 +172,11 @@ Duration getNextSamplerUpdateScheduledDuration() {
170172
}
171173

172174
private void fetchTargets() {
173-
if (!(sampler instanceof XrayRulesSampler)) {
175+
if (this.internalXrayRulesSampler == null) {
174176
throw new IllegalStateException("Programming bug.");
175177
}
176178

177-
XrayRulesSampler xrayRulesSampler = (XrayRulesSampler) sampler;
179+
XrayRulesSampler xrayRulesSampler = this.internalXrayRulesSampler;
178180
try {
179181
Date now = Date.from(Instant.ofEpochSecond(0, clock.now()));
180182
List<SamplingStatisticsDocument> statistics = xrayRulesSampler.snapshot(now);
@@ -188,8 +190,7 @@ private void fetchTargets() {
188190
Map<String, SamplingTargetDocument> targets =
189191
response.getDocuments().stream()
190192
.collect(Collectors.toMap(SamplingTargetDocument::getRuleName, Function.identity()));
191-
sampler =
192-
xrayRulesSampler = xrayRulesSampler.withTargets(targets, requestedTargetRuleNames, now);
193+
updateInternalSamplers(xrayRulesSampler.withTargets(targets, requestedTargetRuleNames, now));
193194
} catch (Throwable t) {
194195
// Might be a transient API failure, try again after a default interval.
195196
fetchTargetsFuture =
@@ -225,6 +226,11 @@ private static String generateClientId() {
225226
return new String(clientIdChars);
226227
}
227228

229+
private void updateInternalSamplers(XrayRulesSampler xrayRulesSampler) {
230+
this.internalXrayRulesSampler = xrayRulesSampler;
231+
this.sampler = Sampler.parentBased(internalXrayRulesSampler);
232+
}
233+
228234
// Visible for testing
229235
XraySamplerClient getClient() {
230236
return client;

aws-xray/src/main/java/io/opentelemetry/contrib/awsxray/SamplingRuleApplier.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,11 @@ public String toString() {
464464
}
465465

466466
private Sampler createRateLimited(int numPerSecond) {
467-
return Sampler.parentBased(new RateLimitingSampler(numPerSecond, clock));
467+
return new RateLimitingSampler(numPerSecond, clock);
468468
}
469469

470470
private static Sampler createFixedRate(double rate) {
471-
return Sampler.parentBased(Sampler.traceIdRatioBased(rate));
471+
return Sampler.traceIdRatioBased(rate);
472472
}
473473

474474
// We keep track of sampling requests and decisions to report to X-Ray to allow it to allocate

aws-xray/src/test/java/io/opentelemetry/contrib/awsxray/AwsXrayRemoteSamplerTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,25 @@ void defaultInitialSampler() {
168168
}
169169
}
170170

171+
@Test
172+
void parentBasedXraySamplerAfterDefaultSampler() {
173+
rulesResponse.set(RULE_RESPONSE_1);
174+
try (AwsXrayRemoteSampler samplerWithLongerPollingInterval =
175+
AwsXrayRemoteSampler.newBuilder(Resource.empty())
176+
.setInitialSampler(Sampler.alwaysOn())
177+
.setEndpoint(server.httpUri().toString())
178+
.setPollingInterval(Duration.ofMillis(5))
179+
.build()) {
180+
await()
181+
.pollDelay(Duration.ofMillis(10))
182+
.untilAsserted(
183+
() -> {
184+
assertThat(sampler.getDescription())
185+
.startsWith("AwsXrayRemoteSampler{ParentBased{root:XrayRulesSampler{[");
186+
});
187+
}
188+
}
189+
171190
// https://github.com/open-telemetry/opentelemetry-java-contrib/issues/376
172191
@Test
173192
void testJitterTruncation() {

0 commit comments

Comments
 (0)