congrats Icon
New! Tabnine Pro 14-day free trial
Start a free trial
Tabnine Logo
TraceAndSpanIdGenerator
Code IndexAdd Tabnine to your IDE (free)

How to use
TraceAndSpanIdGenerator
in
com.nike.wingtips

Best Java code snippets using com.nike.wingtips.TraceAndSpanIdGenerator (Showing top 20 results out of 315)

origin: Nike-Inc/wingtips

/**
 * The given header from the given request as a span ID (or parent span ID), with the generateNewSpanIdIfNotFoundInRequest argument determining whether this method returns null
 * or a new random span ID from {@link TraceAndSpanIdGenerator#generateId()} when the request doesn't contain that header.
 */
protected static String getSpanIdFromRequest(RequestWithHeaders request, String headerName, boolean generateNewSpanIdIfNotFoundInRequest) {
  String spanIdString = getHeaderWithAttributeAsBackup(request, headerName);
  if (spanIdString == null)
    return generateNewSpanIdIfNotFoundInRequest ? TraceAndSpanIdGenerator.generateId() : null;
  return spanIdString;
}
origin: Nike-Inc/wingtips

/**
 * @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());
}
origin: Nike-Inc/wingtips

protected Long nullSafeLong(String lowerHexStr) {
  if (lowerHexStr == null)
    return null;
  return TraceAndSpanIdGenerator.unsignedLowerHexStringToLong(lowerHexStr);
}
origin: Nike-Inc/wingtips

String traceId = generateId();
String spanId = generateId();
String parentId = generateId();
long startTimeEpochMicros = Math.abs(random.nextLong());
long durationNanos = Math.abs(random.nextLong());
assertThat(zipkinSpan.id).isEqualTo(unsignedLowerHexStringToLong(wingtipsSpan.getSpanId()));
assertThat(zipkinSpan.name).isEqualTo(wingtipsSpan.getSpanName());
assertThat(zipkinSpan.parentId).isEqualTo(unsignedLowerHexStringToLong(wingtipsSpan.getParentSpanId()));
assertThat(zipkinSpan.timestamp).isEqualTo(wingtipsSpan.getSpanStartTimeEpochMicros());
assertThat(zipkinSpan.traceId).isEqualTo(unsignedLowerHexStringToLong(wingtipsSpan.getTraceId()));
assertThat(zipkinSpan.duration).isEqualTo(durationMicros);
assertThat(zipkinSpan.binaryAnnotations).contains(tagOneAsAnnotation, tagTwoAsAnnotation);
origin: Nike-Inc/wingtips

@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);
}
origin: Nike-Inc/wingtips

String sanitizedId = TraceAndSpanIdGenerator.longToUnsignedLowerHexString(originalIdAsRawLong);
logger.info(SANITIZED_ID_LOG_MSG, originalId, sanitizedId);
return sanitizedId;
origin: Nike-Inc/wingtips

/**
 * @return A random long pulled from the full 64-bit random search space (as opposed to the 48 bits of randomness you get from
 *          {@link java.util.Random#nextLong()}).
 */
public static long generate64BitRandomLong() {
  byte[] random8Bytes = new byte[8];
  random.nextBytes(random8Bytes);
  return convertBytesToLong(random8Bytes);
}
origin: Nike-Inc/wingtips

@Test
public void generate64BitRandomLong_should_not_generate_duplicate_ids_over_reasonable_number_of_attempts() throws Exception {
  // given
  Set<Long> randomLongs = new HashSet<>();
  int numAttempts = 1000000;
  // when
  for (int i = 0; i < numAttempts; i++) {
    randomLongs.add(TraceAndSpanIdGenerator.generate64BitRandomLong());
  }
  // then
  assertThat(randomLongs.size()).isEqualTo(numAttempts);
}
origin: Nike-Inc/wingtips

