Java Code Examples for brave.propagation.TraceContext#parentIdString()

The following examples show how to use brave.propagation.TraceContext#parentIdString() . 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: JfrScopeDecorator.java    From brave with Apache License 2.0 6 votes vote down vote up
@Override public Scope decorateScope(@Nullable TraceContext context, Scope scope) {
  if (scope == Scope.NOOP) return scope; // we only scope fields constant in the context

  ScopeEvent event = new ScopeEvent();
  if (!event.isEnabled()) return scope;

  if (context != null) {
    event.traceId = context.traceIdString();
    event.parentId = context.parentIdString();
    event.spanId = context.spanIdString();
  }

  event.begin();

  class JfrCurrentTraceContextScope implements Scope {
    @Override public void close() {
      scope.close();
      event.commit();
    }
  }
  return new JfrCurrentTraceContextScope();
}
 
Example 2
Source File: OkhttpTraceInterceptor.java    From zuihou-admin-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * 将X-B3-TraceId | X-B3-SpanId | X-B3-ParentSpanId添加到请求的头部,再将请求发送出去
 */
@Override
public Response intercept(Chain chain) throws IOException {
    Request request = chain.request();
    TraceContext traceContext = tracer.currentSpan().context();
    //copy all headers to newheaders
    Headers.Builder headers = request.headers().newBuilder();
    //add traceid | spanid | parentspanid to headers
    headers.add(TRACE_ID_NAME, traceContext.traceIdString());
    // headers.add(SPAN_ID_NAME, traceContext.spanIdString());
    //set next spanid
    headers.add(SPAN_ID_NAME, HexCodec.toLowerHex(nextId()));
    String parentId = traceContext.parentIdString();
    if (parentId == null) {
        //set parentid = spanid(root) when null
        parentId = HexCodec.toLowerHex(traceContext.spanId());
    }
    headers.add(PARENT_SPAN_ID_NAME, parentId);
    // headers.add(SAMPLED_NAME,"1");

    //rebuild a new request
    request = request.newBuilder().headers(headers.build()).build();
    Response response = chain.proceed(request);

    //将traceid返回去给调用方,如浏览器发起的请求,可在浏览器看到该信息,方便定位
    Headers.Builder responseHeadersBuilder = response.headers()
            .newBuilder()
            .add(TRACE_ID_NAME, traceContext.traceIdString());
    response = response.newBuilder().headers(responseHeadersBuilder.build()).build();

    return response;
}
 
Example 3
Source File: MutableSpan.java    From brave with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance from the given context, and defaults in the span.
 *
 * <p><em>Note:</em> It is unexpected to have context properties also in the span defaults. The
 * context will win in this case, as opposed to throwing an exception.
 *
 * @since 5.12
 */
public MutableSpan(TraceContext context, @Nullable MutableSpan defaults) {
  this(defaults != null ? defaults : EMPTY);
  if (context == null) throw new NullPointerException("context == null");
  // We don't call the setters as context.*IdString are well formed
  this.traceId = context.traceIdString();
  this.localRootId = context.localRootIdString();
  this.parentId = context.parentIdString();
  this.id = context.spanIdString();
  flags = 0; // don't inherit flags from the span
  if (context.debug()) setDebug();
  if (context.shared()) setShared();
}
 
Example 4
Source File: BaggageFields.java    From brave with Apache License 2.0 4 votes vote down vote up
@Override public String getValue(BaggageField field, TraceContext context) {
  return context.parentIdString();
}