Skip to content

Commit

Permalink
Rename getTrace() to getTraceCollector(), and createTrace() to create…
Browse files Browse the repository at this point in the history
…TraceCollector()
  • Loading branch information
nikita-tkachenko-datadog committed Jul 4, 2024
1 parent 4bcf355 commit 096822a
Show file tree
Hide file tree
Showing 31 changed files with 161 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import datadog.trace.api.DDTraceId;
import datadog.trace.api.sampling.PrioritySampling;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTrace;
import datadog.trace.bootstrap.instrumentation.api.AgentTraceCollector;
import datadog.trace.bootstrap.instrumentation.api.AgentTracer;
import datadog.trace.bootstrap.instrumentation.api.PathwayContext;
import io.opentelemetry.api.trace.Span;
Expand Down Expand Up @@ -60,8 +60,8 @@ public long getSpanId() {
}

@Override
public AgentTrace getTrace() {
return AgentTracer.NoopAgentTrace.INSTANCE;
public AgentTraceCollector getTraceCollector() {
return AgentTracer.NoopAgentTraceCollector.INSTANCE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import datadog.trace.api.DDTraceId;
import datadog.trace.api.sampling.PrioritySampling;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.AgentTrace;
import datadog.trace.bootstrap.instrumentation.api.AgentTraceCollector;
import datadog.trace.bootstrap.instrumentation.api.PathwayContext;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -75,7 +75,7 @@ public long getSpanId() {
}

@Override
public AgentTrace getTrace() {
public AgentTraceCollector getTraceCollector() {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,12 @@ abstract class AgentTestRunner extends DDSpecification implements AgentBuilder.L

static void blockUntilChildSpansFinished(AgentSpan span, int numberOfSpans) {
if (span instanceof DDSpan) {
def trace = ((DDSpan) span).context().getTrace()
if (!(trace instanceof PendingTrace)) {
throw new IllegalStateException("Expected $PendingTrace.name trace collector, got $trace.class.name")
def traceCollector = ((DDSpan) span).context().getTraceCollector()
if (!(traceCollector instanceof PendingTrace)) {
throw new IllegalStateException("Expected $PendingTrace.name trace collector, got $traceCollector.class.name")
}

final PendingTrace pendingTrace = (PendingTrace) trace
final PendingTrace pendingTrace = (PendingTrace) traceCollector
final long deadline = System.currentTimeMillis() + TIMEOUT_MILLIS

while (pendingTrace.size() < numberOfSpans) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public void init(Blackhole blackhole) {
.build();

DDTraceId traceId = DDTraceId.ONE;
TraceCollector traceCollector = tracer.createTrace(traceId);
TraceCollector traceCollector = tracer.createTraceCollector(traceId);
DDSpanContext rootContext =
new DDSpanContext(
traceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class PendingTraceWrite {

CoreTracer tracer;
TraceCollector trace;
TraceCollector traceCollector;

@Param({"10", "100"})
int depthPerThread;
Expand All @@ -37,7 +37,7 @@ public void init(TraceCounters counters, Blackhole blackhole) {
.strictTraceWrites(false)
.build();
DDTraceId traceId = DDTraceId.ONE;
trace = tracer.createTrace(traceId);
traceCollector = tracer.createTraceCollector(traceId);
root =
DDSpan.create(
"benchmark",
Expand All @@ -56,7 +56,7 @@ public void init(TraceCounters counters, Blackhole blackhole) {
false,
"type",
0,
trace,
traceCollector,
null,
null,
NoopPathwayContext.INSTANCE,
Expand All @@ -81,7 +81,7 @@ public void init(TraceCounters counters, Blackhole blackhole) {
false,
"type",
0,
trace,
traceCollector,
null,
null,
NoopPathwayContext.INSTANCE,
Expand All @@ -93,13 +93,13 @@ public void init(TraceCounters counters, Blackhole blackhole) {
@Threads(4)
@Benchmark
public void writeTraces() {
trace.registerSpan(root);
traceCollector.registerSpan(root);
for (int i = 0; i < depthPerThread; ++i) {
trace.registerSpan(span);
traceCollector.registerSpan(span);
}
for (int i = 0; i < depthPerThread; ++i) {
trace.onPublish(span);
traceCollector.onPublish(span);
}
trace.onPublish(root);
traceCollector.onPublish(root);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private DDSpan createEnrichedSpanWithOrigin(int iter, final String origin) {

private DDSpan createSpanWithOrigin(int iter, final String origin) {
final DDTraceId traceId = DDTraceId.from(iter);
final TraceCollector trace = tracer.createTrace(traceId);
final TraceCollector traceCollector = tracer.createTraceCollector(traceId);
return DDSpan.create(
"benchmark",
System.currentTimeMillis() * 1000,
Expand All @@ -114,7 +114,7 @@ private DDSpan createSpanWithOrigin(int iter, final String origin) {
false,
"type",
0,
trace,
traceCollector,
null,
null,
NoopPathwayContext.INSTANCE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void setUp(Blackhole blackhole) {
false,
"type",
0,
tracer.createTrace(traceId),
tracer.createTraceCollector(traceId),
null,
null,
null,
Expand Down
20 changes: 10 additions & 10 deletions dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java
Original file line number Diff line number Diff line change
Expand Up @@ -806,11 +806,11 @@ protected void finalize() {
*
* @return a PendingTrace
*/
public TraceCollector createTrace(DDTraceId id) {
public TraceCollector createTraceCollector(DDTraceId id) {
return traceCollectorFactory.create(id);
}

TraceCollector createTrace(DDTraceId id, ConfigSnapshot traceConfig) {
TraceCollector createTraceCollector(DDTraceId id, ConfigSnapshot traceConfig) {
return traceCollectorFactory.create(id, traceConfig);
}

Expand Down Expand Up @@ -987,7 +987,7 @@ void write(final List<DDSpan> trace) {
}
boolean forceKeep = metricsAggregator.publish(writtenTrace);

TraceCollector traceCollector = writtenTrace.get(0).context().getTrace();
TraceCollector traceCollector = writtenTrace.get(0).context().getTraceCollector();
traceCollector.setSamplingPriorityIfNecessary();

DDSpan rootSpan = traceCollector.getRootSpan();
Expand Down Expand Up @@ -1442,7 +1442,7 @@ private DDSpanContext buildSpanContext() {
final long spanId = idGenerationStrategy.generateSpanId();
final long parentSpanId;
final Map<String, String> baggage;
final TraceCollector parentTrace;
final TraceCollector parentTraceCollector;
final int samplingPriority;
final CharSequence origin;
final Map<String, String> coreTags;
Expand Down Expand Up @@ -1477,7 +1477,7 @@ private DDSpanContext buildSpanContext() {
traceId = ddsc.getTraceId();
parentSpanId = ddsc.getSpanId();
baggage = ddsc.getBaggageItems();
parentTrace = ddsc.getTrace();
parentTraceCollector = ddsc.getTraceCollector();
samplingPriority = PrioritySampling.UNSET;
origin = null;
coreTags = null;
Expand Down Expand Up @@ -1551,14 +1551,14 @@ private DDSpanContext buildSpanContext() {

rootSpanTags = localRootSpanTags;

parentTrace = createTrace(traceId, traceConfig);
parentTraceCollector = createTraceCollector(traceId, traceConfig);

if (endToEndStartTime > 0) {
parentTrace.beginEndToEnd(endToEndStartTime);
parentTraceCollector.beginEndToEnd(endToEndStartTime);
}
}

ConfigSnapshot traceConfig = parentTrace.getTraceConfig();
ConfigSnapshot traceConfig = parentTraceCollector.getTraceConfig();

// Use parent pathwayContext if present and started
pathwayContext =
Expand All @@ -1571,7 +1571,7 @@ private DDSpanContext buildSpanContext() {
// when removing fake services the best upward service name to pick is the local root one
// since a split by tag (i.e. servlet context) might have happened on it.
if (!allowInferredServices) {
final DDSpan rootSpan = parentTrace.getRootSpan();
final DDSpan rootSpan = parentTraceCollector.getRootSpan();
serviceName = rootSpan != null ? rootSpan.getServiceName() : null;
}
if (serviceName == null) {
Expand Down Expand Up @@ -1620,7 +1620,7 @@ private DDSpanContext buildSpanContext() {
errorFlag,
spanType,
tagsSize,
parentTrace,
parentTraceCollector,
requestContextDataAppSec,
requestContextDataIast,
ciVisibilityContextData,
Expand Down
39 changes: 20 additions & 19 deletions dd-trace-core/src/main/java/datadog/trace/core/DDSpan.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ static DDSpan create(
final List<AgentSpanLink> links) {
final DDSpan span = new DDSpan(instrumentationName, timestampMicro, context, links);
log.debug("Started span: {}", span);
context.getTrace().registerSpan(span);
context.getTraceCollector().registerSpan(span);
return span;
}

Expand Down Expand Up @@ -132,12 +132,12 @@ private DDSpan(

if (timestampMicro <= 0L) {
// note: getting internal time from the trace implicitly 'touches' it
startTimeNano = context.getTrace().getCurrentTimeNano();
startTimeNano = context.getTraceCollector().getCurrentTimeNano();
externalClock = false;
} else {
startTimeNano = MICROSECONDS.toNanos(timestampMicro);
externalClock = true;
context.getTrace().touch(); // external clock: explicitly update lastReferenced
context.getTraceCollector().touch(); // external clock: explicitly update lastReferenced
}

this.links = links == null ? new CopyOnWriteArrayList<>() : new CopyOnWriteArrayList<>(links);
Expand All @@ -152,7 +152,7 @@ private void finishAndAddToTrace(final long durationNano) {
if (DURATION_NANO_UPDATER.compareAndSet(this, 0, Math.max(1, durationNano))) {
setLongRunningVersion(-this.longRunningVersion);
this.metrics.onSpanFinished();
TraceCollector.PublishState publishState = context.getTrace().onPublish(this);
TraceCollector.PublishState publishState = context.getTraceCollector().onPublish(this);
log.debug("Finished span ({}): {}", publishState, this);
} else {
log.debug("Already finished: {}", this);
Expand All @@ -163,9 +163,9 @@ private void finishAndAddToTrace(final long durationNano) {
public void finish() {
if (!externalClock) {
// no external clock was used, so we can rely on nano time
finishAndAddToTrace(context.getTrace().getCurrentTimeNano() - startTimeNano);
finishAndAddToTrace(context.getTraceCollector().getCurrentTimeNano() - startTimeNano);
} else {
finish(context.getTrace().getTimeSource().getCurrentTimeMicros());
finish(context.getTraceCollector().getTimeSource().getCurrentTimeMicros());
}
}

Expand All @@ -175,17 +175,17 @@ public void finish(final long stopTimeMicros) {
if (!externalClock) {
// first capture wall-clock offset from 'now' to external stop time
long externalOffsetMicros =
stopTimeMicros - context.getTrace().getTimeSource().getCurrentTimeMicros();
stopTimeMicros - context.getTraceCollector().getTimeSource().getCurrentTimeMicros();
// immediately afterwards calculate internal duration of span to 'now'
// note: getting internal time from the trace implicitly 'touches' it
durationNano = context.getTrace().getCurrentTimeNano() - startTimeNano;
durationNano = context.getTraceCollector().getCurrentTimeNano() - startTimeNano;
// drop nanosecond precision part of internal duration (expected behaviour)
durationNano = MILLISECONDS.toNanos(NANOSECONDS.toMillis(durationNano));
// add wall-clock offset to get total duration to external stop time
durationNano += MICROSECONDS.toNanos(externalOffsetMicros);
} else {
durationNano = MICROSECONDS.toNanos(stopTimeMicros) - startTimeNano;
context.getTrace().touch(); // external clock: explicitly update lastReferenced
context.getTraceCollector().touch(); // external clock: explicitly update lastReferenced
}
finishAndAddToTrace(durationNano);
}
Expand Down Expand Up @@ -239,10 +239,11 @@ public final boolean phasedFinish() {
long durationNano;
if (!externalClock) {
// note: getting internal time from the trace implicitly 'touches' it
durationNano = context.getTrace().getCurrentTimeNano() - startTimeNano;
durationNano = context.getTraceCollector().getCurrentTimeNano() - startTimeNano;
} else {
durationNano = context.getTrace().getTimeSource().getCurrentTimeNanos() - startTimeNano;
context.getTrace().touch(); // external clock: explicitly update lastReferenced
durationNano =
context.getTraceCollector().getTimeSource().getCurrentTimeNanos() - startTimeNano;
context.getTraceCollector().touch(); // external clock: explicitly update lastReferenced
}
// Flip the negative bit of the result to allow verifying that publish() is only called once.
if (DURATION_NANO_UPDATER.compareAndSet(this, 0, Math.max(1, durationNano) | Long.MIN_VALUE)) {
Expand All @@ -263,7 +264,7 @@ public final void publish() {
log.debug("Already published: {}", this);
} else if (DURATION_NANO_UPDATER.compareAndSet(
this, durationNano, durationNano & Long.MAX_VALUE)) {
TraceCollector.PublishState publishState = context.getTrace().onPublish(this);
TraceCollector.PublishState publishState = context.getTraceCollector().onPublish(this);
log.debug("Published span ({}): {}", publishState, this);
}
}
Expand Down Expand Up @@ -314,7 +315,7 @@ public AgentSpan getRootSpan() {

@Override
public DDSpan getLocalRootSpan() {
return context.getTrace().getRootSpan();
return context.getTraceCollector().getRootSpan();
}

/**
Expand Down Expand Up @@ -566,9 +567,9 @@ public RequestContext getRequestContext() {

@Override
public Integer forceSamplingDecision() {
TraceCollector trace = this.context.getTrace();
DDSpan rootSpan = trace.getRootSpan();
trace.setSamplingPriorityIfNecessary();
TraceCollector traceCollector = this.context.getTraceCollector();
DDSpan rootSpan = traceCollector.getRootSpan();
traceCollector.setSamplingPriorityIfNecessary();
if (rootSpan == null) {
return null;
}
Expand Down Expand Up @@ -737,7 +738,7 @@ public <U> U getTag(CharSequence name) {

@Override
public boolean hasSamplingPriority() {
return context.getTrace().getRootSpan() == this;
return context.getTraceCollector().getRootSpan() == this;
}

@Override
Expand Down Expand Up @@ -821,7 +822,7 @@ public void setLongRunningVersion(int longRunningVersion) {

@Override
public TraceConfig traceConfig() {
return context.getTrace().getTraceConfig();
return context.getTraceCollector().getTraceConfig();
}

@Override
Expand Down
Loading

0 comments on commit 096822a

Please sign in to comment.