String traceId = generateId();
String spanId = generateId();
long startTimeEpochMicros = Math.abs(random.nextLong());
long durationNanos = Math.abs(random.nextLong());
assertThat(zipkinSpan.id).isEqualTo(unsignedLowerHexStringToLong(wingtipsSpan.getSpanId()));
assertThat(zipkinSpan.name).isEqualTo(wingtipsSpan.getSpanName());
assertThat(zipkinSpan.parentId).isNull();
assertThat(zipkinSpan.timestamp).isEqualTo(wingtipsSpan.getSpanStartTimeEpochMicros());
assertThat(zipkinSpan.traceId).isEqualTo(unsignedLowerHexStringToLong(wingtipsSpan.getTraceId()));
assertThat(zipkinSpan.duration).isEqualTo(durationMicros);
assertThat(zipkinSpan.binaryAnnotations).doesNotContain(tagOneAsAnnotation, tagTwoAsAnnotation);
origin: com.nike.wingtips/wingtips-zipkin2

String sanitizedId = TraceAndSpanIdGenerator.longToUnsignedLowerHexString(originalIdAsRawLong);
logger.info(SANITIZED_ID_LOG_MSG, originalId, sanitizedId);
return sanitizedId;
origin: Nike-Inc/wingtips

@Test
public void convertBytesToLong_should_work_correctly_for_known_value() {
  // given: byte[] that maps to known long value
  final long EXPECTED_LONG_VALUE = 4242424242L;
  ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE / 8);
  buffer.putLong(EXPECTED_LONG_VALUE);
  byte[] longAsByteArray = buffer.array();
  assertThat(longAsByteArray.length).isEqualTo(8);
  // when: convertBytesToLong() is called
  long returnVal = TraceAndSpanIdGenerator.convertBytesToLong(longAsByteArray);
  // then: the return value is what we expect
  assertThat(returnVal).isEqualTo(EXPECTED_LONG_VALUE);
}
origin: Nike-Inc/wingtips

traceId = TraceAndSpanIdGenerator.generateId();
spanId = TraceAndSpanIdGenerator.generateId();
origin: Nike-Inc/wingtips

  protected Long nullSafeLong(String lowerHexStr, int index) {
    if (lowerHexStr == null)
      return null;

    return TraceAndSpanIdGenerator.unsignedLowerHexStringToLong(lowerHexStr, index);
  }
}
origin: Nike-Inc/wingtips

@Test(expected = IllegalArgumentException.class)
public void convertBytesToLong_should_explode_if_byte_array_is_less_than_8_bytes() {
  byte[] badByteArray = new byte[]{0, 0, 0, 0, 0, 0, 1};
  assertThat(badByteArray.length).isLessThan(8);
  TraceAndSpanIdGenerator.convertBytesToLong(badByteArray);
  fail("Expected IllegalArgumentException but none was thrown");
}
origin: Nike-Inc/wingtips

/**
 * Similar to {@link #startRequestWithRootSpan(String)} but takes in an optional {@code userId} to populate the returned {@link Span#getUserId()}.
 * If {@code userId} is null then this method is equivalent to calling {@link #startRequestWithRootSpan(String)}. If you have parent span info then you should call
 * {@link #startRequestWithChildSpan(Span, String)} or {@link #startRequestWithSpanInfo(String, String, String, boolean, String, SpanPurpose)} instead).
 * The newly created root span will have a span purpose of {@link SpanPurpose#SERVER}.
 * <p/>
 * <b>WARNING:</b> This wipes out any existing spans on the span stack for this thread and starts fresh, therefore this should only be called at the request's
 * entry point when it's expected that the span stack should be empty. If you need to start a child span in the middle of a request somewhere then you should call
 * {@link #startSubSpan(String, SpanPurpose)} instead.
 *
 * @param spanName - The span name to use for the new span - should never be null.
 * @param userId - The ID of the user that should be associated with the {@link Span} - can be null.
 * @return The new span (which is now also the current one that will be returned by {@link #getCurrentSpan()}).
 */
public Span startRequestWithRootSpan(String spanName, String userId) {
  boolean sampleable = isNextRootSpanSampleable();
  String traceId = TraceAndSpanIdGenerator.generateId();
  return doNewRequestSpan(traceId, null, spanName, sampleable, userId, SpanPurpose.SERVER);
}
origin: com.nike.wingtips/wingtips-zipkin

protected Long nullSafeLong(String lowerHexStr) {
  if (lowerHexStr == null)
    return null;
  return TraceAndSpanIdGenerator.unsignedLowerHexStringToLong(lowerHexStr);
}
origin: Nike-Inc/wingtips

