Skip to content

Commit 277f047

Browse files
committed
much better to use volatile per trask's suggestion
1 parent a944929 commit 277f047

1 file changed

Lines changed: 11 additions & 16 deletions

File tree

dynamic-control/src/main/java/io/opentelemetry/contrib/dynamic/sampler/DelegatingSampler.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,25 @@
1212
import io.opentelemetry.sdk.trace.samplers.Sampler;
1313
import io.opentelemetry.sdk.trace.samplers.SamplingResult;
1414
import java.util.List;
15-
import java.util.concurrent.atomic.AtomicReference;
1615
import 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
*/
3231
public 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

Comments
 (0)