Skip to content

Commit

Permalink
Merge pull request #1263 from DataDog/mar-kolya/dd-tags
Browse files Browse the repository at this point in the history
Introduce dd.tags
  • Loading branch information
mar-kolya authored Feb 27, 2020
2 parents e8d13aa + 0752b07 commit bc85e69
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 15 deletions.
29 changes: 21 additions & 8 deletions dd-trace-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public class Config {
public static final String TRACE_RESOLVER_ENABLED = "trace.resolver.enabled";
public static final String SERVICE_MAPPING = "service.mapping";

public static final String TAGS = "tags";
@Deprecated // Use dd.tags instead
public static final String GLOBAL_TAGS = "trace.global.tags";
public static final String SPAN_TAGS = "trace.span.tags";
public static final String JMX_TAGS = "trace.jmx.tags";
Expand Down Expand Up @@ -211,7 +213,8 @@ public enum PropagationStyle {
@Getter private final boolean prioritySamplingEnabled;
@Getter private final boolean traceResolverEnabled;
@Getter private final Map<String, String> serviceMapping;
private final Map<String, String> globalTags;
private final Map<String, String> tags;
@Deprecated private final Map<String, String> globalTags;
private final Map<String, String> spanTags;
private final Map<String, String> jmxTags;
@Getter private final List<String> excludedClasses;
Expand Down Expand Up @@ -303,6 +306,7 @@ public enum PropagationStyle {
getBooleanSettingFromEnvironment(TRACE_RESOLVER_ENABLED, DEFAULT_TRACE_RESOLVER_ENABLED);
serviceMapping = getMapSettingFromEnvironment(SERVICE_MAPPING, null);

tags = getMapSettingFromEnvironment(TAGS, null);
globalTags = getMapSettingFromEnvironment(GLOBAL_TAGS, null);
spanTags = getMapSettingFromEnvironment(SPAN_TAGS, null);
jmxTags = getMapSettingFromEnvironment(JMX_TAGS, null);
Expand Down Expand Up @@ -485,6 +489,7 @@ private Config(final Properties properties, final Config parent) {
getPropertyBooleanValue(properties, TRACE_RESOLVER_ENABLED, parent.traceResolverEnabled);
serviceMapping = getPropertyMapValue(properties, SERVICE_MAPPING, parent.serviceMapping);

tags = getPropertyMapValue(properties, TAGS, parent.tags);
globalTags = getPropertyMapValue(properties, GLOBAL_TAGS, parent.globalTags);
spanTags = getPropertyMapValue(properties, SPAN_TAGS, parent.spanTags);
jmxTags = getPropertyMapValue(properties, JMX_TAGS, parent.jmxTags);
Expand Down Expand Up @@ -638,9 +643,9 @@ public Map<String, String> getLocalRootSpanTags() {
}

public Map<String, String> getMergedSpanTags() {
// DO not include runtimeId into span tags: we only want that added to the root span
final Map<String, String> result = newHashMap(globalTags.size() + spanTags.size());
result.putAll(globalTags);
// Do not include runtimeId into span tags: we only want that added to the root span
final Map<String, String> result = newHashMap(getGlobalTags().size() + spanTags.size());
result.putAll(getGlobalTags());
result.putAll(spanTags);
return Collections.unmodifiableMap(result);
}
Expand All @@ -649,8 +654,8 @@ public Map<String, String> getMergedJmxTags() {
final Map<String, String> runtimeTags = getRuntimeTags();
final Map<String, String> result =
newHashMap(
globalTags.size() + jmxTags.size() + runtimeTags.size() + 1 /* for serviceName */);
result.putAll(globalTags);
getGlobalTags().size() + jmxTags.size() + runtimeTags.size() + 1 /* for serviceName */);
result.putAll(getGlobalTags());
result.putAll(jmxTags);
result.putAll(runtimeTags);
// service name set here instead of getRuntimeTags because apm already manages the service tag
Expand All @@ -665,12 +670,12 @@ public Map<String, String> getMergedProfilingTags() {
final String host = getHostName();
final Map<String, String> result =
newHashMap(
globalTags.size()
getGlobalTags().size()
+ profilingTags.size()
+ runtimeTags.size()
+ 3 /* for serviceName and host and language */);
result.put(HOST_TAG, host); // Host goes first to allow to override it
result.putAll(globalTags);
result.putAll(getGlobalTags());
result.putAll(profilingTags);
result.putAll(runtimeTags);
// service name set here instead of getRuntimeTags because apm already manages the service tag
Expand All @@ -694,6 +699,14 @@ public float getInstrumentationAnalyticsSampleRate(final String... aliases) {
return DEFAULT_ANALYTICS_SAMPLE_RATE;
}

/**
* Provide 'global' tags, i.e. tags set everywhere. We have to support old (dd.trace.global.tags)
* version of this setting if new (dd.tags) version has not been specified.
*/
private Map<String, String> getGlobalTags() {
return tags.isEmpty() ? globalTags : tags;
}

/**
* Return a map of tags required by the datadog backend to link runtime metrics (i.e. jmx) and
* traces.
Expand Down
80 changes: 73 additions & 7 deletions dd-trace-api/src/test/groovy/datadog/trace/api/ConfigTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import static datadog.trace.api.Config.SERVICE_NAME
import static datadog.trace.api.Config.SERVICE_TAG
import static datadog.trace.api.Config.SPAN_TAGS
import static datadog.trace.api.Config.SPLIT_BY_TAGS
import static datadog.trace.api.Config.TAGS
import static datadog.trace.api.Config.TRACE_AGENT_PORT
import static datadog.trace.api.Config.TRACE_ENABLED
import static datadog.trace.api.Config.TRACE_RATE_LIMIT
Expand All @@ -76,17 +77,21 @@ class ConfigTest extends DDSpecification {
private static final DD_TRACE_ENABLED_ENV = "DD_TRACE_ENABLED"
private static final DD_WRITER_TYPE_ENV = "DD_WRITER_TYPE"
private static final DD_SERVICE_MAPPING_ENV = "DD_SERVICE_MAPPING"
private static final DD_SPAN_TAGS_ENV = "DD_SPAN_TAGS"
private static final DD_HEADER_TAGS_ENV = "DD_HEADER_TAGS"
private static final DD_TAGS_ENV = "DD_TAGS"
private static final DD_GLOBAL_TAGS_ENV = "DD_TRACE_GLOBAL_TAGS"
private static final DD_SPAN_TAGS_ENV = "DD_TRACE_SPAN_TAGS"
private static final DD_HEADER_TAGS_ENV = "DD_TRACE_HEADER_TAGS"
private static final DD_JMX_TAGS_ENV = "DD_TRACE_JMX_TAGS"
private static final DD_PROPAGATION_STYLE_EXTRACT = "DD_PROPAGATION_STYLE_EXTRACT"
private static final DD_PROPAGATION_STYLE_INJECT = "DD_PROPAGATION_STYLE_INJECT"
private static final DD_JMXFETCH_METRICS_CONFIGS_ENV = "DD_JMXFETCH_METRICS_CONFIGS"
private static final DD_TRACE_AGENT_PORT_ENV = "DD_TRACE_AGENT_PORT"
private static final DD_AGENT_PORT_LEGACY_ENV = "DD_AGENT_PORT"
private static final DD_TRACE_REPORT_HOSTNAME = "DD_TRACE_REPORT_HOSTNAME"

private static final DD_PROFILING_API_KEY = "DD_PROFILING_API_KEY"
private static final DD_PROFILING_API_KEY_OLD = "DD_PROFILING_APIKEY"
private static final DD_PROFILING_API_KEY_ENV = "DD_PROFILING_API_KEY"
private static final DD_PROFILING_API_KEY_OLD_ENV = "DD_PROFILING_APIKEY"
private static final DD_PROFILING_TAGS_ENV = "DD_PROFILING_TAGS"

def "verify defaults"() {
when:
Expand Down Expand Up @@ -379,7 +384,7 @@ class ConfigTest extends DDSpecification {
environmentVariables.set(DD_PROPAGATION_STYLE_INJECT, "Datadog B3")
environmentVariables.set(DD_JMXFETCH_METRICS_CONFIGS_ENV, "some/file")
environmentVariables.set(DD_TRACE_REPORT_HOSTNAME, "true")
environmentVariables.set(DD_PROFILING_API_KEY, "test-api-key")
environmentVariables.set(DD_PROFILING_API_KEY_ENV, "test-api-key")

when:
def config = new Config()
Expand Down Expand Up @@ -990,7 +995,7 @@ class ConfigTest extends DDSpecification {
def "verify api key loaded from file: #path"() {
setup:
environmentVariables.set(DD_PROFILING_API_KEY, "default-api-key")
environmentVariables.set(DD_PROFILING_API_KEY_ENV, "default-api-key")
System.setProperty(PREFIX + PROFILING_API_KEY_FILE, path)
when:
Expand All @@ -1007,7 +1012,7 @@ class ConfigTest extends DDSpecification {
def "verify api key loaded from file for old option name: #path"() {
setup:
environmentVariables.set(DD_PROFILING_API_KEY_OLD, "default-api-key")
environmentVariables.set(DD_PROFILING_API_KEY_OLD_ENV, "default-api-key")
System.setProperty(PREFIX + PROFILING_API_KEY_FILE_OLD, path)
when:
Expand All @@ -1033,4 +1038,65 @@ class ConfigTest extends DDSpecification {
then:
config.profilingApiKey == "test-api-key"
}

def "verify dd.tags overrides global tags in properties"() {
setup:
def prop = new Properties()
prop.setProperty(TAGS, "a:1")
prop.setProperty(GLOBAL_TAGS, "b:2")
prop.setProperty(SPAN_TAGS, "c:3")
prop.setProperty(JMX_TAGS, "d:4")
prop.setProperty(HEADER_TAGS, "e:5")
prop.setProperty(PROFILING_TAGS, "f:6")

when:
Config config = Config.get(prop)

then:
config.mergedSpanTags == [a: "1", c: "3"]
config.mergedJmxTags == [a: "1", d: "4", (RUNTIME_ID_TAG): config.getRuntimeId(), (SERVICE_TAG): config.serviceName]
config.headerTags == [e: "5"]

config.mergedProfilingTags == [a: "1", f: "6", (HOST_TAG): config.getHostName(), (RUNTIME_ID_TAG): config.getRuntimeId(), (SERVICE_TAG): config.serviceName, (LANGUAGE_TAG_KEY): LANGUAGE_TAG_VALUE]
}

def "verify dd.tags overrides global tags in system properties"() {
setup:
System.setProperty(PREFIX + TAGS, "a:1")
System.setProperty(PREFIX + GLOBAL_TAGS, "b:2")
System.setProperty(PREFIX + SPAN_TAGS, "c:3")
System.setProperty(PREFIX + JMX_TAGS, "d:4")
System.setProperty(PREFIX + HEADER_TAGS, "e:5")
System.setProperty(PREFIX + PROFILING_TAGS, "f:6")

when:
Config config = new Config()

then:
config.mergedSpanTags == [a: "1", c: "3"]
config.mergedJmxTags == [a: "1", d: "4", (RUNTIME_ID_TAG): config.getRuntimeId(), (SERVICE_TAG): config.serviceName]
config.headerTags == [e: "5"]

config.mergedProfilingTags == [a: "1", f: "6", (HOST_TAG): config.getHostName(), (RUNTIME_ID_TAG): config.getRuntimeId(), (SERVICE_TAG): config.serviceName, (LANGUAGE_TAG_KEY): LANGUAGE_TAG_VALUE]
}

def "verify dd.tags overrides global tags in env variables"() {
setup:
environmentVariables.set(DD_TAGS_ENV, "a:1")
environmentVariables.set(DD_GLOBAL_TAGS_ENV, "b:2")
environmentVariables.set(DD_SPAN_TAGS_ENV, "c:3")
environmentVariables.set(DD_JMX_TAGS_ENV, "d:4")
environmentVariables.set(DD_HEADER_TAGS_ENV, "e:5")
environmentVariables.set(DD_PROFILING_TAGS_ENV, "f:6")

when:
Config config = new Config()

then:
config.mergedSpanTags == [a: "1", c: "3"]
config.mergedJmxTags == [a: "1", d: "4", (RUNTIME_ID_TAG): config.getRuntimeId(), (SERVICE_TAG): config.serviceName]
config.headerTags == [e: "5"]

config.mergedProfilingTags == [a: "1", f: "6", (HOST_TAG): config.getHostName(), (RUNTIME_ID_TAG): config.getRuntimeId(), (SERVICE_TAG): config.serviceName, (LANGUAGE_TAG_KEY): LANGUAGE_TAG_VALUE]
}
}

0 comments on commit bc85e69

Please sign in to comment.