Skip to content

Commit

Permalink
Merge pull request #748 from DataDog/mar-kolya/cherry-pick-synthetics
Browse files Browse the repository at this point in the history
Mar kolya/cherry pick synthetics
  • Loading branch information
mar-kolya authored Mar 1, 2019
2 parents e6e4ade + 5c53f29 commit 2910574
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 210 deletions.
3 changes: 3 additions & 0 deletions dd-java-agent/instrumentation/instrumentation.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ subprojects { subProj ->
classPath = project(':dd-java-agent:agent-tooling').configurations.instrumentationMuzzle + subProj.configurations.compile + subProj.sourceSets.main.output
}
}

// Make it so all instrumentation subproject tests can be run with a single command.
instr_project.tasks.test.dependsOn(subProj.tasks.test)
}

instr_project.dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ class TestHttpServer implements AutoCloseable {
private TestHttpServer() {
int port = PortUtils.randomOpenPort()
internalServer = new Server(port)
internalServer.stopAtShutdown = true
address = new URI("http://localhost:$port")
}

Expand All @@ -62,6 +61,8 @@ class TestHttpServer implements AutoCloseable {
internalServer.handler = handlerList
System.out.println("Starting server $this on port $address.port")
internalServer.start()
// set after starting, otherwise two callbacks get added.
internalServer.stopAtShutdown = true
return this
}

Expand Down
34 changes: 28 additions & 6 deletions dd-trace-ot/src/main/java/datadog/opentracing/DDSpanContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
public class DDSpanContext implements io.opentracing.SpanContext {
public static final String PRIORITY_SAMPLING_KEY = "_sampling_priority_v1";
public static final String SAMPLE_RATE_KEY = "_sample_rate";
public static final String ORIGIN_KEY = "_dd.origin";

private static final Map<String, Number> EMPTY_METRICS = Collections.emptyMap();

Expand Down Expand Up @@ -62,6 +63,8 @@ public class DDSpanContext implements io.opentracing.SpanContext {
* <p>For thread safety, this boolean is only modified or accessed under instance lock.
*/
private boolean samplingPriorityLocked = false;
/** The origin of the trace. (eg. Synthetics) */
private final String origin;
/** Metrics on the span */
private final AtomicReference<Map<String, Number>> metrics = new AtomicReference<>();

Expand All @@ -77,6 +80,7 @@ public DDSpanContext(
final String operationName,
final String resourceName,
final int samplingPriority,
final String origin,
final Map<String, String> baggageItems,
final boolean errorFlag,
final String spanType,
Expand Down Expand Up @@ -111,10 +115,20 @@ public DDSpanContext(
this.resourceName = resourceName;
this.errorFlag = errorFlag;
this.spanType = spanType;
this.origin = origin;

if (samplingPriority != PrioritySampling.UNSET) {
setSamplingPriority(samplingPriority);
}

if (spanType != null) {
this.tags.put(DDTags.SPAN_TYPE, spanType);
}
if (origin != null) {
this.tags.put(ORIGIN_KEY, origin);
}
this.tags.put(DDTags.THREAD_NAME, threadName);
this.tags.put(DDTags.THREAD_ID, threadId);
}

public String getTraceId() {
Expand Down Expand Up @@ -167,6 +181,11 @@ public String getSpanType() {

public void setSpanType(final String spanType) {
this.spanType = spanType;
if (spanType == null) {
tags.remove(DDTags.SPAN_TYPE);
} else {
tags.put(DDTags.SPAN_TYPE, spanType);
}
}

public void setSamplingPriority(final int newPriority) {
Expand Down Expand Up @@ -236,6 +255,15 @@ public boolean lockSamplingPriority() {
}
}

public String getOrigin() {
final DDSpan rootSpan = trace.getRootSpan();
if (null != rootSpan) {
return rootSpan.context().origin;
} else {
return origin;
}
}

public void setBaggageItem(final String key, final String value) {
baggageItems.put(key, value);
}
Expand Down Expand Up @@ -312,12 +340,6 @@ public synchronized void setTag(final String tag, final Object value) {
}

public synchronized Map<String, Object> getTags() {
tags.put(DDTags.THREAD_NAME, threadName);
tags.put(DDTags.THREAD_ID, threadId);
final String spanType = getSpanType();
if (spanType != null) {
tags.put(DDTags.SPAN_TYPE, spanType);
}
return Collections.unmodifiableMap(tags);
}

Expand Down
10 changes: 9 additions & 1 deletion dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,7 @@ private DDSpanContext buildSpanContext() {
final Map<String, String> baggage;
final PendingTrace parentTrace;
final int samplingPriority;
final String origin;

final DDSpanContext context;
SpanContext parentContext = parent;
Expand All @@ -629,6 +630,7 @@ private DDSpanContext buildSpanContext() {
baggage = ddsc.getBaggageItems();
parentTrace = ddsc.getTrace();
samplingPriority = PrioritySampling.UNSET;
origin = null;
if (serviceName == null) {
serviceName = ddsc.getServiceName();
}
Expand All @@ -651,10 +653,15 @@ private DDSpanContext buildSpanContext() {
samplingPriority = PrioritySampling.UNSET;
baggage = null;
}
// Get header tags whether propagating or not.

// Get header tags and set origin whether propagating or not.
if (parentContext instanceof TagContext) {
tags.putAll(((TagContext) parentContext).getTags());
origin = ((TagContext) parentContext).getOrigin();
} else {
origin = null;
}

// add runtime tags to the root span
for (final Map.Entry<String, String> runtimeTag : runtimeTags.entrySet()) {
tags.put(runtimeTag.getKey(), runtimeTag.getValue());
Expand All @@ -679,6 +686,7 @@ private DDSpanContext buildSpanContext() {
operationName,
resourceName,
samplingPriority,
origin,
baggage,
errorFlag,
spanType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ public class SpanTypeDecorator extends AbstractDecorator {

public SpanTypeDecorator() {
super();
this.setMatchingTag(DDTags.SPAN_TYPE);
setMatchingTag(DDTags.SPAN_TYPE);
}

@Override
public boolean shouldSetTag(final DDSpanContext context, final String tag, final Object value) {
context.setSpanType(String.valueOf(value));
// TODO: Do we really want a span type tag since it already exists on the span?
return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class DatadogHttpCodec {
private static final String TRACE_ID_KEY = "x-datadog-trace-id";
private static final String SPAN_ID_KEY = "x-datadog-parent-id";
private static final String SAMPLING_PRIORITY_KEY = "x-datadog-sampling-priority";
private static final String ORIGIN_KEY = "x-datadog-origin";

public static class Injector {

Expand All @@ -34,6 +35,10 @@ public void inject(final DDSpanContext context, final TextMap carrier) {
if (context.lockSamplingPriority()) {
carrier.put(SAMPLING_PRIORITY_KEY, String.valueOf(context.getSamplingPriority()));
}
final String origin = context.getOrigin();
if (origin != null) {
carrier.put(ORIGIN_KEY, origin);
}

for (final Map.Entry<String, String> entry : context.baggageItems()) {
carrier.put(OT_BAGGAGE_PREFIX + entry.getKey(), encode(entry.getValue()));
Expand Down Expand Up @@ -69,6 +74,7 @@ public SpanContext extract(final TextMap carrier) {
String traceId = "0";
String spanId = "0";
int samplingPriority = PrioritySampling.UNSET;
String origin = null;

for (final Map.Entry<String, String> entry : carrier) {
final String key = entry.getKey().toLowerCase();
Expand All @@ -82,13 +88,15 @@ public SpanContext extract(final TextMap carrier) {
traceId = validateUInt64BitsID(val);
} else if (SPAN_ID_KEY.equalsIgnoreCase(key)) {
spanId = validateUInt64BitsID(val);
} else if (SAMPLING_PRIORITY_KEY.equalsIgnoreCase(key)) {
samplingPriority = Integer.parseInt(val);
} else if (ORIGIN_KEY.equalsIgnoreCase(key)) {
origin = val;
} else if (key.startsWith(OT_BAGGAGE_PREFIX)) {
if (baggage.isEmpty()) {
baggage = new HashMap<>();
}
baggage.put(key.replace(OT_BAGGAGE_PREFIX, ""), decode(val));
} else if (SAMPLING_PRIORITY_KEY.equalsIgnoreCase(key)) {
samplingPriority = Integer.parseInt(val);
}

if (taggedHeaders.containsKey(key)) {
Expand All @@ -102,13 +110,13 @@ public SpanContext extract(final TextMap carrier) {
SpanContext context = null;
if (!"0".equals(traceId)) {
final ExtractedContext ctx =
new ExtractedContext(traceId, spanId, samplingPriority, baggage, tags);
new ExtractedContext(traceId, spanId, samplingPriority, origin, baggage, tags);
ctx.lockSamplingPriority();

log.debug("{} - Parent context extracted", ctx.getTraceId());
context = ctx;
} else if (!tags.isEmpty()) {
context = new TagContext(tags);
} else if (origin != null || !tags.isEmpty()) {
context = new TagContext(origin, tags);
}

return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public ExtractedContext(
final String traceId,
final String spanId,
final int samplingPriority,
final String origin,
final Map<String, String> baggage,
final Map<String, String> tags) {
super(tags);
super(origin, tags);
this.traceId = traceId;
this.spanId = spanId;
this.samplingPriority = samplingPriority;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@
* returned here even if the rest of the request would have returned null.
*/
public class TagContext implements SpanContext {
private final String origin;
private final Map<String, String> tags;

public TagContext(final Map<String, String> tags) {
public TagContext(final String origin, final Map<String, String> tags) {
this.origin = origin;
this.tags = tags;
}

public String getOrigin() {
return origin;
}

public Map<String, String> getTags() {
return tags;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package datadog.opentracing

import datadog.opentracing.propagation.ExtractedContext
import datadog.opentracing.propagation.TagContext
import datadog.trace.api.Config
import datadog.trace.api.DDTags
import datadog.trace.api.sampling.PrioritySampling
import datadog.trace.common.writer.ListWriter
import spock.lang.Specification

import static datadog.opentracing.DDSpanContext.ORIGIN_KEY
import static java.util.concurrent.TimeUnit.MILLISECONDS
import static org.mockito.Mockito.mock
import static org.mockito.Mockito.when
Expand Down Expand Up @@ -382,21 +385,45 @@ class DDSpanBuilderTest extends Specification {

def "ExtractedContext should populate new span details"() {
setup:
def thread = Thread.currentThread()
final DDSpan span = tracer.buildSpan("op name")
.asChildOf(extractedContext).start()

expect:
span.traceId == extractedContext.traceId
span.parentId == extractedContext.spanId
span.samplingPriority == extractedContext.samplingPriority
span.context().origin == extractedContext.origin
span.context().baggageItems == extractedContext.baggage
span.context().@tags == extractedContext.tags + [(Config.RUNTIME_ID_TAG) : config.getRuntimeId(),
(Config.LANGUAGE_TAG_KEY): Config.LANGUAGE_TAG_VALUE,]
(Config.LANGUAGE_TAG_KEY): Config.LANGUAGE_TAG_VALUE,
(DDTags.THREAD_NAME) : thread.name, (DDTags.THREAD_ID): thread.id]

where:
extractedContext | _
new ExtractedContext("1", "2", 0, [:], [:]) | _
new ExtractedContext("3", "4", 1, ["asdf": "qwer"], ["zxcv": "1234"]) | _
extractedContext | _
new ExtractedContext("1", "2", 0, null, [:], [:]) | _
new ExtractedContext("3", "4", 1, "some-origin", ["asdf": "qwer"], [(ORIGIN_KEY): "some-origin", "zxcv": "1234"]) | _
}

def "TagContext should populate default span details"() {
setup:
def thread = Thread.currentThread()
final DDSpan span = tracer.buildSpan("op name").asChildOf(tagContext).start()

expect:
span.traceId != "0"
span.parentId == "0"
span.samplingPriority == PrioritySampling.SAMPLER_KEEP // Since we're using the RateByServiceSampler
span.context().origin == tagContext.origin
span.context().baggageItems == [:]
span.context().@tags == tagContext.tags + [(Config.RUNTIME_ID_TAG) : config.getRuntimeId(),
(Config.LANGUAGE_TAG_KEY): Config.LANGUAGE_TAG_VALUE,
(DDTags.THREAD_NAME) : thread.name, (DDTags.THREAD_ID): thread.id]

where:
tagContext | _
new TagContext(null, [:]) | _
new TagContext("some-origin", [(ORIGIN_KEY): "some-origin", "asdf": "qwer"]) | _
}

def "global span tags populated on each span"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class DDSpanSerializationTest extends Specification {
"operation",
null,
samplingPriority,
null,
new HashMap<>(baggage),
false,
"type",
Expand Down Expand Up @@ -90,6 +91,7 @@ class DDSpanSerializationTest extends Specification {
"fakeOperation",
"fakeResource",
PrioritySampling.UNSET,
null,
Collections.emptyMap(),
false,
"fakeType",
Expand Down Expand Up @@ -117,10 +119,10 @@ class DDSpanSerializationTest extends Specification {
}

where:
value | _
BigInteger.ZERO | _
BigInteger.ONE | _
value | _
BigInteger.ZERO | _
BigInteger.ONE | _
BigInteger.valueOf(Long.MAX_VALUE).subtract(BigInteger.ONE) | _
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE) | _
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE) | _
}
}
Loading

0 comments on commit 2910574

Please sign in to comment.