Skip to content

Commit 9f610f9

Browse files
committed
TagMap that can "mostly" be toggled on / off
Changes TagMap to be an interface with two distinct implementations: - One that extends HashMap - One that implements the new map structure Next change will make it possible to switch between the map implementations via configuration To try and make both TagMap implementations reasonably efficient, added a more method variations: - set methods now return void to avoid Entry creation for HashMap implementation - getAndSet - replaces set for the add cases where the previous Entry is needed - remove(String) - now returns a boolean to indicate that a change happened rather than returning the value or entry - getAndRemove - returns an Entry Many TagMapTest and TagMapLedgerTest have been parameterized to test both implementations of TagMaps Updated calling code & other tests as needed
1 parent 8612213 commit 9f610f9

File tree

18 files changed

+2259
-1516
lines changed

18 files changed

+2259
-1516
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/instrumentation/decorator/HttpServerDecorator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import datadog.trace.api.Config;
1414
import datadog.trace.api.DDTags;
1515
import datadog.trace.api.DDTraceId;
16+
import datadog.trace.api.TagMap;
1617
import datadog.trace.api.TraceConfig;
1718
import datadog.trace.api.function.TriConsumer;
1819
import datadog.trace.api.function.TriFunction;
@@ -408,7 +409,7 @@ public String getSpanType() {
408409
}
409410

