Skip to content

Commit c60e9be

Browse files
committed
Extract static version of buildSpanContext
1 parent ccdfd7b commit c60e9be

File tree

1 file changed

+75
-21
lines changed

1 file changed

+75
-21
lines changed

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

Lines changed: 75 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1574,7 +1574,9 @@ protected static final DDSpan buildSpan(
15741574
return span;
15751575
}
15761576

1577-
private final void addParentContextAsLinks(AgentSpanContext parentContext) {
1577+
private static final List<AgentSpanLink> addParentContextLink(
1578+
List<AgentSpanLink> links,
1579+
AgentSpanContext parentContext) {
15781580
SpanLink link;
15791581
if (parentContext instanceof ExtractedContext) {
15801582
String headers = ((ExtractedContext) parentContext).getPropagationStyle().toString();
@@ -1587,20 +1589,42 @@ private final void addParentContextAsLinks(AgentSpanContext parentContext) {
15871589
} else {
15881590
link = SpanLink.from(parentContext);
15891591
}
1590-
withLink(link);
1592+
return addLink(links, link);
15911593
}
15921594

1593-
private final void addTerminatedContextAsLinks() {
1594-
if (this.parent instanceof TagContext) {
1595+
protected static final List<AgentSpanLink> addTerminatedContextAsLinks(
1596+
List<AgentSpanLink> links,
1597+
AgentSpanContext parentContext)
1598+
{
1599+
if (parentContext instanceof TagContext) {
15951600
List<AgentSpanLink> terminatedContextLinks =
1596-
((TagContext) this.parent).getTerminatedContextLinks();
1601+
((TagContext) parentContext).getTerminatedContextLinks();
15971602
if (!terminatedContextLinks.isEmpty()) {
1598-
if (this.links == null) {
1599-
this.links = new ArrayList<>();
1600-
}
1601-
this.links.addAll(terminatedContextLinks);
1603+
return addLinks(links, terminatedContextLinks);
16021604
}
16031605
}
1606+
return links;
1607+
}
1608+
1609+
protected static final List<AgentSpanLink> addLink(
1610+
List<AgentSpanLink> links,
1611+
AgentSpanLink link)
1612+
{
1613+
if ( links == null ) links = new ArrayList<>();
1614+
links.add(link);
1615+
return links;
1616+
}
1617+
1618+
protected static final List<AgentSpanLink> addLinks(
1619+
List<AgentSpanLink> links,
1620+
List<AgentSpanLink> additionalLinks)
1621+
{
1622+
if ( links == null ) {
1623+
links = new ArrayList<>(additionalLinks);
1624+
} else {
1625+
links.addAll(additionalLinks);
1626+
}
1627+
return links;
16041628
}
16051629

16061630
@Override
@@ -1744,9 +1768,41 @@ public final CoreSpanBuilder withSpanId(final long spanId) {
17441768
* @return the context
17451769
*/
17461770
private final DDSpanContext buildSpanContext() {
1747-
final DDTraceId traceId;
1748-
final long spanId;
1749-
final long parentSpanId;
1771+
return this.buildSpanContext(
1772+
this.tracer,
1773+
this.spanId,
1774+
this.serviceName,
1775+
this.operationName,
1776+
this.resourceName,
1777+
this.parent,
1778+
this.ignoreScope,
1779+
this.errorFlag,
1780+
this.spanType,
1781+
this.tagLedger,
1782+
this.links,
1783+
this.builderRequestContextDataAppSec,
1784+
this.builderRequestContextDataIast,
1785+
this.builderCiVisibilityContextData);
1786+
}
1787+
1788+
protected static final DDSpanContext buildSpanContext(
1789+
final CoreTracer tracer,
1790+
long spanId,
1791+
String serviceName,
1792+
CharSequence operationName,
1793+
String resourceName,
1794+
AgentSpanContext incomingParentContext,
1795+
boolean ignoreScope,
1796+
boolean errorFlag,
1797+
CharSequence spanType,
1798+
TagMap.Ledger tagLedger,
1799+
List<AgentSpanLink> links,
1800+
Object builderRequestContextDataAppSec,
1801+
Object builderRequestContextDataIast,
1802+
Object builderCiVisibilityContextData)
1803+
{
1804+
DDTraceId traceId;
1805+
long parentSpanId;
17501806
final Map<String, String> baggage;
17511807
final Baggage w3cBaggage;
17521808
final TraceCollector parentTraceCollector;
@@ -1762,35 +1818,34 @@ private final DDSpanContext buildSpanContext() {
17621818
Object ciVisibilityContextData;
17631819
final PathwayContext pathwayContext;
17641820
final PropagationTags propagationTags;
1765-
1766-
if (this.spanId == 0) {
1821+
1822+
if (spanId == 0) {
17671823
spanId = tracer.idGenerationStrategy.generateSpanId();
1768-
} else {
1769-
spanId = this.spanId;
17701824
}
17711825

17721826
// Find the parent context
1773-
AgentSpanContext parentContext = parent;
1827+
AgentSpanContext parentContext = incomingParentContext;
17741828
if (parentContext == null && !ignoreScope) {
17751829
// use the Scope as parent unless overridden or ignored.
17761830
final AgentSpan activeSpan = tracer.scopeManager.activeSpan();
17771831
if (activeSpan != null) {
17781832
parentContext = activeSpan.context();
17791833
}
17801834
}
1835+
17811836
// Handle remote terminated context as span links
17821837
if (parentContext != null && parentContext.isRemote()) {
17831838
switch (Config.get().getTracePropagationBehaviorExtract()) {
17841839
case RESTART:
1785-
addParentContextAsLinks(parentContext);
1840+
links = addParentContextLink(links, parentContext);
17861841
parentContext = null;
17871842
break;
17881843
case IGNORE:
17891844
parentContext = null;
17901845
break;
17911846
case CONTINUE:
17921847
default:
1793-
addTerminatedContextAsLinks();
1848+
links = addTerminatedContextAsLinks(links, incomingParentContext);
17941849
}
17951850
}
17961851

@@ -1930,8 +1985,7 @@ private final DDSpanContext buildSpanContext() {
19301985
serviceName = tracer.serviceName;
19311986
}
19321987

1933-
final CharSequence operationName =
1934-
this.operationName != null ? this.operationName : resourceName;
1988+
if ( operationName == null ) operationName = resourceName;
19351989

19361990
final TagMap mergedTracerTags = traceConfig.mergedTracerTags;
19371991
boolean mergedTracerTagsNeedsIntercept = traceConfig.mergedTracerTagsNeedsIntercept;

0 commit comments

Comments
 (0)