spring-cloud / spring-cloud-stream

Framework for building Event-Driven Microservices
http://cloud.spring.io/spring-cloud-stream
Apache License 2.0
969 stars 595 forks source link

Observation tracing propagation is not working for consumers in bath mode #2926

Closed ferblaca closed 2 months ago

ferblaca commented 3 months ago

Describe the issue Distributed traceability does not seem to be working properly for Kafka consumers in batch mode.

To Reproduce Steps to reproduce the behavior:

  1. Start up the project located in https://github.com/ferblaca/demoKafkaBatch/tree/batch-tracing-observation
  2. On start-up, a list of messages to a topic is produced.
  3. Batch consumers consume the messages
  4. Access the url http://localhost:9411/zipkin/traces
  5. Verify that no traces are displayed for the reception of messages.

Version of the framework

Expected behavior

Additional context For consumers who are not in batch mode it works correctly.

sobychacko commented 3 months ago

@ferblaca We will look into it. Thanks for the sample.

sobychacko commented 3 months ago

@ferblaca This is by design and works as expected. Spring for Apache Kafka does not support tracing on batch listeners; it is only supported for record listeners. This is because, in a batch listener, the received records could be from multiple topics/partitions and from multiple producers where adding tracing information was optional. Since there may not be any correlations between records in the batch, the framework cannot make any assumptions about tracing them, such as providing them as a single trace ID, etc. If you convert the type signature of your listener as Message<List<String>>, you can then get a header called kafka_batchConvertedHeaders, which contains a list with the same number of entries as your payload. This list has a Map that contains the tracing headers (traceparent). However, it is up to the application to iterate over this properly and start an observation; the framework cannot do that. On the other hand, if you end up doing this, then why can't you consume the records in record mode and receive the implicit tracing capabilities available through the framework? CC @artembilan for any further insights.

ferblaca commented 3 months ago

Ok @sobychacko , It would be great to indicate this part in the documentation to avoid confusion.

Thank you very much!