Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 57 additions & 21 deletions dd-trace-api/src/main/java/datadog/trace/api/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
@Slf4j
@ToString(includeFieldNames = true)
public class Config {
private static final MethodHandles.Lookup PUBLIC_LOOKUP = MethodHandles.publicLookup();

/** Config keys below */
private static final String PREFIX = "dd.";
Expand Down Expand Up @@ -263,8 +264,7 @@ private String profilingProxyPasswordMasker() {
@Getter private final boolean prioritySamplingEnabled;
@Getter private final boolean traceResolverEnabled;
@Getter private final Map<String, String> serviceMapping;
private final Map<String, String> tags;
@Deprecated private final Map<String, String> globalTags;
@NonNull private final Map<String, String> tags;
private final Map<String, String> spanTags;
private final Map<String, String> jmxTags;
@Getter private final List<String> excludedClasses;
Expand Down Expand Up @@ -356,7 +356,7 @@ private String profilingProxyPasswordMasker() {
site = getSettingFromEnvironment(SITE, DEFAULT_SITE);
serviceName =
getSettingFromEnvironment(
SERVICE_NAME, getSettingFromEnvironment(SERVICE, DEFAULT_SERVICE_NAME));
SERVICE, getSettingFromEnvironment(SERVICE_NAME, DEFAULT_SERVICE_NAME));

traceEnabled = getBooleanSettingFromEnvironment(TRACE_ENABLED, DEFAULT_TRACE_ENABLED);
integrationsEnabled =
Expand All @@ -375,11 +375,13 @@ private String profilingProxyPasswordMasker() {
getBooleanSettingFromEnvironment(TRACE_RESOLVER_ENABLED, DEFAULT_TRACE_RESOLVER_ENABLED);
serviceMapping = getMapSettingFromEnvironment(SERVICE_MAPPING, null);

final Map<String, String> tagsPreMap = new HashMap<>(getMapSettingFromEnvironment(TAGS, null));
addPropToMapIfDefinedByEnvironment(tagsPreMap, ENV);
addPropToMapIfDefinedByEnvironment(tagsPreMap, VERSION);
tags = Collections.unmodifiableMap(tagsPreMap);
globalTags = getMapSettingFromEnvironment(GLOBAL_TAGS, null);
{
final Map<String, String> tags =
new HashMap<>(getMapSettingFromEnvironment(GLOBAL_TAGS, null));
tags.putAll(getMapSettingFromEnvironment(TAGS, null));
this.tags = getMapWithPropertiesDefinedByEnvironment(tags, ENV, VERSION);
}

spanTags = getMapSettingFromEnvironment(SPAN_TAGS, null);
jmxTags = getMapSettingFromEnvironment(JMX_TAGS, null);

Expand Down Expand Up @@ -555,7 +557,8 @@ private Config(final Properties properties, final Config parent) {

apiKey = properties.getProperty(API_KEY, parent.apiKey);
site = properties.getProperty(SITE, parent.site);
serviceName = properties.getProperty(SERVICE_NAME, parent.serviceName);
serviceName =
properties.getProperty(SERVICE, properties.getProperty(SERVICE_NAME, parent.serviceName));

traceEnabled = getPropertyBooleanValue(properties, TRACE_ENABLED, parent.traceEnabled);
integrationsEnabled =
Expand All @@ -575,8 +578,13 @@ 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);
{
final Map<String, String> preTags =
new HashMap<>(
getPropertyMapValue(properties, GLOBAL_TAGS, Collections.<String, String>emptyMap()));
preTags.putAll(getPropertyMapValue(properties, TAGS, parent.tags));
this.tags = overwriteKeysFromProperties(preTags, properties, ENV, VERSION);
}
spanTags = getPropertyMapValue(properties, SPAN_TAGS, parent.spanTags);
jmxTags = getPropertyMapValue(properties, JMX_TAGS, parent.jmxTags);
excludedClasses =
Expand Down Expand Up @@ -807,7 +815,7 @@ public float getInstrumentationAnalyticsSampleRate(final String... aliases) {
* version of this setting if new (dd.tags) version has not been specified.
*/
private Map<String, String> getGlobalTags() {
return tags.isEmpty() ? globalTags : tags;
return tags;
}

/**
Expand Down Expand Up @@ -1101,7 +1109,7 @@ private static <T> T valueOf(
}
try {
return (T)
MethodHandles.publicLookup()
PUBLIC_LOOKUP
.findStatic(tClass, "valueOf", MethodType.methodType(tClass, String.class))
.invoke(value);
} catch (NumberFormatException e) {
Expand Down Expand Up @@ -1236,16 +1244,44 @@ private static Map<String, String> newHashMap(final int size) {

/**
* @param map
* @param propName
* @return true if map was modified
* @param propNames
* @return new unmodifiable copy of {@param map} where properties are overwritten from environment
*/
@NonNull
private static Map<String, String> getMapWithPropertiesDefinedByEnvironment(
@NonNull final Map<String, String> map, @NonNull final String... propNames) {
final Map<String, String> res = new HashMap<>(map);
for (final String propName : propNames) {
final String val = getSettingFromEnvironment(propName, null);
if (val != null) {
res.put(propName, val);
}
}
return Collections.unmodifiableMap(res);
}

/**
* same as {@link Config#getMapWithPropertiesDefinedByEnvironment(Map, String...)} but using
* {@code properties} as source of values to overwrite inside map
*
* @param map
* @param properties
* @param keys
* @return
*/
private static boolean addPropToMapIfDefinedByEnvironment(
final Map<String, String> map, final String propName) {
final String val = getSettingFromEnvironment(propName, null);
if (val != null) {
return !val.equals(map.put(propName, val));
@NonNull
private static Map<String, String> overwriteKeysFromProperties(
@NonNull final Map<String, String> map,
@NonNull final Properties properties,
@NonNull final String... keys) {
final Map<String, String> res = new HashMap<>(map);
for (final String propName : keys) {
final String val = properties.getProperty(propName, null);
if (val != null) {
res.put(propName, val);
}
}
return false;
return Collections.unmodifiableMap(res);
}

@NonNull
Expand Down
Loading