Skip to content

Commit

Permalink
Add _dd.tracer_host to local root spans (#7426)
Browse files Browse the repository at this point in the history
  • Loading branch information
amarziali authored Aug 13, 2024
1 parent 4953aa9 commit 35e8f3d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ class TagsAssert {
// If runtime id is actually different here, it might indicate that
// the Config class was loaded on multiple different class loaders.
assert tags[DDTags.RUNTIME_ID_TAG] == Config.get().runtimeId
assertedTags.add(DDTags.TRACER_HOST)
assert tags[DDTags.TRACER_HOST] == Config.get().getHostName()
} else {
assert tags[DDTags.RUNTIME_ID_TAG] == null
}
Expand Down
1 change: 1 addition & 0 deletions dd-trace-api/src/main/java/datadog/trace/api/DDTags.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class DDTags {

/* Tags below are for internal use only. */
static final String INTERNAL_HOST_NAME = "_dd.hostname";
public static final String TRACER_HOST = "_dd.tracer_host";
public static final String RUNTIME_ID_TAG = "runtime-id";
public static final String RUNTIME_VERSION_TAG = "runtime_version";
static final String SERVICE = "service";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package datadog.trace.core.tagprocessor;

import datadog.trace.api.Config;
import datadog.trace.api.DDTags;
import datadog.trace.core.DDSpanContext;
import java.util.Map;

public class RemoteHostnameAdder implements TagsPostProcessor {
private final Config config;

public RemoteHostnameAdder(Config config) {
this.config = config;
}

@Override
public Map<String, Object> processTags(
Map<String, Object> unsafeTags, DDSpanContext spanContext) {
if (spanContext.getSpanId() == spanContext.getRootSpanId()) {
unsafeTags.put(DDTags.TRACER_HOST, config.getHostName());
}
return unsafeTags;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@

public final class TagsPostProcessorFactory {
private static boolean addBaseService = true;
private static boolean addRemoteHostname = true;

private static class Lazy {
private static TagsPostProcessor create() {
final List<TagsPostProcessor> processors = new ArrayList<>(addBaseService ? 3 : 2);
final List<TagsPostProcessor> processors = new ArrayList<>(4);
processors.add(new PeerServiceCalculator());
if (addBaseService) {
processors.add(new BaseServiceAdder(Config.get().getServiceName()));
}
processors.add(new QueryObfuscator(Config.get().getObfuscationQueryRegexp()));
if (addRemoteHostname) {
processors.add(new RemoteHostnameAdder(Config.get()));
}
return new PostProcessorChain(
processors.toArray(processors.toArray(new TagsPostProcessor[0])));
}
Expand All @@ -35,10 +39,19 @@ public static void withAddBaseService(boolean enabled) {
addBaseService = enabled;
Lazy.instance = Lazy.create();
}
/**
* Mostly used for test purposes.
*
* @param enabled if false, {@link RemoteHostnameAdder} is not put in the chain.
*/
public static void withAddRemoteHostname(boolean enabled) {
addRemoteHostname = enabled;
Lazy.instance = Lazy.create();
}

/** Used for testing purposes. It reset the singleton and restore default options */
public static void reset() {
withAddBaseService(true);
Lazy.instance = Lazy.create();
withAddRemoteHostname(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ abstract class DDCoreSpecification extends DDSpecification {
@Override
void setupSpec() {
TagsPostProcessorFactory.withAddBaseService(false)
TagsPostProcessorFactory.withAddRemoteHostname(false)
}

@Override
Expand Down

0 comments on commit 35e8f3d

Please sign in to comment.