New API: use full 64bit random ID range

This commit is contained in:
Nikolay Martynov 2019-01-03 19:29:03 -05:00
parent bb442846bb
commit 8f79538067
1 changed files with 11 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package datadog.trace.tracer; package datadog.trace.tracer;
import datadog.trace.api.sampling.PrioritySampling; import datadog.trace.api.sampling.PrioritySampling;
import java.math.BigInteger;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -53,9 +54,16 @@ class SpanContextImpl implements SpanContext {
return new SpanContextImpl(traceId, parentId, generateNewId()); return new SpanContextImpl(traceId, parentId, generateNewId());
} }
/** @return Random 64bit unsigned number strictly greater than zero */
static String generateNewId() { static String generateNewId() {
// TODO: expand the range of numbers generated to be from 1 to uint 64 MAX // Note: we can probably optimize this using {@link ThreadLocalRandom#nextLong()}
// Ensure the generated ID is in a valid range: // and {@link Long#toUnsignedString} but {@link Long#toUnsignedString} is only
return String.valueOf(ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE)); // available in Java8+ and {@link ThreadLocalRandom#nextLong()} cannot
// generate negative numbers before Java8.
BigInteger result = BigInteger.ZERO;
while (result.equals(BigInteger.ZERO)) {
result = new BigInteger(64, ThreadLocalRandom.current());
}
return result.toString();
} }
} }