@@ -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 ;
0 commit comments