0

I have a service A that creates an email and sends it to a customer. The customer will receive the email and will, eventually, click on the link in the body to trigger service B.

How can I correlate two different and completely isolated services that are part of the same business process with sleuth?

Should I leave the span "opened" or is there a way to "embed" the trace id somehow on the email?

pgs
  • 3
  • 5

1 Answers1

0

You can use asynchronous communication (http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html#_asynchronous_communication) for example via a trace representation of the ExecutorService called the TraceableExecutorService (http://cloud.spring.io/spring-cloud-sleuth/spring-cloud-sleuth.html#_executor_executorservice_and_scheduledexecutorservice). You emit a completable future that will process the data in a separate thread. At some point you block and then you can retrieve the data. The trace representation of the ExecutorService will take care of passing of tracing data.

UPDATE: If however, these are completely two separate processes then I'd close the span and create a completely separate span the moment someone clicks on the link. You should never leave spans explicitly open. What will bind the 2 processes will be the trace id. Zipkin doesn't yet support these long living tasks in the best possible way from the point of view of the UI but there's some work in progress going on to improve it (via so-called linked spans)

Marcin Grzejszczak
  • 10,624
  • 1
  • 16
  • 32
  • Exactly, these are two completely separated services, therefore, it makes total sense that what will bind them is the trace id. The only thing is that I'm not sure how to pass the trace id since the second service is randomly triggered in time by the user from the link on the email's body. This is not a typical microservices paradigm where you have services talking with each other or that are reacting on data pipes. These services are totally unrelated but they should display the same trace id in the logs. – pgs Jun 06 '17 at 08:22
  • You have to store this info somewhere - there's no other way, unfortunately :/ You can pass the (hashed?) traceid maybe in the email link? – Marcin Grzejszczak Jun 06 '17 at 08:23
  • That was my first guess. In this case I really need to store or pass it around. Would you recommend then to use traces and spans declaratively throughout the code instead of relying on annotations and executors? – pgs Jun 06 '17 at 08:29
  • You'll have to somehow "continue" the trace. So you'll need to build a new span manually I guess. Generate the `spanid`, set the stored value of the `traceid` and it would be good to also set the value of the `parentspanid`. Like I said we don't support this approach out of the box so you'll have to create that span (let' call it `X`) and then run `tracer.continueSpan(X)` to set it in the current context. Remember to detach it afterwards! – Marcin Grzejszczak Jun 06 '17 at 08:32