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
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void setSpanType(final String spanType) {
this.spanType = spanType;
}

public void setSamplingPriority(int newPriority) {
public void setSamplingPriority(final int newPriority) {
if (samplingPriorityLocked) {
log.warn(
"samplingPriority locked at {}. Refusing to set to {}", samplingPriority, newPriority);
Expand Down Expand Up @@ -272,7 +272,7 @@ public synchronized void setTag(final String tag, final Object value) {

public synchronized Map<String, Object> getTags() {
if (tags.isEmpty()) {
tags = Maps.newHashMapWithExpectedSize(2);
tags = Maps.newHashMapWithExpectedSize(3);
}
tags.put(DDTags.THREAD_NAME, threadName);
tags.put(DDTags.THREAD_ID, threadId);
Expand Down
30 changes: 26 additions & 4 deletions dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package datadog.opentracing;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Maps;
import datadog.opentracing.decorators.AbstractDecorator;
import datadog.opentracing.decorators.DDDecoratorsFactory;
import datadog.opentracing.propagation.Codec;
Expand Down Expand Up @@ -44,6 +45,9 @@ public class DDTracer extends ThreadLocalScopeManager implements io.opentracing.
/** Sampler defines the sampling policy in order to reduce the number of traces for instance */
final Sampler sampler;

/** A set of tags that are added to every span */
private final Map<String, Object> spanTags;

/** Span context decorators */
private final Map<String, List<AbstractDecorator>> spanContextDecorators = new HashMap<>();

Expand All @@ -63,7 +67,8 @@ public DDTracer(final Properties config) {
this(
config.getProperty(DDTraceConfig.SERVICE_NAME),
Writer.Builder.forConfig(config),
Sampler.Builder.forConfig(config));
Sampler.Builder.forConfig(config),
DDTraceConfig.parseMap(config.getProperty(DDTraceConfig.SPAN_TAGS)));
log.debug("Using config: {}", config);

// Create decorators from resource files
Expand All @@ -75,10 +80,20 @@ public DDTracer(final Properties config) {
}

public DDTracer(final String serviceName, final Writer writer, final Sampler sampler) {
this(serviceName, writer, sampler, Collections.<String, Object>emptyMap());
}

public DDTracer(
final String serviceName,
final Writer writer,
final Sampler sampler,
final Map<String, Object> spanTags) {
this.serviceName = serviceName;
this.writer = writer;
this.writer.start();
this.sampler = sampler;
this.spanTags = spanTags;

registry = new CodecRegistry();
registry.register(Format.Builtin.HTTP_HEADERS, new HTTPCodec());
registry.register(Format.Builtin.TEXT_MAP, new HTTPCodec());
Expand All @@ -90,7 +105,11 @@ public DDTracer(final String serviceName, final Writer writer, final Sampler sam
}

public DDTracer(final Writer writer) {
this(UNASSIGNED_DEFAULT_SERVICE_NAME, writer, new AllSampler());
this(
UNASSIGNED_DEFAULT_SERVICE_NAME,
writer,
new AllSampler(),
DDTraceConfig.parseMap(new DDTraceConfig().getProperty(DDTraceConfig.SPAN_TAGS)));
}

/**
Expand Down Expand Up @@ -185,6 +204,8 @@ public String toString() {
+ writer
+ ", sampler="
+ sampler
+ ", tags="
+ spanTags
+ '}';
}

Expand Down Expand Up @@ -236,7 +257,8 @@ public class DDSpanBuilder implements SpanBuilder {
private final String operationName;

// Builder attributes
private Map<String, Object> tags = Collections.emptyMap();
private Map<String, Object> tags =
spanTags.isEmpty() ? Collections.<String, Object>emptyMap() : Maps.newHashMap(spanTags);
private long timestamp;
private SpanContext parent;
private String serviceName;
Expand Down Expand Up @@ -361,7 +383,7 @@ public DDSpanBuilder addReference(final String referenceType, final SpanContext
// Private methods
private DDSpanBuilder withTag(final String tag, final Object value) {
if (this.tags.isEmpty()) {
this.tags = new HashMap<>();
this.tags = Maps.newHashMap();
}
this.tags.put(tag, value);
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package datadog.trace.common;

import com.google.common.collect.Maps;
import datadog.opentracing.DDTracer;
import datadog.trace.common.writer.DDAgentWriter;
import datadog.trace.common.writer.Writer;
import java.util.Collections;
import java.util.Map;
import java.util.Properties;
import lombok.extern.slf4j.Slf4j;

/**
* Config gives priority to system properties and falls back to environment variables. It also
Expand All @@ -14,6 +18,7 @@
* <p>System properties are {@link DDTraceConfig#PREFIX}'ed. Environment variables are the same as
* the system property, but uppercased with '.' -> '_'.
*/
@Slf4j
public class DDTraceConfig extends Properties {
/** Config keys below */
private static final String PREFIX = "dd.";
Expand All @@ -23,12 +28,14 @@ public class DDTraceConfig extends Properties {
public static final String AGENT_HOST = "agent.host";
public static final String AGENT_PORT = "agent.port";
public static final String PRIORITY_SAMPLING = "priority.sampling";
public static final String SPAN_TAGS = "trace.span.tags";

private final String serviceName = getPropOrEnv(PREFIX + SERVICE_NAME);
private final String writerType = getPropOrEnv(PREFIX + WRITER_TYPE);
private final String agentHost = getPropOrEnv(PREFIX + AGENT_HOST);
private final String agentPort = getPropOrEnv(PREFIX + AGENT_PORT);
private final String prioritySampling = getPropOrEnv(PREFIX + PRIORITY_SAMPLING);
private final String spanTags = getPropOrEnv(PREFIX + SPAN_TAGS);

public DDTraceConfig() {
super();
Expand All @@ -45,6 +52,7 @@ public DDTraceConfig() {
setIfNotNull(AGENT_HOST, agentHost);
setIfNotNull(AGENT_PORT, agentPort);
setIfNotNull(PRIORITY_SAMPLING, prioritySampling);
setIfNotNull(SPAN_TAGS, spanTags);
}

public DDTraceConfig(final String serviceName) {
Expand All @@ -65,4 +73,23 @@ private String getPropOrEnv(final String name) {
static String propToEnvName(final String name) {
return name.toUpperCase().replace(".", "_");
}

public static Map<String, Object> parseMap(final String str) {
if (str == null || str.trim().isEmpty()) {
return Collections.emptyMap();
}
if (!str.matches("(([^,:]+:[^,:]+,)*([^,:]+:[^,:]+),?)?")) {
log.warn("Invalid config '{}'. Must match 'key1:value1,key2:value2'.", str);
return Collections.emptyMap();
}

final String[] tokens = str.split(",");
final Map<String, Object> map = Maps.newHashMapWithExpectedSize(tokens.length);

for (final String token : tokens) {
final String[] keyValue = token.split(":");
map.put(keyValue[0].trim(), keyValue[1].trim());
}
return Collections.unmodifiableMap(map);
}
}
Loading