Skip to content

Commit 2910574

Browse files
authored
Merge pull request #748 from DataDog/mar-kolya/cherry-pick-synthetics
Mar kolya/cherry pick synthetics
2 parents e6e4ade + 5c53f29 commit 2910574

File tree

17 files changed

+187
-210
lines changed

17 files changed

+187
-210
lines changed

dd-java-agent/instrumentation/instrumentation.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ subprojects { subProj ->
3636
classPath = project(':dd-java-agent:agent-tooling').configurations.instrumentationMuzzle + subProj.configurations.compile + subProj.sourceSets.main.output
3737
}
3838
}
39+
40+
// Make it so all instrumentation subproject tests can be run with a single command.
41+
instr_project.tasks.test.dependsOn(subProj.tasks.test)
3942
}
4043

4144
instr_project.dependencies {

dd-java-agent/testing/src/main/groovy/datadog/trace/agent/test/server/http/TestHttpServer.groovy

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ class TestHttpServer implements AutoCloseable {
4747
private TestHttpServer() {
4848
int port = PortUtils.randomOpenPort()
4949
internalServer = new Server(port)
50-
internalServer.stopAtShutdown = true
5150
address = new URI("http://localhost:$port")
5251
}
5352

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

dd-trace-ot/src/main/java/datadog/opentracing/DDSpanContext.java

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
public class DDSpanContext implements io.opentracing.SpanContext {
2525
public static final String PRIORITY_SAMPLING_KEY = "_sampling_priority_v1";
2626
public static final String SAMPLE_RATE_KEY = "_sample_rate";
27+
public static final String ORIGIN_KEY = "_dd.origin";
2728

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

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

@@ -77,6 +80,7 @@ public DDSpanContext(
7780
final String operationName,
7881
final String resourceName,
7982
final int samplingPriority,
83+
final String origin,
8084
final Map<String, String> baggageItems,
8185
final boolean errorFlag,
8286
final String spanType,
@@ -111,10 +115,20 @@ public DDSpanContext(
111115
this.resourceName = resourceName;
112116
this.errorFlag = errorFlag;
113117
this.spanType = spanType;
118+
this.origin = origin;
114119

115120
if (samplingPriority != PrioritySampling.UNSET) {
116121
setSamplingPriority(samplingPriority);
117122
}
123+
124+
if (spanType != null) {
125+
this.tags.put(DDTags.SPAN_TYPE, spanType);
126+
}
127+
if (origin != null) {
128+
this.tags.put(ORIGIN_KEY, origin);
129+
}
130+
this.tags.put(DDTags.THREAD_NAME, threadName);
131+
this.tags.put(DDTags.THREAD_ID, threadId);
118132
}
119133

120134
public String getTraceId() {
@@ -167,6 +181,11 @@ public String getSpanType() {
167181

168182
public void setSpanType(final String spanType) {
169183
this.spanType = spanType;
184+
if (spanType == null) {
185+
tags.remove(DDTags.SPAN_TYPE);
186+
} else {
187+
tags.put(DDTags.SPAN_TYPE, spanType);
188+
}
170189
}
171190

172191
public void setSamplingPriority(final int newPriority) {
@@ -236,6 +255,15 @@ public boolean lockSamplingPriority() {
236255
}
237256
}
238257

258+
public String getOrigin() {
259+
final DDSpan rootSpan = trace.getRootSpan();
260+
if (null != rootSpan) {
261+
return rootSpan.context().origin;
262+
} else {
263+
return origin;
264+
}
265+
}
266+
239267
public void setBaggageItem(final String key, final String value) {
240268
baggageItems.put(key, value);
241269
}
@@ -312,12 +340,6 @@ public synchronized void setTag(final String tag, final Object value) {
312340
}
313341

314342
public synchronized Map<String, Object> getTags() {
315-
tags.put(DDTags.THREAD_NAME, threadName);
316-
tags.put(DDTags.THREAD_ID, threadId);
317-
final String spanType = getSpanType();
318-
if (spanType != null) {
319-
tags.put(DDTags.SPAN_TYPE, spanType);
320-
}
321343
return Collections.unmodifiableMap(tags);
322344
}
323345

dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,7 @@ private DDSpanContext buildSpanContext() {
610610
final Map<String, String> baggage;
611611
final PendingTrace parentTrace;
612612
final int samplingPriority;
613+
final String origin;
613614

614615
final DDSpanContext context;
615616
SpanContext parentContext = parent;
@@ -629,6 +630,7 @@ private DDSpanContext buildSpanContext() {
629630
baggage = ddsc.getBaggageItems();
630631
parentTrace = ddsc.getTrace();
631632
samplingPriority = PrioritySampling.UNSET;
633+
origin = null;
632634
if (serviceName == null) {
633635
serviceName = ddsc.getServiceName();
634636
}
@@ -651,10 +653,15 @@ private DDSpanContext buildSpanContext() {
651653
samplingPriority = PrioritySampling.UNSET;
652654
baggage = null;
653655
}
654-
// Get header tags whether propagating or not.
656+
657+
// Get header tags and set origin whether propagating or not.
655658
if (parentContext instanceof TagContext) {
656659
tags.putAll(((TagContext) parentContext).getTags());
660+
origin = ((TagContext) parentContext).getOrigin();
661+
} else {
662+
origin = null;
657663
}
664+
658665
// add runtime tags to the root span
659666
for (final Map.Entry<String, String> runtimeTag : runtimeTags.entrySet()) {
660667
tags.put(runtimeTag.getKey(), runtimeTag.getValue());
@@ -679,6 +686,7 @@ private DDSpanContext buildSpanContext() {
679686
operationName,
680687
resourceName,
681688
samplingPriority,
689+
origin,
682690
baggage,
683691
errorFlag,
684692
spanType,

dd-trace-ot/src/main/java/datadog/opentracing/decorators/SpanTypeDecorator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ public class SpanTypeDecorator extends AbstractDecorator {
77

88
public SpanTypeDecorator() {
99
super();
10-
this.setMatchingTag(DDTags.SPAN_TYPE);
10+
setMatchingTag(DDTags.SPAN_TYPE);
1111
}
1212

1313
@Override
1414
public boolean shouldSetTag(final DDSpanContext context, final String tag, final Object value) {
1515
context.setSpanType(String.valueOf(value));
1616
// TODO: Do we really want a span type tag since it already exists on the span?
17-
return false;
17+
return true;
1818
}
1919
}

dd-trace-ot/src/main/java/datadog/opentracing/propagation/DatadogHttpCodec.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class DatadogHttpCodec {
2525
private static final String TRACE_ID_KEY = "x-datadog-trace-id";
2626
private static final String SPAN_ID_KEY = "x-datadog-parent-id";
2727
private static final String SAMPLING_PRIORITY_KEY = "x-datadog-sampling-priority";
28+
private static final String ORIGIN_KEY = "x-datadog-origin";
2829

2930
public static class Injector {
3031

@@ -34,6 +35,10 @@ public void inject(final DDSpanContext context, final TextMap carrier) {
3435
if (context.lockSamplingPriority()) {
3536
carrier.put(SAMPLING_PRIORITY_KEY, String.valueOf(context.getSamplingPriority()));
3637
}
38+
final String origin = context.getOrigin();
39+
if (origin != null) {
40+
carrier.put(ORIGIN_KEY, origin);
41+
}
3742

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

7379
for (final Map.Entry<String, String> entry : carrier) {
7480
final String key = entry.getKey().toLowerCase();
@@ -82,13 +88,15 @@ public SpanContext extract(final TextMap carrier) {
8288
traceId = validateUInt64BitsID(val);
8389
} else if (SPAN_ID_KEY.equalsIgnoreCase(key)) {
8490
spanId = validateUInt64BitsID(val);
91+
} else if (SAMPLING_PRIORITY_KEY.equalsIgnoreCase(key)) {
92+
samplingPriority = Integer.parseInt(val);
93+
} else if (ORIGIN_KEY.equalsIgnoreCase(key)) {
94+
origin = val;
8595
} else if (key.startsWith(OT_BAGGAGE_PREFIX)) {
8696
if (baggage.isEmpty()) {
8797
baggage = new HashMap<>();
8898
}
8999
baggage.put(key.replace(OT_BAGGAGE_PREFIX, ""), decode(val));
90-
} else if (SAMPLING_PRIORITY_KEY.equalsIgnoreCase(key)) {
91-
samplingPriority = Integer.parseInt(val);
92100
}
93101

94102
if (taggedHeaders.containsKey(key)) {
@@ -102,13 +110,13 @@ public SpanContext extract(final TextMap carrier) {
102110
SpanContext context = null;
103111
if (!"0".equals(traceId)) {
104112
final ExtractedContext ctx =
105-
new ExtractedContext(traceId, spanId, samplingPriority, baggage, tags);
113+
new ExtractedContext(traceId, spanId, samplingPriority, origin, baggage, tags);
106114
ctx.lockSamplingPriority();
107115

108116
log.debug("{} - Parent context extracted", ctx.getTraceId());
109117
context = ctx;
110-
} else if (!tags.isEmpty()) {
111-
context = new TagContext(tags);
118+
} else if (origin != null || !tags.isEmpty()) {
119+
context = new TagContext(origin, tags);
112120
}
113121

114122
return context;

dd-trace-ot/src/main/java/datadog/opentracing/propagation/ExtractedContext.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ public ExtractedContext(
1717
final String traceId,
1818
final String spanId,
1919
final int samplingPriority,
20+
final String origin,
2021
final Map<String, String> baggage,
2122
final Map<String, String> tags) {
22-
super(tags);
23+
super(origin, tags);
2324
this.traceId = traceId;
2425
this.spanId = spanId;
2526
this.samplingPriority = samplingPriority;

dd-trace-ot/src/main/java/datadog/opentracing/propagation/TagContext.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
* returned here even if the rest of the request would have returned null.
1010
*/
1111
public class TagContext implements SpanContext {
12+
private final String origin;
1213
private final Map<String, String> tags;
1314

14-
public TagContext(final Map<String, String> tags) {
15+
public TagContext(final String origin, final Map<String, String> tags) {
16+
this.origin = origin;
1517
this.tags = tags;
1618
}
1719

20+
public String getOrigin() {
21+
return origin;
22+
}
23+
1824
public Map<String, String> getTags() {
1925
return tags;
2026
}

dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanBuilderTest.groovy

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package datadog.opentracing
22

33
import datadog.opentracing.propagation.ExtractedContext
4+
import datadog.opentracing.propagation.TagContext
45
import datadog.trace.api.Config
56
import datadog.trace.api.DDTags
7+
import datadog.trace.api.sampling.PrioritySampling
68
import datadog.trace.common.writer.ListWriter
79
import spock.lang.Specification
810

11+
import static datadog.opentracing.DDSpanContext.ORIGIN_KEY
912
import static java.util.concurrent.TimeUnit.MILLISECONDS
1013
import static org.mockito.Mockito.mock
1114
import static org.mockito.Mockito.when
@@ -382,21 +385,45 @@ class DDSpanBuilderTest extends Specification {
382385

383386
def "ExtractedContext should populate new span details"() {
384387
setup:
388+
def thread = Thread.currentThread()
385389
final DDSpan span = tracer.buildSpan("op name")
386390
.asChildOf(extractedContext).start()
387391

388392
expect:
389393
span.traceId == extractedContext.traceId
390394
span.parentId == extractedContext.spanId
391395
span.samplingPriority == extractedContext.samplingPriority
396+
span.context().origin == extractedContext.origin
392397
span.context().baggageItems == extractedContext.baggage
393398
span.context().@tags == extractedContext.tags + [(Config.RUNTIME_ID_TAG) : config.getRuntimeId(),
394-
(Config.LANGUAGE_TAG_KEY): Config.LANGUAGE_TAG_VALUE,]
399+
(Config.LANGUAGE_TAG_KEY): Config.LANGUAGE_TAG_VALUE,
400+
(DDTags.THREAD_NAME) : thread.name, (DDTags.THREAD_ID): thread.id]
395401

396402
where:
397-
extractedContext | _
398-
new ExtractedContext("1", "2", 0, [:], [:]) | _
399-
new ExtractedContext("3", "4", 1, ["asdf": "qwer"], ["zxcv": "1234"]) | _
403+
extractedContext | _
404+
new ExtractedContext("1", "2", 0, null, [:], [:]) | _
405+
new ExtractedContext("3", "4", 1, "some-origin", ["asdf": "qwer"], [(ORIGIN_KEY): "some-origin", "zxcv": "1234"]) | _
406+
}
407+
408+
def "TagContext should populate default span details"() {
409+
setup:
410+
def thread = Thread.currentThread()
411+
final DDSpan span = tracer.buildSpan("op name").asChildOf(tagContext).start()
412+
413+
expect:
414+
span.traceId != "0"
415+
span.parentId == "0"
416+
span.samplingPriority == PrioritySampling.SAMPLER_KEEP // Since we're using the RateByServiceSampler
417+
span.context().origin == tagContext.origin
418+
span.context().baggageItems == [:]
419+
span.context().@tags == tagContext.tags + [(Config.RUNTIME_ID_TAG) : config.getRuntimeId(),
420+
(Config.LANGUAGE_TAG_KEY): Config.LANGUAGE_TAG_VALUE,
421+
(DDTags.THREAD_NAME) : thread.name, (DDTags.THREAD_ID): thread.id]
422+
423+
where:
424+
tagContext | _
425+
new TagContext(null, [:]) | _
426+
new TagContext("some-origin", [(ORIGIN_KEY): "some-origin", "asdf": "qwer"]) | _
400427
}
401428

402429
def "global span tags populated on each span"() {

dd-trace-ot/src/test/groovy/datadog/opentracing/DDSpanSerializationTest.groovy

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class DDSpanSerializationTest extends Specification {
5050
"operation",
5151
null,
5252
samplingPriority,
53+
null,
5354
new HashMap<>(baggage),
5455
false,
5556
"type",
@@ -90,6 +91,7 @@ class DDSpanSerializationTest extends Specification {
9091
"fakeOperation",
9192
"fakeResource",
9293
PrioritySampling.UNSET,
94+
null,
9395
Collections.emptyMap(),
9496
false,
9597
"fakeType",
@@ -117,10 +119,10 @@ class DDSpanSerializationTest extends Specification {
117119
}
118120

119121
where:
120-
value | _
121-
BigInteger.ZERO | _
122-
BigInteger.ONE | _
122+
value | _
123+
BigInteger.ZERO | _
124+
BigInteger.ONE | _
123125
BigInteger.valueOf(Long.MAX_VALUE).subtract(BigInteger.ONE) | _
124-
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE) | _
126+
BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE) | _
125127
}
126128
}

0 commit comments

Comments
 (0)