3

I need to set the traceId with an existing Id (we have created some kind of correlation-id from the main origin app) into brave tracer.

I don't want to use the Spring Sleuth/brave created one as I want to make it consistent throughout my different micro-services. I am able to create traces and span and able to send all details into Zipkin. My sample snippet:

import brave.Span;
import brave.Span.Kind;
import brave.Tracer;
import brave.propagation.TraceContext;  

span = this.tracer.nextSpan().name("myservice");
span.kind(Kind.SERVER);
span.tag("path", servletPath).start();

I am using: Spring Cloud 'Greenwich.BUILD-SNAPSHOT' and brave. The whole purpose is to search using correlationId rather than traceId in zipkin ui.

user8479984
  • 451
  • 2
  • 9
  • 23

1 Answers1

1

You will need your own PropagationFactory implementation. Here is the default one: https://github.com/openzipkin/brave/blob/master/brave/src/main/java/brave/propagation/B3Propagation.java

You can create a bean and sleuth should use that instead of this one.

More specifically you will need an implementation with a custom TraceContext.Extractor<C> implementation. This can then pull the trace ID from your header, and add return the appropriate TraceContext. Then it can pass it along using the normal headers. If you'd like to use the same correlation header when sending downstream then you will also have to implement TraceContext.Injector<C>.

  • Thanks Brian. I believe this will allow to add other headers along with the actual ones. I don't want to add any new header instead I would like to update the existing trace header with my correlationId value. – user8479984 Dec 06 '18 at 19:32
  • My solution is what you are looking for, `PropagationFactory` deals with both incoming and outgoing headers, including defining which ones to check and how to write them back out – Brian Devins-Suresh Apr 04 '19 at 18:09
  • Hi All, is this working to set alphanumeric value to be used as TRACE ID's value? Is it expecting only long values to be used for TRACE ID or any customized TRACE ID's value if it is customized ? – Ketan Oct 02 '19 at 19:56