feat(kafka-clients): handle readonly record headers on context injection through producer wrapper#17530
Conversation
…ion through producer wrapper
...java/io/opentelemetry/instrumentation/kafkaclients/v2_6/internal/KafkaProducerTelemetry.java
Outdated
Show resolved
Hide resolved
| @Test | ||
| @SuppressWarnings({"unchecked"}) | ||
| void testProducerHandlesReadOnlyHeaders() { | ||
| Producer<String, String> producer = |
There was a problem hiding this comment.
alternatively could use a mock, but this is fine too
There was a problem hiding this comment.
I guess it may be more readable with a mock, I just went with this approach because it mirrors the setup of the existing tests in this same file. Your call though, I'd happily change it to use a mock if preferred!
|
@laurit while working on implementing your suggestion I dove a bit deeper into some other parts and realized some error handling we might want to add. For the producer wrapper, at:
So I'm thinking we may want to add some additional error handling to explicitly end the span in case of failure so that the span is always properly ended. WDYT? |
After #17231 when looking at the code again I noticed some additional exception handling in the interception flow. Talking about the interception flow that has the
try/catcharoundpropagator.inject()at:opentelemetry-java-instrumentation/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/main/java/io/opentelemetry/instrumentation/kafkaclients/v2_6/internal/KafkaProducerTelemetry.java
Lines 64 to 71 in bd6e305
I found that, as the comment and log statement there allude to, this is for when a record's headers have already been marked
readonly(which happens after serialization throughKafkaProducer.doSend()). So thistry/catchshould be there, in case of retries for example, to gracefully handle theIllegalStateExceptionthat is thrown when trying to inject headers into a readonlyRecordHeadersinstance.Mea culpa for missing this on the previous PR.