Restructure API in trace/span id classes, no changes just move code around (#2672)

Signed-off-by: Bogdan Drutu <bogdandrutu@gmail.com>
This commit is contained in:
Bogdan Drutu 2021-02-02 14:12:05 -08:00 committed by GitHub
parent 2387287f31
commit 36adb5225c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 54 deletions

View File

@ -46,22 +46,6 @@ public final class SpanId {
return INVALID;
}
/** Generate a valid {@link SpanId} from the given long value. */
public static String fromLong(long id) {
char[] result = getTemporaryBuffer();
BigendianEncoding.longToBase16String(id, result, 0);
return new String(result);
}
private static char[] getTemporaryBuffer() {
char[] chars = charBuffer.get();
if (chars == null) {
chars = new char[HEX_SIZE];
charBuffer.set(chars);
}
return chars;
}
/**
* Returns a {@code SpanId} built from a lowercase base16 representation.
*
@ -77,6 +61,23 @@ public final class SpanId {
return BigendianEncoding.bytesFromBase16(src, srcOffset, HEX_SIZE);
}
/** Encode the bytes as base-16 (hex), padded with '0's on the left. */
public static String bytesToHex(byte[] spanId) {
return BigendianEncoding.toLowerBase16(spanId);
}
/** Generate a valid {@link SpanId} from the given long value. */
public static String fromLong(long id) {
char[] result = getTemporaryBuffer();
BigendianEncoding.longToBase16String(id, result, 0);
return new String(result);
}
/** Convert the the given hex spanId into a long representation. */
public static long asLong(CharSequence src) {
return BigendianEncoding.longFromBase16String(src, 0);
}
/**
* Returns whether the span identifier is valid. A valid span identifier is an 8-byte array with
* at least one non-zero byte.
@ -89,13 +90,12 @@ public final class SpanId {
&& BigendianEncoding.isValidBase16String(spanId);
}
/** Encode the bytes as base-16 (hex), padded with '0's on the left. */
public static String bytesToHex(byte[] spanId) {
return BigendianEncoding.toLowerBase16(spanId);
private static char[] getTemporaryBuffer() {
char[] chars = charBuffer.get();
if (chars == null) {
chars = new char[HEX_SIZE];
charBuffer.set(chars);
}
/** Convert the the given hex spanId into a long representation. */
public static long asLong(CharSequence src) {
return BigendianEncoding.longFromBase16String(src, 0);
return chars;
}
}

View File

@ -46,36 +46,6 @@ public final class TraceId {
return INVALID;
}
/**
* Constructs a {@code TraceId} whose representation is specified by two long values representing
* the lower and higher parts.
*
* <p>There is no restriction on the specified values, other than the already established validity
* rules applying to {@code TraceId}. Specifying 0 for both values will effectively make the new
* {@code TraceId} invalid.
*
* <p>This is equivalent to calling {@link #bytesToHex(byte[])} with the specified values stored
* as big-endian.
*
* @param idHi the higher part of the {@code TraceId}.
* @param idLo the lower part of the {@code TraceId}.
*/
public static String fromLongs(long idHi, long idLo) {
char[] chars = getTemporaryBuffer();
BigendianEncoding.longToBase16String(idHi, chars, 0);
BigendianEncoding.longToBase16String(idLo, chars, 16);
return new String(chars);
}
private static char[] getTemporaryBuffer() {
char[] chars = charBuffer.get();
if (chars == null) {
chars = new char[HEX_SIZE];
charBuffer.set(chars);
}
return chars;
}
/**
* Returns a {@code TraceId} built from a lowercase base16 representation.
*
@ -117,6 +87,27 @@ public final class TraceId {
return new String(chars);
}
/**
* Constructs a {@code TraceId} whose representation is specified by two long values representing
* the lower and higher parts.
*
* <p>There is no restriction on the specified values, other than the already established validity
* rules applying to {@code TraceId}. Specifying 0 for both values will effectively make the new
* {@code TraceId} invalid.
*
* <p>This is equivalent to calling {@link #bytesToHex(byte[])} with the specified values stored
* as big-endian.
*
* @param idHi the higher part of the {@code TraceId}.
* @param idLo the lower part of the {@code TraceId}.
*/
public static String fromLongs(long idHi, long idLo) {
char[] chars = getTemporaryBuffer();
BigendianEncoding.longToBase16String(idHi, chars, 0);
BigendianEncoding.longToBase16String(idLo, chars, 16);
return new String(chars);
}
/**
* Returns the rightmost 8 bytes of the trace-id as a long value. This is used in
* ProbabilitySampler.
@ -138,4 +129,13 @@ public final class TraceId {
public static long traceIdLowBytesAsLong(CharSequence traceId) {
return BigendianEncoding.longFromBase16String(traceId, BigendianEncoding.LONG_BASE16);
}
private static char[] getTemporaryBuffer() {
char[] chars = charBuffer.get();
if (chars == null) {
chars = new char[HEX_SIZE];
charBuffer.set(chars);
}
return chars;
}
}