From 3e4955ad88aa36dafd7865825eba2284445f5bc7 Mon Sep 17 00:00:00 2001 From: Matthew Li Date: Mon, 16 Dec 2024 16:00:51 -0500 Subject: [PATCH] rebasing --- .../java/datadog/trace/core/CoreTracer.java | 2 - .../trace/core/PendingTraceBuffer.java | 45 ++++++++++++++ .../datadog/trace/core/util/TracerDump.java | 58 ------------------- 3 files changed, 45 insertions(+), 60 deletions(-) delete mode 100644 dd-trace-core/src/main/java/datadog/trace/core/util/TracerDump.java diff --git a/dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java b/dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java index b6f4e4bdf1b..7d93d13c2a6 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/CoreTracer.java @@ -252,7 +252,6 @@ public void onRootSpanFinished(AgentSpan root, EndpointTracker tracker) { if (!root.isOutbound()) { profilingContextIntegration.onRootSpanFinished(root, tracker); } - TracerDump.suspendRootSpan(root); } /** @@ -277,7 +276,6 @@ public EndpointTracker onRootSpanStarted(AgentSpan root) { if (!root.isOutbound()) { return profilingContextIntegration.onRootSpanStarted(root); } - TracerDump.addUnfinishedRootSpan(root); return null; } diff --git a/dd-trace-core/src/main/java/datadog/trace/core/PendingTraceBuffer.java b/dd-trace-core/src/main/java/datadog/trace/core/PendingTraceBuffer.java index ac48eb96337..b936ed47993 100644 --- a/dd-trace-core/src/main/java/datadog/trace/core/PendingTraceBuffer.java +++ b/dd-trace-core/src/main/java/datadog/trace/core/PendingTraceBuffer.java @@ -6,10 +6,14 @@ import datadog.communication.ddagent.SharedCommunicationObjects; import datadog.trace.api.Config; +import datadog.trace.api.flare.TracerFlare; import datadog.trace.api.time.TimeSource; import datadog.trace.core.monitor.HealthMetrics; +import java.io.IOException; +import java.util.ArrayList; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; +import java.util.zip.ZipOutputStream; import org.jctools.queues.MessagePassingQueue; import org.jctools.queues.MpscBlockingConsumerArrayQueue; import org.slf4j.Logger; @@ -44,6 +48,7 @@ public interface Element { } private static class DelayingPendingTraceBuffer extends PendingTraceBuffer { + private static final Logger log = LoggerFactory.getLogger(DelayingPendingTraceBuffer.class); private static final long FORCE_SEND_DELAY_MS = TimeUnit.SECONDS.toMillis(5); private static final long SEND_DELAY_NS = TimeUnit.MILLISECONDS.toNanos(500); private static final long SLEEP_TIME_MS = 100; @@ -78,6 +83,7 @@ public void enqueue(Element pendingTrace) { @Override public void start() { + TracerFlare.addReporter(new TracerDump(this)); worker.start(); } @@ -235,6 +241,45 @@ public DelayingPendingTraceBuffer( config, bufferSize, sharedCommunicationObjects, healthMetrics) : null; } + + static class TracerDump implements TracerFlare.Reporter { + + private final DelayingPendingTraceBuffer buffer; + + public TracerDump(DelayingPendingTraceBuffer buffer) { + this.buffer = buffer; + } + + @Override + public void addReportToFlare(ZipOutputStream zip) throws IOException { + TracerFlare.addText(zip, "trace_dump.txt", getDumpText()); + } + + private String getDumpText() { + StringBuilder dumpText = new StringBuilder(); + try { + Element head = buffer.queue.poll(1, TimeUnit.SECONDS); + ArrayList dumpSpans = new ArrayList<>(); + while (head != null && dumpSpans.size() < 100) { // temp arbitrary limit + dumpSpans.add(head); + head = buffer.queue.poll(); + } + for (Element e : dumpSpans) { + buffer.queue.offer(e); + if (e instanceof PendingTrace) { + PendingTrace trace = (PendingTrace) e; + for (DDSpan span : trace.getSpans()) { + dumpText.append(span.toString()).append("\n"); + } + } + } + } catch (InterruptedException e) { + log.error("Error with polling the buffer queue. Buffer: {}", buffer); + Thread.currentThread().interrupt(); + } + return dumpText.toString(); + } + } } static class DiscardingPendingTraceBuffer extends PendingTraceBuffer { diff --git a/dd-trace-core/src/main/java/datadog/trace/core/util/TracerDump.java b/dd-trace-core/src/main/java/datadog/trace/core/util/TracerDump.java deleted file mode 100644 index 755646bbe21..00000000000 --- a/dd-trace-core/src/main/java/datadog/trace/core/util/TracerDump.java +++ /dev/null @@ -1,58 +0,0 @@ -package datadog.trace.core.util; - -import datadog.trace.api.flare.TracerFlare; -import datadog.trace.bootstrap.instrumentation.api.AgentSpan; -import datadog.trace.core.DDSpan; -import datadog.trace.core.PendingTrace; -import java.io.IOException; -import java.lang.ref.WeakReference; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.Set; -import java.util.zip.ZipOutputStream; - -public final class TracerDump implements TracerFlare.Reporter { - - private static final TracerDump INSTANCE = new TracerDump(); - - public static void register() { - TracerFlare.addReporter(INSTANCE); - } - - private static final Set> rootSpans = new HashSet<>(); - private static final Map> idToWeakRef = new HashMap<>(); - - public static void addUnfinishedRootSpan(final AgentSpan rootSpan) { - DDSpan rootDDSpan = (DDSpan) rootSpan; - WeakReference weakRootSpan = new WeakReference<>(rootDDSpan); - idToWeakRef.put(getSpanIdString(rootSpan), weakRootSpan); - rootSpans.add(weakRootSpan); - } - - public static void suspendRootSpan(final AgentSpan rootSpan) { - WeakReference weakRootSpan = idToWeakRef.get(getSpanIdString(rootSpan)); - rootSpans.remove(weakRootSpan); - } - - @Override - public void addReportToFlare(ZipOutputStream zip) throws IOException { - StringBuilder dumpText = new StringBuilder(); - for (WeakReference weakRootSpan : rootSpans) { - DDSpan rootSpan = weakRootSpan.get(); - if (rootSpan != null) { - PendingTrace trace = (PendingTrace) rootSpan.context().getTraceCollector(); - if (trace != null) { - for (DDSpan span : trace.getSpans()) { - dumpText.append(span.toString()).append("\n"); - } - } - } - } - TracerFlare.addText(zip, "trace_dump.txt", dumpText.toString()); - } - - private static String getSpanIdString(final AgentSpan rootSpan) { - return String.valueOf(rootSpan.getSpanId()); - } -}