410411
@Override
411-
public Map<String, Object> getTags() {
412+
public TagMap getTags() {
412413
return serverSpan.getTags();
413414
}
414415

dd-java-agent/agent-debugger/src/test/java/com/datadog/debugger/exception/DefaultExceptionDebuggerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class DefaultExceptionDebuggerTest {
5757
private ConfigurationUpdater configurationUpdater;
5858
private DefaultExceptionDebugger exceptionDebugger;
5959
private TestSnapshotListener listener;
60-
private TagMap spanTags = new TagMap();
60+
private TagMap spanTags = TagMap.create();
6161

6262
@BeforeEach
6363
public void setUp() {

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/gateway/GatewayBridgeSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,7 @@ class GatewayBridgeSpecification extends DDSpecification {
989989
getTraceSegment() >> traceSegment
990990
}
991991
final spanInfo = Mock(AgentSpan) {
992-
getTags() >> ['http.route':'/']
992+
getTags() >> TagMap.fromMap(['http.route':'/'])
993993
}
994994

995995
when:

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1888,7 +1888,7 @@ protected ConfigSnapshot(
18881888
*/
18891889
static TagMap withTracerTags(
18901890
Map<String, ?> userSpanTags, Config config, TraceConfig traceConfig) {
1891-
final TagMap result = new TagMap();
1891+
final TagMap result = TagMap.create();
18921892
result.putAll(userSpanTags);
18931893
if (null != config) { // static
18941894
if (!config.getEnv().isEmpty()) {

dd-trace-core/src/main/java/datadog/trace/core/DDSpanContext.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ public DDSpanContext(
342342
assert pathwayContext != null;
343343
this.pathwayContext = pathwayContext;
344344

345-
this.unsafeTags = new TagMap();
345+
// The +1 is the magic number from the tags below that we set at the end,
346+
// and "* 4 / 3" is to make sure that we don't resize immediately
347+
final int capacity = Math.max((tagsSize <= 0 ? 3 : (tagsSize + 1)) * 4 / 3, 8);
348+
this.unsafeTags = TagMap.create(capacity);
346349

347350
// must set this before setting the service and resource names below
348351
this.profilingContextIntegration = profilingContextIntegration;
@@ -772,7 +775,7 @@ void setAllTags(final TagMap map, boolean needsIntercept) {
772775
Object value = tagEntry.objectValue();
773776

774777
if (!tagInterceptor.interceptTag(ctx, tag, value)) {
775-
ctx.unsafeTags.putEntry(tagEntry);
778+
ctx.unsafeTags.set(tagEntry);
776779
}
777780
});
778781
} else {
@@ -790,15 +793,15 @@ void setAllTags(final TagMap.Ledger ledger) {
790793
synchronized (unsafeTags) {
791794
for (final TagMap.EntryChange entryChange : ledger) {
792795
if (entryChange.isRemoval()) {
793-
unsafeTags.removeEntry(entryChange.tag());
796+
unsafeTags.remove(entryChange.tag());
794797
} else {
795798
TagMap.Entry entry = (TagMap.Entry) entryChange;
796799

797800
String tag = entry.tag();
798801
Object value = entry.objectValue();
799802

800803
if (!tagInterceptor.interceptTag(this, tag, value)) {
801-
unsafeTags.putEntry(entry);
804+
unsafeTags.set(entry);
802805
}
803806
}
804807
}

dd-trace-core/src/main/java/datadog/trace/core/tagprocessor/IntegrationAdder.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,20 @@
22

33
import static datadog.trace.api.DDTags.DD_INTEGRATION;
44

5+
import datadog.trace.api.TagMap;
56
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
67
import datadog.trace.core.DDSpanContext;
78
import java.util.List;
8-
import java.util.Map;
9-
10-
public class IntegrationAdder implements TagsPostProcessor {
119

10+
public class IntegrationAdder extends TagsPostProcessor {
1211
@Override
13-
public Map<String, Object> processTags(
14-
Map<String, Object> unsafeTags, DDSpanContext spanContext, List<AgentSpanLink> spanLinks) {
12+
public void processTags(
13+
TagMap unsafeTags, DDSpanContext spanContext, List<AgentSpanLink> spanLinks) {
1514
final CharSequence instrumentationName = spanContext.getIntegrationName();
1615
if (instrumentationName != null) {
17-
unsafeTags.put(DD_INTEGRATION, instrumentationName);
16+
unsafeTags.set(DD_INTEGRATION, instrumentationName);
1817
} else {
1918
unsafeTags.remove(DD_INTEGRATION);
2019
}
21-
return unsafeTags;
2220
}
2321
}

dd-trace-core/src/main/java/datadog/trace/core/tagprocessor/PayloadTagsProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void processTags(
7979
RedactionRules redactionRules = tagPrefixRedactionRules.getValue();
8080
Object tagValue = unsafeTags.getObject(tagPrefix);
8181
if (tagValue instanceof PayloadTagsData) {
82-
if (unsafeTags.remove(tagPrefix) != null) {
82+
if (unsafeTags.remove(tagPrefix)) {
8383
spanMaxTags -= 1;
8484
}
8585

dd-trace-core/src/test/groovy/datadog/trace/core/datastreams/DefaultPathwayContextTest.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package datadog.trace.core.datastreams
33
import datadog.communication.ddagent.DDAgentFeaturesDiscovery
44
import datadog.trace.api.Config
55
import datadog.trace.api.DDTraceId
6+
import datadog.trace.api.TagMap
67
import datadog.trace.api.TraceConfig
78
import datadog.trace.api.WellKnownTags
89
import datadog.trace.api.datastreams.StatsPoint
@@ -454,7 +455,7 @@ class DefaultPathwayContextTest extends DDCoreSpecification {
454455
Map<String, String> carrier = [(PROPAGATION_KEY_BASE64): encoded, "someotherkey": "someothervalue"]
455456
def contextVisitor = new Base64MapContextVisitor()
456457
457-
def spanContext = new ExtractedContext(DDTraceId.ONE, 1, 0, null, 0, null, null, null, null, localTraceConfig, DATADOG)
458+
def spanContext = new ExtractedContext(DDTraceId.ONE, 1, 0, null, 0, null, (TagMap)null, null, null, localTraceConfig, DATADOG)
458459
def baseContext = AgentSpan.fromSpanContext(spanContext).storeInto(root())
459460
def propagator = dataStreams.propagator()
460461
@@ -549,7 +550,7 @@ class DefaultPathwayContextTest extends DDCoreSpecification {
549550
def encoded = context.encode()
550551
Map<String, String> carrier = [(PROPAGATION_KEY_BASE64): encoded, "someotherkey": "someothervalue"]
551552
def contextVisitor = new Base64MapContextVisitor()
552-
def spanContext = new ExtractedContext(DDTraceId.ONE, 1, 0, null, 0, null, null, null, null, null, DATADOG)
553+
def spanContext = new ExtractedContext(DDTraceId.ONE, 1, 0, null, 0, null, (TagMap)null, null, null, null, DATADOG)
553554
def baseContext = AgentSpan.fromSpanContext(spanContext).storeInto(root())
554555
def propagator = dataStreams.propagator()
555556

internal-api/src/main/java/datadog/trace/api/Config.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3657,9 +3657,7 @@ public boolean isJdkSocketEnabled() {
36573657
public TagMap getLocalRootSpanTags() {
36583658
final Map<String, String> runtimeTags = getRuntimeTags();
36593659

3660-
final TagMap result = new TagMap();
3661-
result.putAll(runtimeTags);
3662-
3660+
final TagMap result = TagMap.fromMap(runtimeTags);
36633661
result.put(LANGUAGE_TAG_KEY, LANGUAGE_TAG_VALUE);
36643662
result.put(SCHEMA_VERSION_TAG_KEY, SpanNaming.instance().version());
36653663
result.put(DDTags.PROFILING_ENABLED, isProfilingEnabled() ? 1 : 0);

0 commit comments

Comments
 (0)