1212import io .opentelemetry .sdk .trace .samplers .Sampler ;
1313import io .opentelemetry .sdk .trace .samplers .SamplingResult ;
1414import java .util .List ;
15- import java .util .concurrent .atomic .AtomicReference ;
1615import javax .annotation .Nullable ;
1716
1817/**
1918 * A {@link Sampler} implementation that delegates sampling decisions to another {@link Sampler}
20- * instance held in an {@link AtomicReference} . This allows the effective sampling strategy to be
21- * reconfigured at runtime without rebuilding the {@code TracerSdkProvider} or recreating
22- * instrumented components.
19+ * instance held in a volatile field . This allows the effective sampling strategy to be reconfigured
20+ * at runtime without rebuilding the {@code TracerSdkProvider} or recreating instrumented
21+ * components.
2322 *
24- * <p>This class is thread-safe. All access to the current delegate sampler is performed through an
25- * {@link AtomicReference} , so sampling decisions and delegate updates may occur concurrently
26- * without additional synchronization.
23+ * <p>This class is thread-safe. All access to the current delegate sampler is performed through a
24+ * volatile reference , so sampling decisions and delegate updates may occur concurrently without
25+ * additional synchronization.
2726 *
2827 * <p>The delegate sampler can be updated dynamically via {@link #setDelegate(Sampler)}. Passing
2928 * {@code null} to {@code setDelegate} or the constructor will cause {@link Sampler#alwaysOn()} to
3029 * be used as the fallback delegate.
3130 */
3231public class DelegatingSampler implements Sampler {
3332
34- private final AtomicReference < Sampler > delegate ;
33+ private volatile Sampler delegate ;
3534
3635 /**
3736 * Creates a new {@link DelegatingSampler} with the given initial delegate.
@@ -43,8 +42,7 @@ public class DelegatingSampler implements Sampler {
4342 * {@link Sampler#alwaysOn()} by default
4443 */
4544 public DelegatingSampler (@ Nullable Sampler initialDelegate ) {
46- this .delegate =
47- new AtomicReference <>(initialDelegate != null ? initialDelegate : Sampler .alwaysOn ());
45+ this .delegate = initialDelegate != null ? initialDelegate : Sampler .alwaysOn ();
4846 }
4947
5048 public DelegatingSampler () {
@@ -61,26 +59,23 @@ public DelegatingSampler() {
6159 * Sampler#alwaysOn()}
6260 */
6361 public void setDelegate (@ Nullable Sampler sampler ) {
64- delegate . set ( sampler != null ? sampler : Sampler .alwaysOn () );
62+ delegate = sampler != null ? sampler : Sampler .alwaysOn ();
6563 }
6664
6765 @ Override
68- // delegate AtomicReference is guaranteed to be non-null
69- @ SuppressWarnings ("NullAway" )
7066 public SamplingResult shouldSample (
7167 Context ctx ,
7268 String traceId ,
7369 String name ,
7470 SpanKind kind ,
7571 Attributes attrs ,
7672 List <LinkData > links ) {
77- return delegate .get (). shouldSample (ctx , traceId , name , kind , attrs , links );
73+ return delegate .shouldSample (ctx , traceId , name , kind , attrs , links );
7874 }
7975
8076 @ Override
81- @ SuppressWarnings ("NullAway" )
8277 public String getDescription () {
83- return "delegating/" + delegate .get (). getDescription ();
78+ return "delegating/" + delegate .getDescription ();
8479 }
8580
8681 @ Override
0 commit comments