Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to log the parentSpanId? #18

Closed
sstong123 opened this issue Nov 10, 2017 · 5 comments
Closed

How to log the parentSpanId? #18

sstong123 opened this issue Nov 10, 2017 · 5 comments

Comments

@sstong123
Copy link

seems below config parentSpanId doesn't work:
%d{ABSOLUTE} [%X{traceId}/%X{spanId}/%X{parentSpanId}] %-5p [%t] %C{2} - %m%n

@codefromthecrypt
Copy link
Member

codefromthecrypt commented Nov 10, 2017 via email

@sstong123
Copy link
Author

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

@codefromthecrypt
Copy link
Member

codefromthecrypt commented Nov 21, 2017 via email

@sstong123
Copy link
Author

sstong123 commented Nov 21, 2017

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)

@sstong123
Copy link
Author

filed in brave

codefromthecrypt pushed a commit that referenced this issue Oct 8, 2020
- Add Maven wrapper so that a user can start playing with the example
  even without local Maven installation.
- Update Armeria from 0.96.0 to 0.97.0
- Update Brave from 5.9.0 to 5.9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants