0

We are trying to add tracing to micro services so it can viewed in the google Stackdriver UI. We are using Java Springboot apps deployed into Kubernetes containers, each microservices communicates over http. We’ve seen that there is Sleuth and Zipkin which if we move our RestTemplate to a bean will work. However we don’t really want to have to deploy a zipkin pod in each of our containers or create new zipkin collector pods. Ideally we would like to get this working using just the google cloud tracing sdk with using sleuth/zipkin. Playing around with the sdk we are able to get data into Stackdriver using the google cloud grpc library which just sends the data directly from the application into Stackdriver. The problem we have now is that we can send the trace id to a downstream micro service but we cannot seem to find a way to create a new span on the same trace id, it always creates a new one. I can’t seem to find any documentation on how to do this. Surely what we are doing is what this library was build for? Any pointers help on this would be great.

Adding a bit more info......

I cannot supply actual code because this is my problem, I can't actually find what I want to do.
Let me try to explain with a bit of code/pseudo code. So lets assume this scenario, I have 3 microservices, A, B and C.

Microservice A (top level where trace is created)
  TraceContext context = tracer.startSpan("myspan1");
  TraceId traceId = context.getHandle().getCurrentSpanContext().getTraceId();
    Call Microservice B over http passing traceId in the B3-X-TraceId header
  tracer.endSpan(context);

MicroService B  
  Read B3-X-TraceId from header
  So at this point I want to call Microservice C but I want to create a new span on the same trace
  I just do not see any mechanism to do this and this is where I'm stuck.
  This is what I want to do in pseudo code 
    TraceContext context = tracer.startSpan("myspan2");
    attach the trace id that came in the header to the context
      Call Microservice C over http passing traceId in the B3-X-TraceId header
    tracer.endSpan(context);

Hope this makes sense in what I'm trying to do.

2 Answers2

0

The code you've provided is not related to Sleuth but opentracing. In Sleuth you would call Tracer.createSpan("name") and that way a child span od your current trace would be created.

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • Hi Marcin, Yes exactly, I do not want to use Sleuth as our current system does not create the RestTemplate as a Bean. – Marvin Wright Dec 28 '17 at 14:54
  • you can add the `TraceRestTemplateInterceptor` https://github.com/spring-cloud/spring-cloud-sleuth/blob/v1.3.0.RELEASE/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/instrument/web/client/TraceRestTemplateInterceptor.java manually to the non-bean rest template – Marcin Grzejszczak Dec 28 '17 at 14:55
0

I've also managed to get it working by using just the cloud trace api by doing this before I create a span.

SpanContext spanContext = Trace.getSpanContextFactory().fromHeader(traceId);
Trace.getSpanContextHandler().attach(spanContext);

Not sure if there is a negative of doing this.