Brave 4.6
Brave 4.6 adds Kafka 0.11.0.0 trace instrumentation and lets you be more flexible with thread locals.
Kafka Tracing
Message Tracing is routinely asked for by users who want to see relationships between producers and consumers.
With significant effort by @ImFlog and supported by review by @dgrabows and @devinsba, you can now trace Kafka message flows with Brave. Here's the README, if you'd like to jump right in.
KafkaTracing
offers decorators for your producers and consumers, doing the heavy lifting for you.
To use the producer simply wrap it like this :
Producer<K, V> producer = new KafkaProducer<>(settings);
TracingProducer<K, V> tracingProducer = kafkaTracing.producer(producer);
tracingProducer.send(new ProducerRecord<K, V>("my-topic", key, value));
Same goes for the consumer :
Consumer<K, V> consumer = new KafkaConsumer<>(settings);
TracingConsumer<K, V> tracingConsumer = kafkaTracing.consumer(consumer);
tracingConsumer.poll(10);
The above will report data to Zipkin when messages are sent or consumed (even in bulk). Unlike RPC, message processing is decoupled from consumption. For example, messages are often consumed in bulk and processing may happen later or never.
When you are ready to process a message, you can continue the trace like this:
Span forProcessor = kafkaTracing.joinSpan(record);
Non-inheriting default context
Those using CurrentTraceContext.Default
, may be aware that it uses an inheritable thread local.
This exists for historical reasons around convenience. However, there are problems with it, such as the inability to cleanup data. You can create CurrentTraceContext.Default
without inheritance, and without writing your own.