Remove helper methods fromBytes from Trace/Span Id (#2727)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
Bogdan Drutu 2021-02-05 20:29:51 -08:00 committed by GitHub
parent d932baa919
commit d6fea3a70a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36 additions and 172 deletions

View File

@ -123,12 +123,6 @@ final class BigendianEncoding {
return result;
}
static void bytesToBase16(byte[] bytes, char[] dest) {
for (int i = 0; i < bytes.length; i++) {
byteToBase16(bytes[i], dest, i * 2);
}
}
/**
* Encodes the specified byte, and returns the encoded {@code String}.
*

View File

@ -59,22 +59,6 @@ public final class SpanId {
&& BigendianEncoding.isValidBase16String(spanId);
}
/**
* Returns the lowercase hex (base16) representation of the {@code SpanId} converted from the
* given bytes representation.
*
* @param spanIdBytes the bytes (8-byte array) representation of the {@code SpanId}.
* @return the lowercase hex (base16) representation of the {@code SpanId}.
* @throws NullPointerException if {@code spanIdBytes} is null.
* @throws IndexOutOfBoundsException if {@code spanIdBytes} too short.
*/
public static String fromBytes(byte[] spanIdBytes) {
Objects.requireNonNull(spanIdBytes, "spanIdBytes");
char[] result = getTemporaryBuffer();
BigendianEncoding.bytesToBase16(spanIdBytes, result);
return new String(result);
}
/**
* Returns the bytes (8-byte array) representation of the {@code SpanId} converted from the given
* lowercase hex (base16) representation.
@ -98,9 +82,6 @@ public final class SpanId {
* rules applying to {@code SpanId}. Specifying 0 for the long value will effectively return
* {@link #getInvalid()}.
*
* <p>This is equivalent to calling {@link #fromBytes(byte[])} with the specified value stored as
* big-endian.
*
* @param id the higher part of the {@code TraceId}.
* @return the lowercase hex (base16) representation of the {@code SpanId}.
*/

View File

@ -63,22 +63,6 @@ public final class TraceId {
&& BigendianEncoding.isValidBase16String(traceId);
}
/**
* Returns the lowercase hex (base16) representation of the {@code TraceId} converted from the
* given bytes representation.
*
* @param traceIdBytes the bytes (16-byte array) representation of the {@code TraceId}.
* @return the lowercase hex (base16) representation of the {@code TraceId}.
* @throws NullPointerException if {@code traceIdBytes} is null.
* @throws IndexOutOfBoundsException if {@code traceIdBytes} too short.
*/
public static String fromBytes(byte[] traceIdBytes) {
Objects.requireNonNull(traceIdBytes, "traceIdBytes");
char[] result = getTemporaryBuffer();
BigendianEncoding.bytesToBase16(traceIdBytes, result);
return new String(result);
}
/**
* Returns the bytes (16-byte array) representation of the {@code TraceId} converted from the
* given lowercase hex (base16) representation.
@ -102,9 +86,6 @@ public final class TraceId {
* rules applying to {@code TraceId}. Specifying 0 for both values will effectively return {@link
* #getInvalid()}.
*
* <p>This is equivalent to calling {@link #fromBytes(byte[])} with the specified values stored as
* big-endian.
*
* @param traceIdLongHighPart the higher part of the long values representation of the {@code
* TraceId}.
* @param traceIdLongLowPart the lower part of the long values representation of the {@code

View File

@ -12,7 +12,9 @@ import org.junit.jupiter.api.Test;
/** Unit tests for {@link SpanId}. */
class SpanIdTest {
private static final String first = "0000000000000061";
private static final byte[] firstBytes = new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'};
private static final String second = "ff00000000000041";
private static final byte[] secondBytes = new byte[] {(byte) 0xFF, 0, 0, 0, 0, 0, 0, 'A'};
@Test
@ -25,25 +27,18 @@ class SpanIdTest {
@Test
void isValid() {
assertThat(SpanId.isValid(SpanId.getInvalid())).isFalse();
assertThat(SpanId.isValid(SpanId.fromBytes(firstBytes))).isTrue();
assertThat(SpanId.isValid(SpanId.fromBytes(secondBytes))).isTrue();
assertThat(SpanId.isValid(first)).isTrue();
assertThat(SpanId.isValid(second)).isTrue();
assertThat(SpanId.isValid("000000000000z000")).isFalse();
}
@Test
void fromLowerHex() {
assertThat(SpanId.fromBytes(SpanId.asBytes("0000000000000000"))).isEqualTo(SpanId.getInvalid());
assertThat(SpanId.asBytes(SpanId.getInvalid())).isEqualTo(new byte[] {0, 0, 0, 0, 0, 0, 0, 0});
assertThat(SpanId.asBytes("0000000000000061")).isEqualTo(firstBytes);
assertThat(SpanId.asBytes("ff00000000000041")).isEqualTo(secondBytes);
}
@Test
public void toLowerHex() {
assertThat(SpanId.getInvalid()).isEqualTo("0000000000000000");
assertThat(SpanId.fromBytes(firstBytes)).isEqualTo("0000000000000061");
assertThat(SpanId.fromBytes(secondBytes)).isEqualTo("ff00000000000041");
}
@Test
void toFromLong() {
Random random = new Random();

View File

@ -7,7 +7,6 @@ package io.opentelemetry.api.trace;
import static org.assertj.core.api.Assertions.assertThat;
import java.nio.ByteBuffer;
import java.util.Random;
import org.junit.jupiter.api.Test;
@ -17,11 +16,8 @@ class TraceIdTest {
new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'a'};
private static final byte[] secondBytes =
new byte[] {(byte) 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'A'};
private static final String first = TraceId.fromBytes(firstBytes);
private static final String second =
TraceId.fromLongs(
ByteBuffer.wrap(secondBytes).getLong(), ByteBuffer.wrap(secondBytes, 8, 8).getLong());
private static final String first = "00000000000000000000000000000061";
private static final String second = "ff000000000000000000000000000041";
@Test
void invalid() {
@ -44,53 +40,25 @@ class TraceIdTest {
@Test
void testGetRandomTracePart() {
byte[] id = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x00
};
String traceId = TraceId.fromBytes(id);
String traceId = "0102030405060708090a0b0c0d0e0f00";
assertThat(TraceId.getTraceIdRandomPart(traceId)).isEqualTo(0x090A0B0C0D0E0F00L);
}
@Test
void testGetRandomTracePart_NegativeLongRepresentation() {
byte[] id = {
(byte) 0xFF, // force a negative value
0x01,
0x02,
0x03,
0x04,
0x05,
0x06,
0x00,
(byte) 0xFF, // force a negative value
0x0A,
0x0B,
0x0C,
0x0D,
0x0E,
0x0F,
0x00
};
String traceId = TraceId.fromBytes(id);
String traceId = "ff01020304050600ff0a0b0c0d0e0f00";
assertThat(TraceId.highPartAsLong(traceId)).isEqualTo(0xFF01020304050600L);
assertThat(TraceId.lowPartAsLong(traceId)).isEqualTo(0xFF0A0B0C0D0E0F00L);
}
@Test
void fromLowerHex() {
assertThat(TraceId.fromBytes(TraceId.asBytes("00000000000000000000000000000000")))
.isEqualTo(TraceId.getInvalid());
void asBytes() {
assertThat(TraceId.asBytes(TraceId.getInvalid()))
.isEqualTo(new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
assertThat(TraceId.asBytes("00000000000000000000000000000061")).isEqualTo(firstBytes);
assertThat(TraceId.asBytes("ff000000000000000000000000000041")).isEqualTo(secondBytes);
}
@Test
void toLowerHex() {
assertThat(TraceId.getInvalid()).isEqualTo("00000000000000000000000000000000");
assertThat(TraceId.fromBytes(firstBytes)).isEqualTo("00000000000000000000000000000061");
assertThat(TraceId.fromBytes(secondBytes)).isEqualTo("ff000000000000000000000000000041");
}
@Test
void toFromLongs() {
Random random = new Random();

View File

@ -15,14 +15,13 @@ import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.io.BaseEncoding;
import com.google.protobuf.util.Durations;
import com.google.protobuf.util.Timestamps;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.exporter.jaeger.proto.api_v2.Model;
import io.opentelemetry.sdk.resources.Resource;
@ -42,6 +41,7 @@ import org.junit.jupiter.api.Test;
/** Unit tests for {@link Adapter}. */
class AdapterTest {
private static final BaseEncoding hex = BaseEncoding.base16().lowerCase();
private static final String LINK_TRACE_ID = "00000000000000000000000000cba123";
private static final String LINK_SPAN_ID = "0000000000fed456";
private static final String TRACE_ID = "00000000000000000000000000abc123";
@ -73,9 +73,8 @@ class AdapterTest {
// test
Model.Span jaegerSpan = Adapter.toJaeger(span);
assertThat(TraceId.fromBytes(jaegerSpan.getTraceId().toByteArray()))
.isEqualTo(span.getTraceId());
assertThat(SpanId.fromBytes(jaegerSpan.getSpanId().toByteArray())).isEqualTo(span.getSpanId());
assertThat(hex.encode(jaegerSpan.getTraceId().toByteArray())).isEqualTo(span.getTraceId());
assertThat(hex.encode(jaegerSpan.getSpanId().toByteArray())).isEqualTo(span.getSpanId());
assertThat(jaegerSpan.getOperationName()).isEqualTo("GET /api/endpoint");
assertThat(jaegerSpan.getStartTime()).isEqualTo(Timestamps.fromMillis(startMs));
assertThat(Durations.toMillis(jaegerSpan.getDuration())).isEqualTo(duration);
@ -216,8 +215,8 @@ class AdapterTest {
Model.SpanRef spanRef = Adapter.toSpanRef(link);
// verify
assertThat(SpanId.fromBytes(spanRef.getSpanId().toByteArray())).isEqualTo(SPAN_ID);
assertThat(TraceId.fromBytes(spanRef.getTraceId().toByteArray())).isEqualTo(TRACE_ID);
assertThat(hex.encode(spanRef.getSpanId().toByteArray())).isEqualTo(SPAN_ID);
assertThat(hex.encode(spanRef.getTraceId().toByteArray())).isEqualTo(TRACE_ID);
assertThat(spanRef.getRefType()).isEqualTo(Model.SpanRefType.FOLLOWS_FROM);
}
@ -330,8 +329,8 @@ class AdapterTest {
boolean found = false;
for (Model.SpanRef spanRef : jaegerSpan.getReferencesList()) {
if (Model.SpanRefType.FOLLOWS_FROM.equals(spanRef.getRefType())) {
assertThat(TraceId.fromBytes(spanRef.getTraceId().toByteArray())).isEqualTo(LINK_TRACE_ID);
assertThat(SpanId.fromBytes(spanRef.getSpanId().toByteArray())).isEqualTo(LINK_SPAN_ID);
assertThat(hex.encode(spanRef.getTraceId().toByteArray())).isEqualTo(LINK_TRACE_ID);
assertThat(hex.encode(spanRef.getSpanId().toByteArray())).isEqualTo(LINK_SPAN_ID);
found = true;
}
}
@ -342,8 +341,8 @@ class AdapterTest {
boolean found = false;
for (Model.SpanRef spanRef : jaegerSpan.getReferencesList()) {
if (Model.SpanRefType.CHILD_OF.equals(spanRef.getRefType())) {
assertThat(TraceId.fromBytes(spanRef.getTraceId().toByteArray())).isEqualTo(TRACE_ID);
assertThat(SpanId.fromBytes(spanRef.getSpanId().toByteArray())).isEqualTo(PARENT_SPAN_ID);
assertThat(hex.encode(spanRef.getTraceId().toByteArray())).isEqualTo(TRACE_ID);
assertThat(hex.encode(spanRef.getSpanId().toByteArray())).isEqualTo(PARENT_SPAN_ID);
found = true;
}
}

View File

@ -14,6 +14,7 @@ import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import com.google.common.collect.Lists;
import com.google.common.io.BaseEncoding;
import com.google.common.io.Closer;
import io.grpc.ManagedChannel;
import io.grpc.Server;
@ -23,10 +24,8 @@ import io.grpc.stub.StreamObserver;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.exporter.jaeger.proto.api_v2.Collector;
import io.opentelemetry.exporter.jaeger.proto.api_v2.CollectorServiceGrpc;
@ -50,6 +49,7 @@ import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatchers;
class JaegerGrpcSpanExporterTest {
private static final BaseEncoding hex = BaseEncoding.base16().lowerCase();
private static final String TRACE_ID = "00000000000000000000000000abc123";
private static final String SPAN_ID = "0000000000def456";
private static final String SPAN_ID_2 = "0000000000aef789";
@ -125,7 +125,7 @@ class JaegerGrpcSpanExporterTest {
Model.Batch batch = requestCaptor.getValue().getBatch();
assertThat(batch.getSpans(0).getOperationName()).isEqualTo("GET /api/endpoint");
assertThat(SpanId.fromBytes(batch.getSpans(0).getSpanId().toByteArray())).isEqualTo(SPAN_ID);
assertThat(hex.encode(batch.getSpans(0).getSpanId().toByteArray())).isEqualTo(SPAN_ID);
assertThat(
getTagValue(batch.getProcess().getTagsList(), "resource-attr-key")
@ -214,14 +214,12 @@ class JaegerGrpcSpanExporterTest {
if (processTag.isPresent()) {
assertThat(processTag2.isPresent()).isFalse();
assertThat(batch.getSpans(0).getOperationName()).isEqualTo("GET /api/endpoint/1");
assertThat(SpanId.fromBytes(batch.getSpans(0).getSpanId().toByteArray()))
.isEqualTo(SPAN_ID);
assertThat(hex.encode(batch.getSpans(0).getSpanId().toByteArray())).isEqualTo(SPAN_ID);
assertThat(processTag.get().getVStr()).isEqualTo("resource-attr-value-1");
assertThat(batch.getProcess().getServiceName()).isEqualTo("myServiceName1");
} else if (processTag2.isPresent()) {
assertThat(batch.getSpans(0).getOperationName()).isEqualTo("GET /api/endpoint/2");
assertThat(SpanId.fromBytes(batch.getSpans(0).getSpanId().toByteArray()))
.isEqualTo(SPAN_ID_2);
assertThat(hex.encode(batch.getSpans(0).getSpanId().toByteArray())).isEqualTo(SPAN_ID_2);
assertThat(processTag2.get().getVStr()).isEqualTo("resource-attr-value-2");
assertThat(batch.getProcess().getServiceName()).isEqualTo("myServiceName2");
} else {
@ -232,7 +230,7 @@ class JaegerGrpcSpanExporterTest {
private static void verifyBatch(Model.Batch batch) throws Exception {
assertThat(batch.getSpansCount()).isEqualTo(1);
assertThat(TraceId.fromBytes(batch.getSpans(0).getTraceId().toByteArray())).isEqualTo(TRACE_ID);
assertThat(hex.encode(batch.getSpans(0).getTraceId().toByteArray())).isEqualTo(TRACE_ID);
assertThat(batch.getProcess().getTagsCount()).isEqualTo(5);
assertThat(

View File

@ -8,10 +8,8 @@ package io.opentelemetry.exporter.otlp.trace;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.resources.Resource;
@ -47,12 +45,8 @@ public class RequestMarshalState {
private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO =
InstrumentationLibraryInfo.create("name", null);
private static final byte[] TRACE_ID_BYTES =
new byte[] {123, 46, 23, 78, 12, 5, (byte) 180, (byte) 223, 45, 89, 71, 61, 62, 29, 34, 54};
private static final String TRACE_ID = TraceId.fromBytes(TRACE_ID_BYTES);
private static final byte[] SPAN_ID_BYTES =
new byte[] {(byte) 198, (byte) 245, (byte) 213, (byte) 156, 46, 31, 29, 101};
private static final String SPAN_ID = SpanId.fromBytes(SPAN_ID_BYTES);
private static final String TRACE_ID = "7b2e170db4df2d593ddb4ddf2ddf2d59";
private static final String SPAN_ID = "170d3ddb4d23e81f";
private static final SpanContext SPAN_CONTEXT =
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault());

View File

@ -11,10 +11,8 @@ import com.google.protobuf.CodedOutputStream;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.proto.collector.trace.v1.ExportTraceServiceRequest;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
@ -52,11 +50,8 @@ class TraceMarshalerTest {
private static final InstrumentationLibraryInfo INSTRUMENTATION_LIBRARY_INFO =
InstrumentationLibraryInfo.create("name", null);
private static final byte[] TRACE_ID_BYTES =
new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4};
private static final String TRACE_ID = TraceId.fromBytes(TRACE_ID_BYTES);
private static final byte[] SPAN_ID_BYTES = new byte[] {0, 0, 0, 0, 4, 3, 2, 1};
private static final String SPAN_ID = SpanId.fromBytes(SPAN_ID_BYTES);
private static final String TRACE_ID = "00000000000000000000000001020304";
private static final String SPAN_ID = "0000000004030201";
private static final SpanContext SPAN_CONTEXT =
SpanContext.create(

View File

@ -22,11 +22,9 @@ import static org.assertj.core.api.Assertions.assertThat;
import com.google.protobuf.ByteString;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.proto.common.v1.AnyValue;
import io.opentelemetry.proto.common.v1.KeyValue;
@ -42,9 +40,9 @@ import org.junit.jupiter.api.Test;
class SpanAdapterTest {
private static final byte[] TRACE_ID_BYTES =
new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4};
private static final String TRACE_ID = TraceId.fromBytes(TRACE_ID_BYTES);
private static final String TRACE_ID = "00000000000000000000000001020304";
private static final byte[] SPAN_ID_BYTES = new byte[] {0, 0, 0, 0, 4, 3, 2, 1};
private static final String SPAN_ID = SpanId.fromBytes(SPAN_ID_BYTES);
private static final String SPAN_ID = "0000000004030201";
private static final SpanContext SPAN_CONTEXT =
SpanContext.create(TRACE_ID, SPAN_ID, TraceFlags.getSampled(), TraceState.getDefault());

View File

@ -13,7 +13,6 @@ import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.IdGenerator;
@ -148,26 +147,7 @@ class TraceIdRatioBasedSamplerTest {
final Sampler defaultProbability = Sampler.traceIdRatioBased(0.0001);
// This traceId will not be sampled by the Probability Sampler because the last 8 bytes as long
// is not less than probability * Long.MAX_VALUE;
String notSampledTraceId =
TraceId.fromBytes(
new byte[] {
0,
0,
0,
0,
0,
0,
0,
0,
(byte) 0x8F,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF
});
String notSampledTraceId = "00000000000000008fffffffffffffff";
SamplingResult samplingResult1 =
defaultProbability.shouldSample(
invalidParentContext,
@ -179,26 +159,7 @@ class TraceIdRatioBasedSamplerTest {
assertThat(samplingResult1.getDecision()).isEqualTo(SamplingDecision.DROP);
// This traceId will be sampled by the Probability Sampler because the last 8 bytes as long
// is less than probability * Long.MAX_VALUE;
String sampledTraceId =
TraceId.fromBytes(
new byte[] {
(byte) 0x00,
(byte) 0x00,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
(byte) 0xFF,
0,
0,
0,
0,
0,
0,
0,
0
});
String sampledTraceId = "0000ffffffffffff0000000000000000";
SamplingResult samplingResult2 =
defaultProbability.shouldSample(
invalidParentContext,