make thread safe for nanoTime()
MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java
dd-trace/src/main/java/com/datadoghq/trace/DDBaseSpan.java
package com.datadoghq.trace;
+import com.datadoghq.trace.util.Clock;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.opentracing.BaseSpan;
@@ -13,14 +14,14 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j
public abstract class DDBaseSpan<S extends BaseSpan> implements BaseSpan<S> {
+ /** The context attached to the span */
+ protected final DDSpanContext context;
/** StartTime stores the creation time of the span in milliseconds */
protected long startTimeMicro;
/** StartTimeNano stores the only the nanoseconds for more accuracy */
protected long startTimeNano;
/** The duration in nanoseconds computed using the startTimeMicro and startTimeNano */
protected long durationNano;
- /** The context attached to the span */
- protected final DDSpanContext context;
/**
* A simple constructor. Currently, users have
@@ -38,14 +39,14 @@ public abstract class DDBaseSpan<S extends BaseSpan> implements BaseSpan<S> {
} else {
this.startTimeMicro = timestampMicro;
}
- this.startTimeNano = System.nanoTime();
+ this.startTimeNano = Clock.nowNanos();
// track each span of the trace
this.context.getTrace().add(this);
}
public final void finish() {
- this.durationNano = System.nanoTime() - startTimeNano;
+ this.durationNano = Clock.nowNanos() - startTimeNano;
afterFinish();
}

MODIFY
dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
dd-trace/src/main/java/com/datadoghq/trace/DDTracer.java
@@ -5,6 +5,7 @@ import com.datadoghq.trace.propagation.Codec;
import com.datadoghq.trace.propagation.HTTPCodec;
import com.datadoghq.trace.sampling.AllSampler;
import com.datadoghq.trace.sampling.Sampler;
+import com.datadoghq.trace.util.Clock;
import com.datadoghq.trace.writer.LoggingWriter;
import com.datadoghq.trace.writer.Writer;
import io.opentracing.ActiveSpan;
@@ -136,6 +137,24 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
writer.close();
}
+ @Override
+ public String toString() {
+ return "DDTracer{" + "writer=" + writer + ", sampler=" + sampler + '}';
+ }
+
+ private static class CodecRegistry {
+
+ private final Map<Format<?>, Codec<?>> codecs = new HashMap<>();
+
+ <T> Codec<T> get(final Format<T> format) {
+ return (Codec<T>) codecs.get(format);
+ }
+
+ public <T> void register(final Format<T> format, final Codec<T> codec) {
+ codecs.put(format, codec);
+ }
+ }
+
/** Spans are built using this builder */
public class DDSpanBuilder implements SpanBuilder {
private final ActiveSpanSource spanSource;
@@ -153,6 +172,11 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
private String spanType;
private boolean ignoreActiveSpan = false;
+ public DDSpanBuilder(final String operationName, final ActiveSpanSource spanSource) {
+ this.operationName = operationName;
+ this.spanSource = spanSource;
+ }
+
@Override
public SpanBuilder ignoreActiveSpan() {
this.ignoreActiveSpan = true;
@@ -206,11 +230,6 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
return withTag(tag, (Object) bool);
}
- public DDSpanBuilder(final String operationName, final ActiveSpanSource spanSource) {
- this.operationName = operationName;
- this.spanSource = spanSource;
- }
-
@Override
public DDSpanBuilder withStartTimestamp(final long timestampMillis) {
this.timestamp = timestampMillis;
@@ -271,7 +290,7 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
}
private long generateNewId() {
- return System.nanoTime();
+ return Clock.nowNanos();
}
/**
@@ -344,22 +363,4 @@ public class DDTracer extends ThreadLocalActiveSpanSource implements io.opentrac
return context;
}
}
-
- private static class CodecRegistry {
-
- private final Map<Format<?>, Codec<?>> codecs = new HashMap<>();
-
- <T> Codec<T> get(final Format<T> format) {
- return (Codec<T>) codecs.get(format);
- }
-
- public <T> void register(final Format<T> format, final Codec<T> codec) {
- codecs.put(format, codec);
- }
- }
-
- @Override
- public String toString() {
- return "DDTracer{" + "writer=" + writer + ", sampler=" + sampler + '}';
- }
}

ADD
dd-trace/src/main/java/com/datadoghq/trace/util/Clock.java
+package com.datadoghq.trace.util;
+
+public class Clock {
+
+ public synchronized static long nowNanos() {
+ return System.nanoTime();
+ }
+}

make thread safe for nanoTime()