I use spring boot 3. After sending message to kafka, using spring cloud stream, I would like to log offset and partition of sent message with the traceId from the thread which sent the message.
Here's code:
build.gradle
contains next dependencies:
implementation "io.micrometer:micrometer-registry-prometheus"
implementation 'io.micrometer:micrometer-tracing'
implementation 'io.micrometer:micrometer-tracing-bridge-brave'
application.yml producer contains recordMetadataChannel: meta
and spring.cloud.stream.kafka.binder.enable-observation: true
And configuration class contains:
@Bean
public MessageChannel meta() {
return new DirectChannel();
}
@Bean
public IntegrationFlow logMeta() {
return f -> f.channel("meta")
.handle((payload, headers) -> {
log.info("Log metadata from headers")
return payload;
});
}
While I'm able to fetch the metadata, I would like to log it with traceId and spanId in MDC. The Kafka headers obtained in IntegrationFlow don't contain traceparent
header, though I can see them in the message, which is sent further.
Is there any way to receive the traceId for logging?