Skip to content

Commit a6b2e1f

Browse files
author
Andrew Kent
committed
Don't report traces closed by garbage collection.
1 parent 78e6f6a commit a6b2e1f

4 files changed

Lines changed: 26 additions & 6 deletions

File tree

dd-trace-ot/src/main/java/datadog/opentracing/DDTracer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,12 +297,17 @@ void write(final PendingTrace trace) {
297297
}
298298
}
299299
}
300-
traceCount.incrementAndGet();
300+
incrementTraceCount();
301301
if (!writtenTrace.isEmpty() && sampler.sample(writtenTrace.get(0))) {
302302
writer.write(writtenTrace);
303303
}
304304
}
305305

306+
/** Increment the reported trace count, but do not write a trace. */
307+
void incrementTraceCount() {
308+
traceCount.incrementAndGet();
309+
}
310+
306311
@Override
307312
public void close() {
308313
PendingTrace.close();

dd-trace-ot/src/main/java/datadog/opentracing/PendingTrace.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,19 @@ public synchronized boolean clean() {
204204
int count = 0;
205205
while ((ref = referenceQueue.poll()) != null) {
206206
weakReferences.remove(ref);
207+
if (isWritten.compareAndSet(false, true)) {
208+
// preserve throughput count.
209+
// Don't report the trace because the data comes from buggy uses of the api and is suspect.
210+
tracer.incrementTraceCount();
211+
}
207212
count++;
208213
expireReference();
209214
}
210215
if (count > 0) {
211-
log.debug("{} unfinished spans garbage collected!", count);
216+
log.debug(
217+
"trace {} : {} unfinished spans garbage collected. Trace will not report.",
218+
traceId,
219+
count);
212220
}
213221
return count > 0;
214222
}

dd-trace-ot/src/test/groovy/datadog/opentracing/PendingTraceTest.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class PendingTraceTest extends Specification {
9494
traceCount.get() == 1
9595
}
9696

97-
def "trace reported when unfinished child discarded"() {
97+
def "trace does not report when unfinished child discarded"() {
9898
when:
9999
def child = tracer.buildSpan("child").asChildOf(rootSpan).start()
100100
rootSpan.finish()
@@ -116,7 +116,7 @@ class PendingTraceTest extends Specification {
116116
trace.pendingReferenceCount.get() == 0
117117
trace.weakReferences.size() == 0
118118
trace.asList() == [rootSpan]
119-
writer == [[rootSpan]]
119+
writer == []
120120
traceCount.get() == 1
121121
}
122122

dd-trace-ot/src/test/groovy/datadog/opentracing/scopemanager/ScopeManagerTest.groovy

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import io.opentracing.Span
1010
import io.opentracing.noop.NoopSpan
1111
import spock.lang.Specification
1212
import spock.lang.Subject
13+
import spock.lang.Timeout
1314

1415
import java.lang.ref.WeakReference
16+
import java.util.concurrent.TimeUnit
1517
import java.util.concurrent.atomic.AtomicBoolean
1618
import java.util.concurrent.atomic.AtomicReference
1719

@@ -179,11 +181,13 @@ class ScopeManagerTest extends Specification {
179181
false | true
180182
}
181183

184+
@Timeout(value = 60, unit = TimeUnit.SECONDS)
182185
def "hard reference on continuation prevents trace from reporting"() {
183186
setup:
184187
def builder = tracer.buildSpan("test")
185188
def scope = (ContinuableScope) builder.startActive(false)
186189
def span = scope.span()
190+
def traceCount = ((DDSpan) span).context().tracer.traceCount
187191
scope.setAsyncPropagation(true)
188192
def continuation = scope.capture()
189193
scope.close()
@@ -199,7 +203,9 @@ class ScopeManagerTest extends Specification {
199203
def continuationRef = new WeakReference<>(continuation)
200204
continuation = null // Continuation references also hold up traces.
201205
TestUtils.awaitGC(continuationRef)
202-
writer.waitForTraces(1)
206+
while (traceCount.get() == 0) {
207+
// wait until trace count increments or timeout expires
208+
}
203209
}
204210
if (autoClose) {
205211
if (continuation != null) {
@@ -208,7 +214,8 @@ class ScopeManagerTest extends Specification {
208214
}
209215

210216
then:
211-
writer == [[span]]
217+
forceGC ? true : writer == [[span]]
218+
traceCount.get() == 1
212219

213220
where:
214221
autoClose | forceGC

0 commit comments

Comments
 (0)