0

I'm developing an application with 2 microservices: Microservice A sends data to kafka broker and Microservice B consumes records from kafka and stores data into mysql db. My goal is to get the full trace of a request, starting from the Microservice A that receives a POST request, passing through kafka and ending in Microservice B, propagating the opentelemetry traceId generated in the Microservice A into Microservice B logs. Opentelemetry uses W3C standard to propagate the context. I'm able to inject into the kafka record header parameter traceParent the corrent traceId using

@Inject
@Channel("channel-out")
Emitter<MyData> emitter;

...

emitter.send(Message.of(myData, Metadata.of(TracingMetadata.withCurrent(Context.current()))));

on the Microservice A side, but on the Microservice B side I'm not able to get the propagated context. Microservice B consumes record using

@Incoming("channel-in")
public void myFunction(MyData d) {...}

I'm expecting that on the Microservice B side is used the same traceId injected on the traceParent parameter of the kafka record, but if I check the traceId using

Span.current().getSpanContext().getTraceId()

it's all zeros, that is an invalid value for W3C standard.

I'm using Java as language and Quarkus as framework, opentelemetry and smallrye reactive messaging for kafka.

Can anyone help me? Thanks.

1 Answers1

0

TracingMetadata has a method to retrieve metadata from messages Please check this link for documentation, You can get the context from the metadata and call makeCurrent().

You can follow this doc it has a code snippet for the same.

Gaurang
  • 106
  • 7
  • if I try to consume a Message with Incoming annotation on the Microservice B side, I can only get Metadata from Message and not TracingMetadata, so I can't get the previous Context and set it as current. Anyway, I have found a solution: I consume ConsumerRecord and I'm able to get the traceparent (in W3C standard) injected into the record header. Then I create a new SpanContext, wrap it into a Span and then set the Span as current using a Tracer and execute my operations into a Scope. – Matteo Di Lorenzi Nov 16 '22 at 11:48