@Test(expected = IllegalArgumentException.class)
public void convertBytesToLong_should_explode_if_byte_array_is_more_than_8_bytes() {
  byte[] badByteArray = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 1};
  assertThat(badByteArray.length).isGreaterThan(8);
  TraceAndSpanIdGenerator.convertBytesToLong(badByteArray);
  fail("Expected IllegalArgumentException but none was thrown");
}
origin: Nike-Inc/wingtips

@Test
public void generateId_should_not_generate_duplicate_ids_over_reasonable_number_of_attempts() throws Exception {
  // given
  Set<String> ids = new HashSet<>();
  int numAttempts = 1000000;
  // when
  for (int i = 0; i < numAttempts; i++) {
    ids.add(TraceAndSpanIdGenerator.generateId());
  }
  // then
  assertThat(ids.size()).isEqualTo(numAttempts);
}
origin: com.nike.wingtips/wingtips-zipkin

  protected Long nullSafeLong(String lowerHexStr, int index) {
    if (lowerHexStr == null)
      return null;

    return TraceAndSpanIdGenerator.unsignedLowerHexStringToLong(lowerHexStr, index);
  }
}
origin: Nike-Inc/wingtips

@Test
public void generateId_should_return_16_char_length_string_that_can_be_parsed_into_a_long_when_interpreted_as_a_64_bit_unsigned_hex_long() {
  Set<Character> charactersFromIds = new HashSet<>();
  for (int i = 0; i < 10000; i++) {
    // given: String ID value generated by generateId()
    final String idVal = TraceAndSpanIdGenerator.generateId();
    // then: that ID has 16 characters and can be interpreted as a 64 bit unsigned hex long and parsed into a Java long primitive
    assertThat(idVal).hasSize(16);
    Throwable parseException = catchThrowable(new ThrowableAssert.ThrowingCallable() {
      @Override
      public void call() throws Throwable {
        new BigInteger(idVal, 16).longValue();
      }
    });
    assertThat(parseException).isNull();
    // Store the characters we see in this ID in a set so we can verify later that we've only ever seen hex-compatible characters.
    for (char c : idVal.toCharArray()) {
      charactersFromIds.add(c);
    }
  }
  // We should have only run into lowercase hex characters, and given how many IDs we generated we should have hit all the lowercase hex characters.
  assertThat(charactersFromIds).containsOnly('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
}
com.nike.wingtipsTraceAndSpanIdGenerator

Javadoc

ID generation class for use with trace IDs, span IDs, and parent span IDs for the Span class. Call the static #generateId() method whenever you need to create a new ID. These IDs are fundamentally 64-bit longs, but they are returned by #generateId() encoded as unsigned and in hexadecimal format. To turn those strings back into Java long primitives you can call #unsignedLowerHexStringToLong(String). We use 64-bit longs to conform to the Google Dapper paper (see http://static.googleusercontent.com/media/research.google.com/en/us/pubs/archive/36356.pdf) and unsigned hex encoding to conform to the ZipKin distributed tracing B3 implementation (see http://zipkin.io/pages/instrumenting.html).

Most used methods

  • generateId
  • longToUnsignedLowerHexString
  • unsignedLowerHexStringToLong
  • convertBytesToLong
    Converts the given 8 bytes to a long value. Implementation for this taken from java.util.UUID#UUID(b
  • generate64BitRandomLong
  • getRandomInstance
    Tries to retrieve and return the SecureRandom with the given implementation using SecureRandom#getIn

Popular in Java

  • Reading from database using SQL prepared statement
  • orElseThrow (Optional)
    Return the contained value, if present, otherwise throw an exception to be created by the provided s
  • getApplicationContext (Context)
  • getContentResolver (Context)
  • Component (java.awt)
    A component is an object having a graphical representation that can be displayed on the screen and t
  • Font (java.awt)
    The Font class represents fonts, which are used to render text in a visible way. A font provides the
  • Set (java.util)
    A Set is a data structure which does not allow duplicate elements.
  • Callable (java.util.concurrent)
    A task that returns a result and may throw an exception. Implementors define a single method with no
  • Options (org.apache.commons.cli)
    Main entry-point into the library. Options represents a collection of Option objects, which describ
  • Reflections (org.reflections)
    Reflections one-stop-shop objectReflections scans your classpath, indexes the metadata, allows you t
  • Sublime Text for Python
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now