Java Code Examples for brave.propagation.TraceContextOrSamplingFlags#create()

The following examples show how to use brave.propagation.TraceContextOrSamplingFlags#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GrpcPropagation.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public TraceContextOrSamplingFlags extract(R request) {
  if (!(request instanceof GrpcServerRequest)) return delegate.extract(request);

  Metadata metadata = ((GrpcClientRequest) request).headers;

  // First, check if we are propagating gRPC tags.
  TagsBin tagsBin = metadata.get(GRPC_TAGS_BIN);

  // Next, check to see if there is a gRPC formatted trace context: use it if parsable.
  byte[] bytes = metadata.get(GRPC_TRACE_BIN);
  if (bytes != null) {
    TraceContext maybeContext = TraceContextBinaryFormat.parseBytes(bytes, tagsBin);
    if (maybeContext != null) return TraceContextOrSamplingFlags.create(maybeContext);
  }

  // Finally, try to extract an incoming, non-gRPC trace context. If tags exist, propagate them.
  TraceContextOrSamplingFlags result = delegate.extract(request);
  if (tagsBin == null) return result;
  return result.toBuilder().addExtra(tagsBin).build();
}
 
Example 2
Source File: TracerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void nextSpan_ensuresSampling_whenCreatingNewChild() {
  TraceContext notYetSampled =
    tracer.newTrace().context().toBuilder().sampled(null).build();

  TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(notYetSampled);
  assertThat(tracer.nextSpan(extracted).context().sampled())
    .isTrue();
}
 
Example 3
Source File: TracerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void nextSpan_extractedTraceId() {
  TraceIdContext traceIdContext = TraceIdContext.newBuilder().traceId(1L).build();
  TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(traceIdContext);

  assertThat(tracer.nextSpan(extracted).context().traceId())
    .isEqualTo(1L);
}
 
Example 4
Source File: TracerTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Test public void nextSpan_extractedTraceContext() {
  TraceContextOrSamplingFlags extracted = TraceContextOrSamplingFlags.create(context);

  assertThat(tracer.nextSpan(extracted).context())
    .extracting(TraceContext::traceId, TraceContext::parentId)
    .containsExactly(1L, 2L);
}
 
Example 5
Source File: ITKafkaTracing.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public <R> TraceContext.Extractor<R> extractor(Getter<R, String> getter) {
  return request -> {
    String result = getter.get(request, TRACE_ID);
    if (result == null) return TraceContextOrSamplingFlags.create(SamplingFlags.EMPTY);
    return TraceContextOrSamplingFlags.create(TraceIdContext.newBuilder()
      .traceId(HexCodec.lowerHexToUnsignedLong(result))
      .build());
  };
}
 
Example 6
Source File: XCloudTraceContextExtractor.java    From zipkin-gcp with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a tracing context if the extracted string follows the "x-cloud-trace-context: TRACE_ID"
 * or "x-cloud-trace-context: TRACE_ID/SPAN_ID" format; or the "x-cloud-trace-context:
 * TRACE_ID/SPAN_ID;o=TRACE_TRUE" format and {@code TRACE_TRUE}'s value is {@code 1}.
 */
@Override public TraceContextOrSamplingFlags extract(R request) {
  if (request == null) throw new NullPointerException("request == null");
  TraceContextOrSamplingFlags context = primary.extract(request);
  if (context != TraceContextOrSamplingFlags.EMPTY) return context;

  TraceContextOrSamplingFlags result = TraceContextOrSamplingFlags.EMPTY;

  String xCloudTraceContext = getter.get(request, StackdriverTracePropagation.TRACE_ID_NAME);

  if (xCloudTraceContext != null) {
    String[] tokens = xCloudTraceContext.split("/");

    long[] traceId = convertHexTraceIdToLong(tokens[0]);

    // traceId is null if invalid
    if (traceId != null) {
      long spanId = 0; // 0 indicates no span ID is set by the user
      Boolean traceTrue = null; // null means to defer trace decision to sampler

      // A span ID exists. A TRACE_TRUE flag also possibly exists.
      if (tokens.length >= 2) {
        String[] traceOptionTokens = tokens[1].split(";");

        if (traceOptionTokens.length >= 1
            && !traceOptionTokens[0].isEmpty()) {
          spanId = parseUnsignedLong(traceOptionTokens[0]);
        }

        if (traceOptionTokens.length >= 2) {
          traceTrue = extractTraceTrueFromToken(traceOptionTokens[1]);
        }
      }

      if (spanId == 0) {
        result = TraceContextOrSamplingFlags.create(
            TraceIdContext.newBuilder()
                .traceIdHigh(traceId[0])
                .traceId(traceId[1])
                .sampled(traceTrue)
                .build());
      } else {
        result = TraceContextOrSamplingFlags.create(
            TraceContext.newBuilder()
                .traceIdHigh(traceId[0])
                .traceId(traceId[1])
                .spanId(spanId)
                .sampled(traceTrue)
                .build());
      }
    }
  }

  return result;
}