Skip to content

Commit 10628b0

Browse files
committed
Reorganizing link & parent resolution
Previous static extraction caused a test to fail because links logic relied on having a side effect on the builder instance's List<AgentSpanLink>
1 parent 8f50da6 commit 10628b0

File tree

1 file changed

+113
-112
lines changed

1 file changed

+113
-112
lines changed

dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java

Lines changed: 113 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,37 +1083,29 @@ static final ReusableSingleSpanBuilder reuseSingleSpanBuilder(
10831083

10841084
@Override
10851085
public AgentSpan startSpan(final String instrumentationName, final CharSequence spanName) {
1086-
return singleSpanBuilder(instrumentationName, spanName).start();
1086+
return CoreSpanBuilder.startSpan(this, instrumentationName, spanName, null, false, CoreSpanBuilder.DEFAULT_TIMESTAMP_MICRO);
10871087
}
10881088

10891089
@Override
10901090
public AgentSpan startSpan(
10911091
final String instrumentationName, final CharSequence spanName, final long startTimeMicros) {
1092-
return singleSpanBuilder(instrumentationName, spanName)
1093-
.withStartTimestamp(startTimeMicros)
1094-
.start();
1092+
return CoreSpanBuilder.startSpan(this, instrumentationName, spanName, null, false, startTimeMicros);
10951093
}
10961094

10971095
@Override
10981096
public AgentSpan startSpan(
10991097
String instrumentationName, final CharSequence spanName, final AgentSpanContext parent) {
1100-
return singleSpanBuilder(instrumentationName, spanName)
1101-
.ignoreActiveSpan()
1102-
.asChildOf(parent)
1103-
.start();
1098+
return CoreSpanBuilder.startSpan(this, instrumentationName, spanName, parent, true, CoreSpanBuilder.DEFAULT_TIMESTAMP_MICRO);
11041099
}
11051100

11061101
@Override
11071102
public AgentSpan startSpan(
11081103
final String instrumentationName,
11091104
final CharSequence spanName,
11101105
final AgentSpanContext parent,
1111-
final long startTimeMicros) {
1112-
return singleSpanBuilder(instrumentationName, spanName)
1113-
.ignoreActiveSpan()
1114-
.asChildOf(parent)
1115-
.withStartTimestamp(startTimeMicros)
1116-
.start();
1106+
final long startTimeMicros)
1107+
{
1108+
return CoreSpanBuilder.startSpan(this, instrumentationName, spanName, parent, true, startTimeMicros);
11171109
}
11181110

11191111
@Override
@@ -1521,6 +1513,10 @@ private static <K, V> Map<V, K> invertMap(Map<K, V> map) {
15211513

15221514
/** Spans are built using this builder */
15231515
public abstract static class CoreSpanBuilder implements AgentTracer.SpanBuilder {
1516+
protected static final boolean IGNORE_SCOPE_DEFAULT = false;
1517+
protected static final int SPAN_ID_DEFAULT = 0;
1518+
protected static final long DEFAULT_TIMESTAMP_MICRO = 0L;
1519+
15241520
protected final CoreTracer tracer;
15251521

15261522
protected String instrumentationName;
@@ -1535,12 +1531,12 @@ public abstract static class CoreSpanBuilder implements AgentTracer.SpanBuilder
15351531
protected String resourceName;
15361532
protected boolean errorFlag;
15371533
protected CharSequence spanType;
1538-
protected boolean ignoreScope = false;
1534+
protected boolean ignoreScope = IGNORE_SCOPE_DEFAULT;
15391535
protected Object builderRequestContextDataAppSec;
15401536
protected Object builderRequestContextDataIast;
15411537
protected Object builderCiVisibilityContextData;
15421538
protected List<AgentSpanLink> links;
1543-
protected long spanId;
1539+
protected long spanId = SPAN_ID_DEFAULT;
15441540
// Make sure any fields added here are also reset properly in ReusableSingleSpanBuilder.reset
15451541

15461542
CoreSpanBuilder(CoreTracer tracer) {
@@ -1553,11 +1549,6 @@ public final CoreSpanBuilder ignoreActiveSpan() {
15531549
return this;
15541550
}
15551551

1556-
@Deprecated
1557-
protected final DDSpan buildSpan() {
1558-
return buildSpan(tracer, instrumentationName, timestampMicro, links, buildSpanContext());
1559-
}
1560-
15611552
protected static final DDSpan buildSpan(
15621553
final CoreTracer tracer,
15631554
long spanId,
@@ -1566,7 +1557,7 @@ protected static final DDSpan buildSpan(
15661557
String serviceName,
15671558
CharSequence operationName,
15681559
String resourceName,
1569-
AgentSpanContext incomingParentContext,
1560+
AgentSpanContext resolvedParentContext,
15701561
boolean ignoreScope,
15711562
boolean errorFlag,
15721563
CharSequence spanType,
@@ -1575,7 +1566,7 @@ protected static final DDSpan buildSpan(
15751566
Object builderRequestContextDataAppSec,
15761567
Object builderRequestContextDataIast,
15771568
Object builderCiVisibilityContextData) {
1578-
return buildSpan(
1569+
return buildSpanImpl(
15791570
tracer,
15801571
instrumentationName,
15811572
timestampMicro,
@@ -1586,8 +1577,7 @@ protected static final DDSpan buildSpan(
15861577
serviceName,
15871578
operationName,
15881579
resourceName,
1589-
incomingParentContext,
1590-
ignoreScope,
1580+
resolvedParentContext,
15911581
errorFlag,
15921582
spanType,
15931583
tagLedger,
@@ -1597,7 +1587,7 @@ protected static final DDSpan buildSpan(
15971587
builderCiVisibilityContextData));
15981588
}
15991589

1600-
protected static final DDSpan buildSpan(
1590+
protected static final DDSpan buildSpanImpl(
16011591
CoreTracer tracer,
16021592
String instrumentationName,
16031593
long timestampMicro,
@@ -1663,19 +1653,52 @@ protected static final List<AgentSpanLink> addLinks(
16631653
public abstract AgentSpan start();
16641654

16651655
protected AgentSpan startImpl() {
1666-
AgentSpanContext pc = parent;
1667-
if (pc == null && !ignoreScope) {
1668-
final AgentSpan span = tracer.activeSpan();
1669-
if (span != null) {
1670-
pc = span.context();
1671-
}
1672-
}
1673-
1674-
if (pc == BlackHoleSpan.Context.INSTANCE) {
1675-
return new BlackHoleSpan(pc.getTraceId());
1676-
}
1677-
return buildSpan();
1656+
return startSpan(
1657+
this.tracer,
1658+
this.spanId,
1659+
this.instrumentationName,
1660+
this.timestampMicro,
1661+
this.serviceName,
1662+
this.operationName,
1663+
this.resourceName,
1664+
this.parent,
1665+
this.ignoreScope,
1666+
this.errorFlag,
1667+
this.spanType,
1668+
this.tagLedger,
1669+
this.links,
1670+
this.builderRequestContextDataAppSec,
1671+
this.builderRequestContextDataIast,
1672+
this.builderCiVisibilityContextData);
1673+
}
1674+
1675+
protected static final AgentSpan startSpan(
1676+
final CoreTracer tracer,
1677+
String instrumentationName,
1678+
CharSequence operationName,
1679+
AgentSpanContext specifiedParentContext,
1680+
boolean ignoreScope,
1681+
long timestampMicros)
1682+
{
1683+
return startSpan(
1684+
tracer,
1685+
SPAN_ID_DEFAULT,
1686+
instrumentationName,
1687+
timestampMicros,
1688+
null /* serviceName */,
1689+
operationName,
1690+
null /* resourceName */,
1691+
specifiedParentContext,
1692+
ignoreScope,
1693+
false /* errorFlag */,
1694+
null /* spanType */,
1695+
null /* tagLedger */,
1696+
null /* links */,
1697+
null /* appSec */,
1698+
null /* iast */,
1699+
null /* ciViz */);
16781700
}
1701+
16791702

16801703
protected static final AgentSpan startSpan(
16811704
final CoreTracer tracer,
@@ -1685,26 +1708,49 @@ protected static final AgentSpan startSpan(
16851708
String serviceName,
16861709
CharSequence operationName,
16871710
String resourceName,
1688-
AgentSpanContext incomingParentContext,
1711+
AgentSpanContext specifiedParentContext,
16891712
boolean ignoreScope,
16901713
boolean errorFlag,
16911714
CharSequence spanType,
16921715
TagMap.Ledger tagLedger,
16931716
List<AgentSpanLink> links,
16941717
Object builderRequestContextDataAppSec,
16951718
Object builderRequestContextDataIast,
1696-
Object builderCiVisibilityContextData) {
1697-
AgentSpanContext pc = incomingParentContext;
1698-
if (pc == null && !ignoreScope) {
1699-
final AgentSpan span = tracer.activeSpan();
1700-
if (span != null) {
1701-
pc = span.context();
1719+
Object builderCiVisibilityContextData)
1720+
{
1721+
// Find the parent context
1722+
AgentSpanContext parentContext = specifiedParentContext;
1723+
if (parentContext == null && !ignoreScope) {
1724+
// use the Scope as parent unless overridden or ignored.
1725+
final AgentSpan activeSpan = tracer.scopeManager.activeSpan();
1726+
if (activeSpan != null) {
1727+
parentContext = activeSpan.context();
1728+
}
1729+
}
1730+
1731+
if (parentContext == BlackHoleSpan.Context.INSTANCE) {
1732+
return new BlackHoleSpan(parentContext.getTraceId());
1733+
}
1734+
1735+
// Handle remote terminated context as span links
1736+
if (parentContext != null && parentContext.isRemote()) {
1737+
switch (Config.get().getTracePropagationBehaviorExtract()) {
1738+
case RESTART:
1739+
links = addParentContextLink(links, parentContext);
1740+
parentContext = null;
1741+
break;
1742+
1743+
case IGNORE:
1744+
parentContext = null;
1745+
break;
1746+
1747+
case CONTINUE:
1748+
default:
1749+
links = addTerminatedContextAsLinks(links, specifiedParentContext);
1750+
break;
17021751
}
17031752
}
1704-
1705-
if (pc == BlackHoleSpan.Context.INSTANCE) {
1706-
return new BlackHoleSpan(pc.getTraceId());
1707-
}
1753+
17081754
return buildSpan(
17091755
tracer,
17101756
spanId,
@@ -1713,7 +1759,7 @@ protected static final AgentSpan startSpan(
17131759
serviceName,
17141760
operationName,
17151761
resourceName,
1716-
incomingParentContext,
1762+
parentContext,
17171763
ignoreScope,
17181764
errorFlag,
17191765
spanType,
@@ -1846,32 +1892,13 @@ public final CoreSpanBuilder withSpanId(final long spanId) {
18461892
*
18471893
* @return the context
18481894
*/
1849-
private final DDSpanContext buildSpanContext() {
1850-
return this.buildSpanContext(
1851-
this.tracer,
1852-
this.spanId,
1853-
this.serviceName,
1854-
this.operationName,
1855-
this.resourceName,
1856-
this.parent,
1857-
this.ignoreScope,
1858-
this.errorFlag,
1859-
this.spanType,
1860-
this.tagLedger,
1861-
this.links,
1862-
this.builderRequestContextDataAppSec,
1863-
this.builderRequestContextDataIast,
1864-
this.builderCiVisibilityContextData);
1865-
}
1866-
18671895
protected static final DDSpanContext buildSpanContext(
18681896
final CoreTracer tracer,
18691897
long spanId,
18701898
String serviceName,
18711899
CharSequence operationName,
18721900
String resourceName,
1873-
AgentSpanContext incomingParentContext,
1874-
boolean ignoreScope,
1901+
AgentSpanContext resolvedParentContext,
18751902
boolean errorFlag,
18761903
CharSequence spanType,
18771904
TagMap.Ledger tagLedger,
@@ -1901,38 +1928,12 @@ protected static final DDSpanContext buildSpanContext(
19011928
spanId = tracer.idGenerationStrategy.generateSpanId();
19021929
}
19031930

1904-
// Find the parent context
1905-
AgentSpanContext parentContext = incomingParentContext;
1906-
if (parentContext == null && !ignoreScope) {
1907-
// use the Scope as parent unless overridden or ignored.
1908-
final AgentSpan activeSpan = tracer.scopeManager.activeSpan();
1909-
if (activeSpan != null) {
1910-
parentContext = activeSpan.context();
1911-
}
1912-
}
1913-
1914-
// Handle remote terminated context as span links
1915-
if (parentContext != null && parentContext.isRemote()) {
1916-
switch (Config.get().getTracePropagationBehaviorExtract()) {
1917-
case RESTART:
1918-
links = addParentContextLink(links, parentContext);
1919-
parentContext = null;
1920-
break;
1921-
case IGNORE:
1922-
parentContext = null;
1923-
break;
1924-
case CONTINUE:
1925-
default:
1926-
links = addTerminatedContextAsLinks(links, incomingParentContext);
1927-
}
1928-
}
1929-
19301931
String parentServiceName = null;
19311932
// Propagate internal trace.
19321933
// Note: if we are not in the context of distributed tracing, and we are starting the first
19331934
// root span, parentContext will be null at this point.
1934-
if (parentContext instanceof DDSpanContext) {
1935-
final DDSpanContext ddsc = (DDSpanContext) parentContext;
1935+
if (resolvedParentContext instanceof DDSpanContext) {
1936+
final DDSpanContext ddsc = (DDSpanContext) resolvedParentContext;
19361937
traceId = ddsc.getTraceId();
19371938
parentSpanId = ddsc.getSpanId();
19381939
baggage = ddsc.getBaggageItems();
@@ -1948,7 +1949,7 @@ protected static final DDSpanContext buildSpanContext(
19481949
if (serviceName == null) {
19491950
serviceName = parentServiceName;
19501951
}
1951-
RequestContext requestContext = ((DDSpanContext) parentContext).getRequestContext();
1952+
RequestContext requestContext = ((DDSpanContext) resolvedParentContext).getRequestContext();
19521953
if (requestContext != null) {
19531954
requestContextDataAppSec = requestContext.getData(RequestContextSlot.APPSEC);
19541955
requestContextDataIast = requestContext.getData(RequestContextSlot.IAST);
@@ -1962,21 +1963,21 @@ protected static final DDSpanContext buildSpanContext(
19621963
} else {
19631964
long endToEndStartTime;
19641965

1965-
if (parentContext instanceof ExtractedContext) {
1966+
if (resolvedParentContext instanceof ExtractedContext) {
19661967
// Propagate external trace
1967-
final ExtractedContext extractedContext = (ExtractedContext) parentContext;
1968+
final ExtractedContext extractedContext = (ExtractedContext) resolvedParentContext;
19681969
traceId = extractedContext.getTraceId();
19691970
parentSpanId = extractedContext.getSpanId();
19701971
samplingPriority = extractedContext.getSamplingPriority();
19711972
endToEndStartTime = extractedContext.getEndToEndStartTime();
19721973
propagationTags = extractedContext.getPropagationTags();
1973-
} else if (parentContext != null) {
1974+
} else if (resolvedParentContext != null) {
19741975
traceId =
1975-
parentContext.getTraceId() == DDTraceId.ZERO
1976+
resolvedParentContext.getTraceId() == DDTraceId.ZERO
19761977
? tracer.idGenerationStrategy.generateTraceId()
1977-
: parentContext.getTraceId();
1978-
parentSpanId = parentContext.getSpanId();
1979-
samplingPriority = parentContext.getSamplingPriority();
1978+
: resolvedParentContext.getTraceId();
1979+
parentSpanId = resolvedParentContext.getSpanId();
1980+
samplingPriority = resolvedParentContext.getSamplingPriority();
19801981
endToEndStartTime = 0;
19811982
propagationTags = tracer.propagationTagsFactory.empty();
19821983
} else {
@@ -1991,8 +1992,8 @@ protected static final DDSpanContext buildSpanContext(
19911992
ConfigSnapshot traceConfig;
19921993

19931994
// Get header tags and set origin whether propagating or not.
1994-
if (parentContext instanceof TagContext) {
1995-
TagContext tc = (TagContext) parentContext;
1995+
if (resolvedParentContext instanceof TagContext) {
1996+
TagContext tc = (TagContext) resolvedParentContext;
19961997
traceConfig = (ConfigSnapshot) tc.getTraceConfig();
19971998
coreTags = tc.getTags();
19981999
coreTagsNeedsIntercept = true; // maybe intercept isn't needed?
@@ -2028,10 +2029,10 @@ protected static final DDSpanContext buildSpanContext(
20282029

20292030
// Use parent pathwayContext if present and started
20302031
pathwayContext =
2031-
parentContext != null
2032-
&& parentContext.getPathwayContext() != null
2033-
&& parentContext.getPathwayContext().isStarted()
2034-
? parentContext.getPathwayContext()
2032+
resolvedParentContext != null
2033+
&& resolvedParentContext.getPathwayContext() != null
2034+
&& resolvedParentContext.getPathwayContext().isStarted()
2035+
? resolvedParentContext.getPathwayContext()
20352036
: tracer.dataStreamsMonitoring.newPathwayContext();
20362037

20372038
// when removing fake services the best upward service name to pick is the local root one

0 commit comments

Comments
 (0)