@@ -5,6 +5,7 @@ import datadog.communication.monitor.Monitoring
55import datadog.trace.SamplingPriorityMetadataChecker
66import datadog.trace.api.DDSpanId
77import datadog.trace.api.DDTraceId
8+ import datadog.trace.api.flare.TracerFlare
89import datadog.trace.api.sampling.PrioritySampling
910import datadog.trace.api.time.SystemTimeSource
1011import datadog.trace.bootstrap.instrumentation.api.AgentTracer.NoopPathwayContext
@@ -20,8 +21,11 @@ import spock.util.concurrent.PollingConditions
2021import java.util.concurrent.CountDownLatch
2122import java.util.concurrent.TimeUnit
2223import java.util.concurrent.atomic.AtomicInteger
24+ import java.util.zip.ZipInputStream
25+ import java.util.zip.ZipOutputStream
2326
2427import static datadog.trace.core.PendingTraceBuffer.BUFFER_SIZE
28+ import static java.nio.charset.StandardCharsets.UTF_8
2529
2630@Timeout (5 )
2731class PendingTraceBufferTest extends DDSpecification {
@@ -443,6 +447,71 @@ class PendingTraceBufferTest extends DDSpecification {
443447 }
444448 }
445449
450+ def " testing tracer flare dump" () {
451+ setup :
452+ // Don't start the buffer thread
453+ TracerFlare . addReporter {} // exercises default methods
454+ def dumpReporter = Mock (PendingTraceBuffer.TracerDump )
455+ TracerFlare . addReporter(dumpReporter)
456+
457+ when :
458+ def pendingTrace = factory. create(DDTraceId . ONE )
459+ def span = newSpanOf(pendingTrace)
460+ def entries = buildAndExtractZip()
461+
462+ then :
463+ 1 * dumpReporter. prepareForFlare()
464+
465+ then :
466+ 1 * dumpReporter. addReportToFlare(_)
467+
468+ then :
469+ 1 * dumpReporter. cleanupAfterFlare()
470+
471+ and :
472+ entries. size() == 2
473+ entries[" trace_dump.txt" ] == " example text"
474+ entries[" flare_errors.txt" ] =~
475+ / ^(java.lang.IllegalStateException: (bin|txt) \( expected\)\n ){2}$/
476+ // then:
477+ // 1 * tracer.captureTraceConfig() >> traceConfig
478+ // pendingTrace.rootSpanWritten
479+ // pendingTrace.isEnqueued == 0
480+ // buffer.queue.size() == 0
481+ // _ * bufferSpy.longRunningSpansEnabled()
482+ // 1 * tracer.writeTimer() >> Monitoring.DISABLED.newTimer("")
483+ // 1 * tracer.write({ it.size() == 1 })
484+ // 1 * tracer.getPartialFlushMinSpans() >> 10000
485+ // 1 * traceConfig.getServiceMapping() >> [:]
486+ // 2 * tracer.getTimeWithNanoTicks(_)
487+ // 1 * tracer.onRootSpanPublished(_)
488+ // 0 * _
489+ //
490+ // when: "fail to fill the buffer"
491+ // for (i in 1..buffer.queue.capacity()) {
492+ // addContinuation(newSpanOf(span)).finish()
493+ // }
494+ //
495+ // then:
496+ // pendingTrace.isEnqueued == 1
497+ // buffer.queue.size() == 1
498+ // buffer.queue.capacity() * bufferSpy.enqueue(_)
499+ // _ * bufferSpy.longRunningSpansEnabled()
500+ // _ * tracer.getPartialFlushMinSpans() >> 10000
501+ // _ * traceConfig.getServiceMapping() >> [:]
502+ // _ * tracer.getTimeWithNanoTicks(_)
503+ // 0 * _
504+ //
505+ // when: "process the buffer"
506+ // buffer.start()
507+ //
508+ // then:
509+ // new PollingConditions(timeout: 3, initialDelay: 0, delay: 0.5, factor: 1).eventually {
510+ // assert pendingTrace.isEnqueued == 0
511+ // }
512+ }
513+
514+
446515 def addContinuation (DDSpan span ) {
447516 def scope = scopeManager. activate(span, ScopeSource . INSTRUMENTATION , true )
448517 continuations << scope. capture()
@@ -502,4 +571,27 @@ class PendingTraceBufferTest extends DDSpecification {
502571 PropagationTags . factory(). empty())
503572 return DDSpan . create(" test" , 0 , context, null )
504573 }
574+
575+ def buildAndExtractZip () {
576+ TracerFlare . prepareForFlare()
577+ def out = new ByteArrayOutputStream ()
578+ try (ZipOutputStream zip = new ZipOutputStream (out)) {
579+ TracerFlare . addReportsToFlare(zip)
580+ } finally {
581+ TracerFlare . cleanupAfterFlare()
582+ }
583+
584+ def entries = [:]
585+
586+ def zip = new ZipInputStream (new ByteArrayInputStream (out. toByteArray()))
587+ def entry
588+ while (entry = zip. nextEntry) {
589+ def bytes = new ByteArrayOutputStream ()
590+ bytes << zip
591+ entries. put(entry. name, entry. name. endsWith(" .bin" )
592+ ? bytes. toByteArray() : new String (bytes. toByteArray(), UTF_8 ))
593+ }
594+
595+ return entries
596+ }
505597}
0 commit comments