Prevent traces from finishing with 0 duration
Ensure a minimum duration of 1.
Also improve logging a bit.
MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java
dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java
@@ -50,7 +50,9 @@ public abstract class DDBaseSpan<S extends BaseSpan> implements BaseSpan<S> {
}
public final void finish(final long stoptimeMicros) {
- this.durationNano = TimeUnit.MICROSECONDS.toNanos(stoptimeMicros - this.startTimeMicro);
+ // Ensure that duration is at least 1. Less than 1 is possible due to our use of system clock instead of nano time.
+ this.durationNano =
+ Math.max(1, TimeUnit.MICROSECONDS.toNanos(stoptimeMicros - this.startTimeMicro));
afterFinish();
}

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
@@ -223,15 +223,16 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
@Override
public ActiveSpan startActive() {
- final ActiveSpan activeSpan = spanSource.makeActive(startSpan());
- log.debug("{} - Starting a new active span.", activeSpan);
+ final DDSpan span = startSpan();
+ final ActiveSpan activeSpan = spanSource.makeActive(span);
+ log.debug("Starting a new active span: {}", span);
return activeSpan;
}
@Override
public DDSpan startManual() {
final DDSpan span = startSpan();
- log.debug("{} - Starting a new manuel span.", span);
+ log.debug("Starting a new manual span: {}", span);
return span;
}

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/writer/DDAgentWriter.java
dd-trace/src/main/java/com/datadoghq/trace/writer/DDAgentWriter.java
@@ -141,11 +141,13 @@ public class DDAgentWriter implements Writer {
final Future<Long> future = executor.submit(new SendingTask());
try {
final long nbTraces = future.get(API_TIMEOUT_SECONDS, TimeUnit.SECONDS);
- log.debug("Successfully sending {} traces to the API", nbTraces);
+ if (nbTraces > 0) {
+ log.debug("Successfully sent {} traces to the API", nbTraces);
+ }
} catch (final TimeoutException e) {
- log.debug("Timeout! Fail to send traces to the API: {}", e.getMessage());
+ log.debug("Timeout! Failed to send traces to the API: {}", e.getMessage());
} catch (final Throwable e) {
- log.debug("Fail to send traces to the API: {}", e.getMessage());
+ log.debug("Failed to send traces to the API: {}", e.getMessage());
}
}

Prevent traces from finishing with 0 duration

Ensure a minimum duration of 1.

Also improve logging a bit.