Skip to content

KafkaTemplate is reporting 2 errors to observation on immediate errors (one in callback and one in catched exception from the same callback) #2817

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BartoszSta opened this issue Sep 21, 2023 · 1 comment · Fixed by #2823

Comments

@BartoszSta
Copy link

In what version(s) of Spring for Apache Kafka are you seeing this issue?

3.0.7

Describe the bug

When KafkaTemplate sends message which immediately fails (for example because of TopicAuthorizationException) then it will report two exceptions to observation:

  • one in callback,
  • one in catched exception which is thrown in doSend() method after checking that (sendFuture.isDone()) - original exception is then wrapped with KafkaException and thrown (throw new KafkaException("Send failed", e.getCause())) - this exception is then caught in observeSend method and reported to observation again (but this time wrapped KafkaException is reported)

So now after such 1 failure I see two metric counters increased, one with original exception for example TopicAuthorizationException and another with KafkaException which is not perfect because I had just one fail.

To Reproduce

It is easy to reproduce by sending message to wrongly named topic, assuming auto create topics is possible on kafka:

kafkaTemplate.send("wrong%Topic", "data")

This will cause 2 counters increased: one for InvalidTopicException and KafkaException.

Expected behavior

Only one error (Exception) is reported to observation which results in just one counter increased.

@artembilan artembilan self-assigned this Sep 21, 2023
@artembilan artembilan added this to the 3.1.0-RC1 milestone Sep 21, 2023
artembilan added a commit to artembilan/spring-kafka that referenced this issue Sep 22, 2023
Fixes spring-projects#2817

An observation in `KafkaTemplate` can be marked with error from a `Callback`.
Then `Future` is evaluated and its exception is thrown back to the `observeSend()`.
Here this exception is caught and reported to the observation again.
This creates a second timer in the Micrometer, but with different error tag

* Check for error presence in the `observeSend()` `catch` block and skip
second report

**Cherry-pick to `3.0.x`**
garyrussell pushed a commit that referenced this issue Sep 25, 2023
* GH-2817: KafkaTemplate: No double error timers

Fixes #2817

An observation in `KafkaTemplate` can be marked with error from a `Callback`.
Then `Future` is evaluated and its exception is thrown back to the `observeSend()`.
Here this exception is caught and reported to the observation again.
This creates a second timer in the Micrometer, but with different error tag

* Check for error presence in the `observeSend()` `catch` block and skip
second report

**Cherry-pick to `3.0.x`**

* * Fix import order for Checkstyle rule
garyrussell pushed a commit that referenced this issue Sep 25, 2023
* GH-2817: KafkaTemplate: No double error timers

Fixes #2817

An observation in `KafkaTemplate` can be marked with error from a `Callback`.
Then `Future` is evaluated and its exception is thrown back to the `observeSend()`.
Here this exception is caught and reported to the observation again.
This creates a second timer in the Micrometer, but with different error tag

* Check for error presence in the `observeSend()` `catch` block and skip
second report

**Cherry-pick to `3.0.x`**

* * Fix import order for Checkstyle rule
@pavanvarmab
Copy link

I am facing the same with the custom ProducerListener callback implementation. Exception is raised after invoking the onError() function.

I see onError is invoked in buildCallback() and an Exception is raised after that at sendFuture.get()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants