Skip to content

Commit a519cba

Browse files
committed
Fix AtomicInteger wrong use
1 parent 6f6d6ae commit a519cba

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

dd-trace-core/src/main/java/datadog/trace/core/propagation/ptags/PTagsFactory.java

+17-12
Original file line numberDiff line numberDiff line change
@@ -205,16 +205,20 @@ public void updateTraceSamplingPriority(int samplingPriority, int samplingMechan
205205

206206
@Override
207207
public void updatePropagatedTraceSource(final int product) {
208-
// if is nort marked for the product
209-
if (!ProductTraceSource.isProductMarked(productTraceSource.get(), product)) {
210-
// This should invalidate any cached w3c and datadog header
211-
clearCachedHeader(DATADOG);
212-
clearCachedHeader(W3C);
213-
productTraceSource.updateAndGet(
214-
value ->
215-
ProductTraceSource.updateProduct(
216-
value, product)); // Set the bit for the given product
217-
}
208+
productTraceSource.updateAndGet(
209+
currentValue -> {
210+
// If the product is already marked, return the same value (no change)
211+
if (ProductTraceSource.isProductMarked(currentValue, product)) {
212+
return currentValue;
213+
}
214+
215+
// Invalidate cached headers (atomic context ensures correctness)
216+
clearCachedHeader(DATADOG);
217+
clearCachedHeader(W3C);
218+
219+
// Set the bit for the given product
220+
return ProductTraceSource.updateProduct(currentValue, product);
221+
});
218222
}
219223

220224
@Override
@@ -357,12 +361,13 @@ int getXDatadogTagsSize() {
357361
size = PTagsCodec.calcXDatadogTagsSize(getTagPairs());
358362
size = PTagsCodec.calcXDatadogTagsSize(size, DECISION_MAKER_TAG, decisionMakerTagValue);
359363
size = PTagsCodec.calcXDatadogTagsSize(size, TRACE_ID_TAG, traceIdHighOrderBitsHexTagValue);
360-
if (productTraceSource.get() != 0) {
364+
int currentProductTraceSource = productTraceSource.get();
365+
if (currentProductTraceSource != 0) {
361366
size =
362367
PTagsCodec.calcXDatadogTagsSize(
363368
size,
364369
TRACE_SOURCE_TAG,
365-
TagValue.from(ProductTraceSource.getBitfieldHex(productTraceSource.get())));
370+
TagValue.from(ProductTraceSource.getBitfieldHex(currentProductTraceSource)));
366371
}
367372
xDatadogTagsSize = size;
368373
}

0 commit comments

Comments
 (0)