Skip to content

Commit

Permalink
Move to a concurrent map for the meta struct field
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-alvarez-alvarez committed May 20, 2024
1 parent 83a1439 commit 2b40f78
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions dd-trace-core/src/main/java/datadog/trace/core/DDSpanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ public class DDSpanContext
implements AgentSpan.Context, RequestContext, TraceSegment, ProfilerContext {
private static final Logger log = LoggerFactory.getLogger(DDSpanContext.class);

private static final int INITIAL_META_STRUCT_SIZE = 1 << 3;

public static final String PRIORITY_SAMPLING_KEY = "_sampling_priority_v1";
public static final String SAMPLE_RATE_KEY = "_sample_rate";

Expand All @@ -66,6 +64,7 @@ public class DDSpanContext
DDCaches.newFixedSizeCache(256);

private static final Map<String, String> EMPTY_BAGGAGE = Collections.emptyMap();
private static final Map<String, Object> EMPTY_META_STRUCT = Collections.emptyMap();

/** The collection of all span related to this one */
private final PendingTrace trace;
Expand Down Expand Up @@ -146,12 +145,10 @@ public class DDSpanContext

/**
* Metastruct keys are associated to the current span, they will not propagate to the children
* span, they are an efficient way to send binary data to the agent without relying on bulky json
* span. They are an efficient way to send binary data to the agent without relying on bulky json
* payloads inside tags.
*
* <p>Metastruct follows the same assumptions as tags in terms of concurrency
*/
private Map<String, Object> unsafeMetaStruct;
private volatile Map<String, Object> metaStruct = EMPTY_META_STRUCT;

public DDSpanContext(
final DDTraceId traceId,
Expand Down Expand Up @@ -792,14 +789,9 @@ public Map<String, Object> getTags() {
}
}

/** Builds a readonly copy holding the current state of the metaStruct */
/** Builds a readonly view of the metaStruct */
public Map<String, Object> getMetaStruct() {
synchronized (this) {
if (null == unsafeMetaStruct || unsafeMetaStruct.isEmpty()) {
return Collections.emptyMap();
}
return Collections.unmodifiableMap(new HashMap<>(unsafeMetaStruct));
}
return Collections.unmodifiableMap(metaStruct);
}

/**
Expand All @@ -815,16 +807,18 @@ public <T> void setMetaStruct(final String field, final T value) {
if (null == field) {
return;
}
synchronized (this) {
if (null == unsafeMetaStruct) {
unsafeMetaStruct = new HashMap<>(INITIAL_META_STRUCT_SIZE);
}
if (null == value) {
unsafeMetaStruct.remove(field);
} else {
unsafeMetaStruct.put(field, value);
if (metaStruct == EMPTY_META_STRUCT) {
synchronized (this) {
if (metaStruct == EMPTY_META_STRUCT) {
metaStruct = new ConcurrentHashMap<>(4);
}
}
}
if (null == value) {
metaStruct.remove(field);
} else {
metaStruct.put(field, value);
}
}

public void processTagsAndBaggage(
Expand Down

0 comments on commit 2b40f78

Please sign in to comment.