1818import java .util .List ;
1919import java .util .Map ;
2020import java .util .Objects ;
21- import java .util .concurrent .atomic .AtomicInteger ;
21+ import java .util .concurrent .atomic .AtomicIntegerFieldUpdater ;
2222import javax .annotation .Nonnull ;
2323
2424public class PTagsFactory implements PropagationTags .Factory {
@@ -84,7 +84,10 @@ static class PTags extends PropagationTags {
8484 // extracted decision maker tag for easier updates
8585 private volatile TagValue decisionMakerTagValue ;
8686
87- private final AtomicInteger traceSource ;
87+ private static final AtomicIntegerFieldUpdater <PTags > AFU_TRACE_SOURCE =
88+ AtomicIntegerFieldUpdater .newUpdater (PTags .class , "traceSource" );
89+
90+ private volatile int traceSource ;
8891 private volatile String debugPropagation ;
8992
9093 // xDatadogTagsSize of the tagPairs, does not include the decision maker tag
@@ -152,7 +155,7 @@ public PTags(
152155 this .tagPairs = tagPairs ;
153156 this .canChangeDecisionMaker = decisionMakerTagValue == null ;
154157 this .decisionMakerTagValue = decisionMakerTagValue ;
155- this .traceSource = new AtomicInteger ( traceSource ) ;
158+ this .traceSource = traceSource ;
156159 this .samplingPriority = samplingPriority ;
157160 this .origin = origin ;
158161 this .lastParentId = lastParentId ;
@@ -230,7 +233,8 @@ private void doUpdateTraceSamplingPriority(int samplingPriority, int samplingMec
230233
231234 @ Override
232235 public void addTraceSource (final int product ) {
233- traceSource .updateAndGet (
236+ AFU_TRACE_SOURCE .updateAndGet (
237+ this ,
234238 currentValue -> {
235239 // If the product is already marked, return the same value (no change)
236240 if (ProductTraceSource .isProductMarked (currentValue , product )) {
@@ -248,7 +252,7 @@ public void addTraceSource(final int product) {
248252
249253 @ Override
250254 public int getTraceSource () {
251- return traceSource . get () ;
255+ return traceSource ;
252256 }
253257
254258 @ Override
@@ -386,7 +390,7 @@ int getXDatadogTagsSize() {
386390 size = PTagsCodec .calcXDatadogTagsSize (getTagPairs ());
387391 size = PTagsCodec .calcXDatadogTagsSize (size , DECISION_MAKER_TAG , decisionMakerTagValue );
388392 size = PTagsCodec .calcXDatadogTagsSize (size , TRACE_ID_TAG , traceIdHighOrderBitsHexTagValue );
389- int currentProductTraceSource = traceSource . get () ;
393+ int currentProductTraceSource = traceSource ;
390394 if (currentProductTraceSource != ProductTraceSource .UNSET ) {
391395 size =
392396 PTagsCodec .calcXDatadogTagsSize (
0 commit comments