/** * @return A newly-generated random 64-bit long encoded as a String <b>UNSIGNED AND IN HEX FORMAT</b> - intended for use as a trace or span ID. * The returned string will have a length of 16 characters (zeroes will be prepended as padding if necessary to * reach a string length of 16). The hex format is necessary to be fully B3 compatible and therefore fully Zipkin compatible * (see <a href="http://zipkin.io/pages/instrumenting.html">http://zipkin.io/pages/instrumenting.html</a>). If you simply want a full 64-bit * random normal *signed* Java long encoded in *decimal* format for other reasons then you can call {@link String#valueOf(long)} and pass in a * long created by {@link #generate64BitRandomLong()}. * <p>You can convert the unsigned hex-encoded longs returned by this method back into normal Java long primitives by passing them to * {@link #unsignedLowerHexStringToLong(String)}. */ public static String generateId() { return longToUnsignedLowerHexString(generate64BitRandomLong()); }
String sanitizedId = TraceAndSpanIdGenerator.longToUnsignedLowerHexString(originalIdAsRawLong); logger.info(SANITIZED_ID_LOG_MSG, originalId, sanitizedId); return sanitizedId;
String sanitizedId = TraceAndSpanIdGenerator.longToUnsignedLowerHexString(originalIdAsRawLong); logger.info(SANITIZED_ID_LOG_MSG, originalId, sanitizedId); return sanitizedId;
@DataProvider(value = { "0000000000000000 | 0", "0000000000000001 | 1", "ffffffffffffffff | 18446744073709551615", "fffffffffffffffe | 18446744073709551614", "7fae59489091369a | 9200389256962455194", "eb5e7aaefeb92b4f | 16960128138740312911", "d2153abe4c047408 | 15138070311469347848", "9041ee0d07d6c72c | 10394851154681317164", "6470a5ce0e9262f4 | 7237466905610707700", "000003c8a251fb93 | 4160251624339", }, splitBy = "\\|") @Test public void longToUnsignedLowerHexString_and_unsignedLowerHexStringToLong_work_as_expected_for_known_values(String actualHexValue, String actualUnsignedDecimalValue) { // given long actualSignedPrimitive = new BigInteger(actualUnsignedDecimalValue).longValue(); // when String calculatedHexValue = TraceAndSpanIdGenerator.longToUnsignedLowerHexString(actualSignedPrimitive); long calculatedPrimitiveValue = TraceAndSpanIdGenerator.unsignedLowerHexStringToLong(actualHexValue); // then assertThat(calculatedHexValue).isEqualTo(actualHexValue); assertThat(calculatedPrimitiveValue).isEqualTo(actualSignedPrimitive); }