Skip to content

Commit 6c468be

Browse files
amarzialidevflow.devflow-routing-intake
andauthored
Do not remove the version tag if manually set (#10703)
do not remove the version tag on the BaseServiceAdder Make version working nicely with config consistency and manual set spans Fix tests and corner cases Co-authored-by: devflow.devflow-routing-intake <devflow.devflow-routing-intake@kubernetes.us1.ddbuild.io>
1 parent 4bd113b commit 6c468be

File tree

9 files changed

+111
-70
lines changed

9 files changed

+111
-70
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
import datadog.trace.bootstrap.instrumentation.api.SpanAttributes;
6969
import datadog.trace.bootstrap.instrumentation.api.SpanLink;
7070
import datadog.trace.bootstrap.instrumentation.api.TagContext;
71+
import datadog.trace.bootstrap.instrumentation.api.Tags;
7172
import datadog.trace.civisibility.interceptor.CiVisibilityApmProtocolInterceptor;
7273
import datadog.trace.civisibility.interceptor.CiVisibilityTelemetryInterceptor;
7374
import datadog.trace.civisibility.interceptor.CiVisibilityTraceInterceptor;
@@ -2074,6 +2075,9 @@ protected static final DDSpanContext buildSpanContext(
20742075
context.setAllTags(coreTags, coreTagsNeedsIntercept);
20752076
context.setAllTags(rootSpanTags, rootSpanTagsNeedsIntercept);
20762077
context.setAllTags(contextualTags);
2078+
// remove version here since will be done later on the postProcessor.
2079+
// it will allow knowing if it will be set manually or not
2080+
context.removeTag(Tags.VERSION);
20772081
return context;
20782082
}
20792083
}
@@ -2238,9 +2242,6 @@ static TagMap withTracerTags(
22382242
if (!config.getEnv().isEmpty()) {
22392243
result.put("env", config.getEnv());
22402244
}
2241-
if (!config.getVersion().isEmpty()) {
2242-
result.put("version", config.getVersion());
2243-
}
22442245
if (config.isDataJobsEnabled()) {
22452246
result.put(DJM_ENABLED, 1);
22462247
}

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

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package datadog.trace.core.tagprocessor;
2+
3+
import static datadog.trace.bootstrap.instrumentation.api.Tags.VERSION;
4+
5+
import datadog.trace.api.DDTags;
6+
import datadog.trace.api.TagMap;
7+
import datadog.trace.bootstrap.instrumentation.api.AgentSpanLink;
8+
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString;
9+
import datadog.trace.core.DDSpanContext;
10+
import java.util.List;
11+
import javax.annotation.Nullable;
12+
13+
public final class InternalTagsAdder extends TagsPostProcessor {
14+
private final UTF8BytesString ddService;
15+
private final UTF8BytesString version;
16+
17+
public InternalTagsAdder(@Nullable final String ddService, @Nullable final String version) {
18+
this.ddService = ddService != null ? UTF8BytesString.create(ddService) : null;
19+
this.version = version != null && !version.isEmpty() ? UTF8BytesString.create(version) : null;
20+
}
21+
22+
@Override
23+
public void processTags(
24+
TagMap unsafeTags, DDSpanContext spanContext, List<AgentSpanLink> spanLinks) {
25+
if (spanContext == null || ddService == null) {
26+
return;
27+
}
28+
29+
if (!ddService.toString().equalsIgnoreCase(spanContext.getServiceName())) {
30+
// service name != DD_SERVICE
31+
unsafeTags.set(DDTags.BASE_SERVICE, ddService);
32+
} else {
33+
// as per config consistency, the version tag is added across tracers only if
34+
// the service name is DD_SERVICE and version tag is not manually set
35+
if (version != null && !unsafeTags.containsKey(VERSION)) {
36+
unsafeTags.set(VERSION, version);
37+
}
38+
}
39+
}
40+
}

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import java.util.List;
66

77
public final class TagsPostProcessorFactory {
8-
private static boolean addBaseService = true;
8+
private static boolean addInternalTags = true;
99
private static boolean addRemoteHostname = true;
1010

1111
private static class Lazy {
@@ -15,8 +15,9 @@ private static class Lazy {
1515
private static TagsPostProcessor createEagerChain() {
1616
final List<TagsPostProcessor> processors = new ArrayList<>(3);
1717
processors.add(new PeerServiceCalculator());
18-
if (addBaseService) {
19-
processors.add(new BaseServiceAdder(Config.get().getServiceName()));
18+
if (addInternalTags) {
19+
processors.add(
20+
new InternalTagsAdder(Config.get().getServiceName(), Config.get().getVersion()));
2021
}
2122
// Add HTTP endpoint post processor for resource renaming
2223
// This must run BEFORE metrics aggregation so the correct resource name is used in metrics
@@ -64,10 +65,10 @@ public static TagsPostProcessor lazyProcessor() {
6465
/**
6566
* Mostly used for test purposes.
6667
*
67-
* @param enabled if false, {@link BaseServiceAdder} is not put in the chain.
68+
* @param enabled if false, {@link InternalTagsAdder} is not put in the chain.
6869
*/
69-
public static void withAddBaseService(boolean enabled) {
70-
addBaseService = enabled;
70+
public static void withAddInternalTags(boolean enabled) {
71+
addInternalTags = enabled;
7172
Lazy.eagerProcessor = Lazy.createEagerChain();
7273
}
7374

@@ -83,7 +84,7 @@ public static void withAddRemoteHostname(boolean enabled) {
8384

8485
/** Used for testing purposes. It reset the singleton and restore default options */
8586
public static void reset() {
86-
withAddBaseService(true);
87+
withAddInternalTags(true);
8788
withAddRemoteHostname(true);
8889
}
8990
}

dd-trace-core/src/test/groovy/datadog/trace/core/CoreTracerTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ class CoreTracerTest extends DDCoreSpecification {
476476
setup:
477477
injectSysConfig(SERVICE_NAME, "dd_service_name")
478478
injectSysConfig(VERSION, "1.0.0")
479-
TagsPostProcessorFactory.withAddBaseService(true)
479+
TagsPostProcessorFactory.withAddInternalTags(true)
480480
def tracer = tracerBuilder().writer(new ListWriter()).build()
481481

482482
when:
@@ -491,7 +491,7 @@ class CoreTracerTest extends DDCoreSpecification {
491491
span2.finish()
492492
then:
493493
span2.getServiceName() == "dd_service_name"
494-
span2.getTags()["version"] == "1.0.0"
494+
span2.getTags()["version"]?.toString() == "1.0.0"
495495

496496
cleanup:
497497
tracer?.close()

dd-trace-core/src/test/groovy/datadog/trace/core/tagprocessor/BaseServiceAdderTest.groovy

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package datadog.trace.core.tagprocessor
2+
3+
import static datadog.trace.bootstrap.instrumentation.api.Tags.VERSION
4+
5+
import datadog.trace.bootstrap.instrumentation.api.UTF8BytesString
6+
import datadog.trace.core.DDSpanContext
7+
import datadog.trace.test.util.DDSpecification
8+
9+
class InternalTagsAdderTest extends DDSpecification {
10+
def "should add _dd.base_service when service differs to ddService"() {
11+
setup:
12+
def calculator = new InternalTagsAdder("test", null)
13+
def spanContext = Mock(DDSpanContext)
14+
15+
when:
16+
def enrichedTags = calculator.processTags([:], spanContext, [])
17+
18+
then:
19+
1 * spanContext.getServiceName() >> serviceName
20+
21+
and:
22+
assert enrichedTags == expectedTags
23+
24+
where:
25+
serviceName | expectedTags
26+
"anotherOne" | ["_dd.base_service": UTF8BytesString.create("test")]
27+
"test" | [:]
28+
"TeSt" | [:]
29+
}
30+
31+
def "should add version when DD_SERVICE = #serviceName and version = #ddVersion"() {
32+
setup:
33+
def calculator = new InternalTagsAdder("same", ddVersion)
34+
def spanContext = Mock(DDSpanContext)
35+
36+
when:
37+
def enrichedTags = calculator.processTags(tags, spanContext, [])
38+
39+
then:
40+
1 * spanContext.getServiceName() >> serviceName
41+
42+
and:
43+
assert enrichedTags?.get(VERSION)?.toString() == expected
44+
45+
46+
where:
47+
serviceName | ddVersion | tags | expected
48+
"same" | null | [:] | null
49+
"different" | "1.0" | [:] | null
50+
"different" | "1.0" | ["version": "2.0"] | "2.0"
51+
"same" | null | ["version": "2.0"] | "2.0"
52+
"same" | "1.0" | ["version": "2.0"] | "2.0"
53+
"same" | "1.0" | [:] | "1.0"
54+
}
55+
}

dd-trace-core/src/test/groovy/datadog/trace/core/test/DDCoreSpecification.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ abstract class DDCoreSpecification extends DDSpecification {
4545

4646
@Override
4747
void setupSpec() {
48-
TagsPostProcessorFactory.withAddBaseService(false)
48+
TagsPostProcessorFactory.withAddInternalTags(false)
4949
TagsPostProcessorFactory.withAddRemoteHostname(false)
5050
}
5151

internal-api/src/main/java/datadog/trace/bootstrap/instrumentation/api/Tags.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ public class Tags {
164164
public static final String DD_ENV = "dd.env";
165165

166166
public static final String ENV = "env";
167+
public static final String VERSION = "version";
167168

168169
/** ASM force tracer to keep the trace */
169170
public static final String ASM_KEEP = "asm.keep";

0 commit comments

Comments
 (0)