/** Returns the hex representation of the span's ID */ public String spanIdString() { String r = spanIdString; if (r == null) { r = spanIdString = toLowerHex(spanId); } return r; }
static long tryParse16HexCharacters(CharSequence lowerHex, int index, int end) { int endIndex = index + 16; if (endIndex > end) return 0L; return HexCodec.lenientLowerHexToUnsignedLong(lowerHex, index, endIndex); }
/** * Parses a 1 to 32 character lower-hex string with no prefix into an unsigned long, tossing any * bits higher than 64. */ public static long lowerHexToUnsignedLong(CharSequence lowerHex) { int length = lowerHex.length(); if (length < 1 || length > 32) throw isntLowerHexLong(lowerHex); // trim off any high bits int beginIndex = length > 16 ? length - 16 : 0; return lowerHexToUnsignedLong(lowerHex, beginIndex); }
/** Returns the hex representation of the span's trace ID */ public String traceIdString() { String r = traceIdString; if (r == null) { if (traceIdHigh != 0) { char[] result = new char[32]; writeHexLong(result, 0, traceIdHigh); writeHexLong(result, 16, traceId); r = new String(result); } else { r = toLowerHex(traceId); } traceIdString = r; } return r; }
/** * Parses a 16 character lower-hex string with no prefix into an unsigned long, starting at the * specified index. */ public static long lowerHexToUnsignedLong(CharSequence lowerHex, int index) { int endIndex = Math.min(index + 16, lowerHex.length()); long result = lenientLowerHexToUnsignedLong(lowerHex, index, endIndex); if (result == 0) throw isntLowerHexLong(lowerHex); return result; }
/** Returns {@code $traceId} */ @Override public String toString() { boolean traceHi = traceIdHigh != 0; char[] result = new char[traceHi ? 32 : 16]; int pos = 0; if (traceHi) { writeHexLong(result, pos, traceIdHigh); pos += 16; } writeHexLong(result, pos, traceId); return new String(result); }
/** Writes 35 characters representing the input trace ID to the buffer at the given offset */ static void writeRoot(TraceContext context, char[] result, int offset) { result[offset] = '1'; // version result[offset + 1] = '-'; // delimiter long high = context.traceIdHigh(); writeHexByte(result, offset + 2, (byte) ((high >>> 56L) & 0xff)); writeHexByte(result, offset + 4, (byte) ((high >>> 48L) & 0xff)); writeHexByte(result, offset + 6, (byte) ((high >>> 40L) & 0xff)); writeHexByte(result, offset + 8, (byte) ((high >>> 32L) & 0xff)); result[offset + 10] = '-'; writeHexByte(result, offset + 11, (byte) ((high >>> 24L) & 0xff)); writeHexByte(result, offset + 13, (byte) ((high >>> 16L) & 0xff)); writeHexByte(result, offset + 15, (byte) ((high >>> 8L) & 0xff)); writeHexByte(result, offset + 17, (byte) (high & 0xff)); writeHexLong(result, offset + 19, context.traceId()); }
/** Inspired by {@code okio.Buffer.writeLong} */ public static void writeHexLong(char[] data, int pos, long v) { writeHexByte(data, pos + 0, (byte) ((v >>> 56L) & 0xff)); writeHexByte(data, pos + 2, (byte) ((v >>> 48L) & 0xff)); writeHexByte(data, pos + 4, (byte) ((v >>> 40L) & 0xff)); writeHexByte(data, pos + 6, (byte) ((v >>> 32L) & 0xff)); writeHexByte(data, pos + 8, (byte) ((v >>> 24L) & 0xff)); writeHexByte(data, pos + 10, (byte) ((v >>> 16L) & 0xff)); writeHexByte(data, pos + 12, (byte) ((v >>> 8L) & 0xff)); writeHexByte(data, pos + 14, (byte) (v & 0xff)); }
@Test public void verifyRoundTrip_128BitTrace() throws Exception { String high64Bits = "463ac35c9f6413ad"; String low64Bits = "48485a3953bb6124"; inject(map, high64Bits + low64Bits, null, low64Bits, true, null); verifyRoundTrip(TraceContextOrSamplingFlags.create(rootSpan.toBuilder() .traceIdHigh(HexCodec.lowerHexToUnsignedLong(high64Bits)) .traceId(HexCodec.lowerHexToUnsignedLong(low64Bits)) .spanId(HexCodec.lowerHexToUnsignedLong(low64Bits)).build())); }
/** Returns the hex representation of the span's trace ID */ public String traceIdString() { String r = traceIdString; if (r == null) { if (traceIdHigh != 0) { char[] result = new char[32]; writeHexLong(result, 0, traceIdHigh); writeHexLong(result, 16, traceId); r = new String(result); } else { r = toLowerHex(traceId); } traceIdString = r; } return r; }
/** Returns {@code $traceId/$spanId} */ @Override public String toString() { boolean traceHi = traceIdHigh != 0; char[] result = new char[((traceHi ? 3 : 2) * 16) + 1]; // 2 ids and the delimiter int pos = 0; if (traceHi) { writeHexLong(result, pos, traceIdHigh); pos += 16; } writeHexLong(result, pos, traceId); pos += 16; result[pos++] = '/'; writeHexLong(result, pos, spanId); return new String(result); }
/** * Parses a 16 character lower-hex string with no prefix into an unsigned long, starting at the * specified index. */ public static long lowerHexToUnsignedLong(CharSequence lowerHex, int index) { int endIndex = Math.min(index + 16, lowerHex.length()); long result = lenientLowerHexToUnsignedLong(lowerHex, index, endIndex); if (result == 0) throw isntLowerHexLong(lowerHex); return result; }
/** Inspired by {@code okio.Buffer.writeLong} */ public static void writeHexLong(char[] data, int pos, long v) { writeHexByte(data, pos + 0, (byte) ((v >>> 56L) & 0xff)); writeHexByte(data, pos + 2, (byte) ((v >>> 48L) & 0xff)); writeHexByte(data, pos + 4, (byte) ((v >>> 40L) & 0xff)); writeHexByte(data, pos + 6, (byte) ((v >>> 32L) & 0xff)); writeHexByte(data, pos + 8, (byte) ((v >>> 24L) & 0xff)); writeHexByte(data, pos + 10, (byte) ((v >>> 16L) & 0xff)); writeHexByte(data, pos + 12, (byte) ((v >>> 8L) & 0xff)); writeHexByte(data, pos + 14, (byte) (v & 0xff)); }
@Test public void verifyRoundTrip_128BitTrace() throws Exception { String high64Bits = "463ac35c9f6413ad"; String low64Bits = "48485a3953bb6124"; inject(map, high64Bits + low64Bits, null, low64Bits, true, null); verifyRoundTrip(TraceContextOrSamplingFlags.create(rootSpan.toBuilder() .traceIdHigh(HexCodec.lowerHexToUnsignedLong(high64Bits)) .traceId(HexCodec.lowerHexToUnsignedLong(low64Bits)) .spanId(HexCodec.lowerHexToUnsignedLong(low64Bits)).build())); }
/** Returns the hex representation of the span's parent ID */ @Nullable public String parentIdString() { String r = parentIdString; if (r == null && parentId != 0L) { r = parentIdString = toLowerHex(parentId); } return r; }
/** Parses the parent id from the input string. Returns true if the ID was missing or valid. */ final <C, K> boolean parseParentId(Propagation.Getter<C, K> getter, C carrier, K key) { String parentIdString = getter.get(carrier, key); if (parentIdString == null) return true; // absent parent is ok int length = parentIdString.length(); if (invalidIdLength(key, length, 16)) return false; parentId = lenientLowerHexToUnsignedLong(parentIdString, 0, length); if (parentId != 0) return true; maybeLogNotLowerHex(parentIdString); return false; }
/** Inspired by {@code okio.Buffer.writeLong} */ public static String toLowerHex(long v) { char[] data = new char[16]; writeHexLong(data, 0, v); return new String(data); }
/** * Parses a 1 to 32 character lower-hex string with no prefix into an unsigned long, tossing any * bits higher than 64. */ public static long lowerHexToUnsignedLong(CharSequence lowerHex) { int length = lowerHex.length(); if (length < 1 || length > 32) throw isntLowerHexLong(lowerHex); // trim off any high bits int beginIndex = length > 16 ? length - 16 : 0; return lowerHexToUnsignedLong(lowerHex, beginIndex); }
/** Returns the hex representation of the span's local root ID */ @Nullable public String localRootIdString() { String r = localRootIdString; if (r == null && localRootId != 0L) { r = localRootIdString = toLowerHex(localRootId); } return r; }
/** Parses the span id from the input string. Returns true if the ID is valid. */ final <C, K> boolean parseSpanId(Propagation.Getter<C, K> getter, C carrier, K key) { String spanIdString = getter.get(carrier, key); if (isNull(key, spanIdString)) return false; int length = spanIdString.length(); if (invalidIdLength(key, length, 16)) return false; spanId = lenientLowerHexToUnsignedLong(spanIdString, 0, length); if (spanId == 0) { maybeLogNotLowerHex(spanIdString); return false; } return true; }