Java Code Examples for brave.propagation.TraceContext#Extractor

The following examples show how to use brave.propagation.TraceContext#Extractor . 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: DubboClientHandler.java    From brave-instrumentation-dubbo with Apache License 2.0 5 votes vote down vote up
/**
 * Starts the client span after assigning it a name and tags.
 */
public Span handleSend(TraceContext.Extractor extractor, TraceContext.Injector injector) {
  Span span = nextSpan(extractor.extract(RpcContext.getContext().getAttachments()));
  injector.inject(span.context(), RpcContext.getContext().getAttachments());
  if (span.isNoop()) return span;
  span.kind(Span.Kind.CLIENT);
  Tracer.SpanInScope ws = tracer.withSpanInScope(span);
  try {
    parser.request(adapter, RpcContext.getContext(), span);
  } finally {
    ws.close();
  }
  return span.start();
}
 
Example 2
Source File: DubboServerHandler.java    From brave-instrumentation-dubbo with Apache License 2.0 5 votes vote down vote up
public Span handleReceive(TraceContext.Extractor extractor) {
  Span span = nextSpan(extractor.extract(RpcContext.getContext().getAttachments()));
  if (span.isNoop()) return span;
  span.kind(Span.Kind.SERVER);
  Tracer.SpanInScope ws = tracer.withSpanInScope(span);
  try {
    parser.request(adapter, RpcContext.getContext(), span);
  } finally {
    ws.close();
  }
  return span.start();
}
 
Example 3
Source File: TraceActivityMonitor.java    From mdw with Apache License 2.0 5 votes vote down vote up
/**
 * Resumes or forks
 */
private void startSpan(ActivityRuntimeContext context) {
    Tracing tracing = TraceHelper.getTracing("mdw-activity");
    Tracer tracer = tracing.tracer();
    Span span = tracer.currentSpan();
    if (span == null) {
        // async brave server if b3 requestHeaders populated (subspan)
        Object requestHeaders = context.getValue("requestHeaders");
        if (requestHeaders instanceof Map) {
            Map<?, ?> headers = (Map<?, ?>) requestHeaders;
            if (headers.containsKey("x-b3-traceid")) {
                TraceContext.Extractor<Map<?, ?>> extractor =
                        tracing.propagation().extractor((map, key) -> {
                            Object val = map.get(key.toLowerCase());
                            return val == null ? null : val.toString();
                        });
                TraceContextOrSamplingFlags extracted = extractor.extract(headers);
                span = extracted.context() != null
                        ? tracer.joinSpan(extracted.context())
                        : tracer.nextSpan(extracted);
                span.name("m" + context.getMasterRequestId()).kind(Span.Kind.SERVER);
                span = tracer.newChild(span.context()).name("p" + context.getProcessInstanceId());
            }
        }
    }
    if (span == null) {
        // create a new root span for async start
        span = tracer.nextSpan().name("a" + context.getActivityInstanceId());
    }
    span.start().flush(); // async
    Tracer.SpanInScope spanInScope = tracer.withSpanInScope(span);
    context.getRuntimeAttributes().put(Tracer.SpanInScope.class, spanInScope);
}
 
Example 4
Source File: PropagationTest.java    From brave with Apache License 2.0 5 votes vote down vote up
@Override public void accept(Propagation<?> propagation) {
  TraceContext.Injector<Map<Object, String>> injector = propagation.injector(Map::put);
  TraceContext.Extractor<Map<Object, String>> extractor = propagation.extractor(Map::get);

  TraceContext ctx = TraceContext.newBuilder().traceId(1L).spanId(2L).sampled(false).build();
  Map<Object, String> map = new LinkedHashMap<>();
  injector.inject(ctx, map);

  assertThat(extractor.extract(map).context())
    .isEqualToIgnoringGivenFields(ctx, "traceIdString", "spanIdString");
}
 
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());
  };
}