Coder Social home page Coder Social logo

Comments (5)

codefromthecrypt avatar codefromthecrypt commented on June 28, 2024

from brave-example.

sstong123 avatar sstong123 commented on June 28, 2024

For log data analysis,also need to know the call chains.

from brave-example.

codefromthecrypt avatar codefromthecrypt commented on June 28, 2024

from brave-example.

sstong123 avatar sstong123 commented on June 28, 2024

1). I customized the brave example code to add the parentSpanId as below:

import brave.internal.HexCodec;
import brave.internal.Nullable;
import brave.propagation.CurrentTraceContext;
import brave.propagation.TraceContext;
import org.apache.logging.log4j.ThreadContext;

/**
 * Adds {@linkplain ThreadContext} properties "traceId" , "spanId" and "parentSpanId" when a {@link
 * brave.Tracer#currentSpan() span is current}. These can be used in log correlation.
 */
public final class MyThreadContextCurrentTraceContext extends CurrentTraceContext {
  public static MyThreadContextCurrentTraceContext create() {
    return create(CurrentTraceContext.Default.inheritable());
  }

  public static MyThreadContextCurrentTraceContext create(CurrentTraceContext delegate) {
    return new MyThreadContextCurrentTraceContext(delegate);
  }

  final CurrentTraceContext delegate;

  MyThreadContextCurrentTraceContext(CurrentTraceContext delegate) {
    if (delegate == null) throw new NullPointerException("delegate == null");
    this.delegate = delegate;
  }

  @Override public TraceContext get() {
    return delegate.get();
  }

  @Override public Scope newScope(@Nullable TraceContext currentSpan) {
    final String previousTraceId = ThreadContext.get("traceId");
    final String previousSpanId = ThreadContext.get("spanId");

    if (currentSpan != null) {
      ThreadContext.put("traceId", currentSpan.traceIdString());
      ThreadContext.put("spanId", HexCodec.toLowerHex(currentSpan.spanId()));
      Long parentId= currentSpan.parentId();     
      if(parentId!=null){
    	  String pid=HexCodec.toLowerHex(parentId);
    	  ThreadContext.put("parentSpanId",pid);
      }
      
    } else {
      ThreadContext.remove("traceId");
      ThreadContext.remove("spanId");
      ThreadContext.remove("parentSpanId");
    }

    final Scope scope = delegate.newScope(currentSpan);
    class ThreadContextCurrentTraceContextScope implements Scope {
      @Override public void close() {
        scope.close();
        ThreadContext.put("traceId", previousTraceId);
        ThreadContext.put("spanId", previousSpanId);
      }
    }
    return new ThreadContextCurrentTraceContextScope();
  }
}

2). add the below line in log4j config file:
[%X{traceId}/%X{spanId}/%X{parentSpanId}]

3). after I sent the request to the parent system,I got parentSpanId:""(blank) that is correct,
but after resent the request to parent system for several times(about 10 times), I got the parentSpanId not blank,why this happened?(it should be blank)

from brave-example.

sstong123 avatar sstong123 commented on June 28, 2024

filed in brave

from brave-example.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.