Skip to content

Commit

Permalink
Auto-detect Android and use an Android-friendly IdGenerator instance. (
Browse files Browse the repository at this point in the history
…#3176)

* Auto-detect Android and use an Android-friendly IdGenerator instance.

* simplify the random initialization

* Make the AndroidFriendlyRandomIdGenerator even simpler by just keeping a single Random instance
  • Loading branch information
jkwatson authored Apr 27, 2021
1 parent cf368d6 commit f5c1e0d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.trace;

import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.TraceId;
import java.util.Random;

/**
* {@link IdGenerator} instance that doesn't use {@link java.util.concurrent.ThreadLocalRandom},
* which is broken on most versions of Android (it uses the same seed everytime it starts up).
*/
enum AndroidFriendlyRandomIdGenerator implements IdGenerator {
INSTANCE;

private static final Random random = new Random();

private static final long INVALID_ID = 0;

@Override
public String generateSpanId() {
long id;
do {
id = random.nextLong();
} while (id == INVALID_ID);
return SpanId.fromLong(id);
}

@Override
public String generateTraceId() {
long idHi;
long idLo;
do {
idHi = random.nextLong();
idLo = random.nextLong();
} while (idHi == INVALID_ID && idLo == INVALID_ID);
return TraceId.fromLongs(idHi, idLo);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public interface IdGenerator {
* randomness but may change in the future.
*/
static IdGenerator random() {
// note: check borrowed from OkHttp's check for Android.
if ("Dalvik".equals(System.getProperty("java.vm.name"))) {
return AndroidFriendlyRandomIdGenerator.INSTANCE;
}
return RandomIdGenerator.INSTANCE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,25 @@ void defaults() {

// Can't assert values but can assert they're valid, try a lot as a sort of fuzz check.
for (int i = 0; i < 1000; i++) {
CharSequence traceId = generator.generateTraceId();
assertThat(traceId.toString()).isNotEqualTo(TraceId.getInvalid());
String traceId = generator.generateTraceId();
assertThat(traceId).isNotEqualTo(TraceId.getInvalid());

CharSequence spanId = generator.generateSpanId();
assertThat(spanId.toString()).isNotEqualTo(SpanId.getInvalid());
String spanId = generator.generateSpanId();
assertThat(spanId).isNotEqualTo(SpanId.getInvalid());
}
}

@Test
void androidVersion() {
IdGenerator generator = AndroidFriendlyRandomIdGenerator.INSTANCE;

// Can't assert values but can assert they're valid, try a lot as a sort of fuzz check.
for (int i = 0; i < 1000; i++) {
String traceId = generator.generateTraceId();
assertThat(traceId).isNotEqualTo(TraceId.getInvalid());

String spanId = generator.generateSpanId();
assertThat(spanId).isNotEqualTo(SpanId.getInvalid());
}
}
}

0 comments on commit f5c1e0d

Please sign in to comment.