Skip to content

Fix KafkaTemplate hiding exceptions when starting observation #3779

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

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix KafkaTemplate hiding exceptions when starting observation
This fixes that exceptions thrown from observation.start() are hidden by KafkaTemplate throwing a new exception due to registering observation error without successfully starting the observation.
Signed-off-by: Christian Fredriksson <[email protected]>
  • Loading branch information
cfredri4 committed Mar 5, 2025
commit 91376de75c9b340fe52bdf4714c492fe26247cf7
Original file line number Diff line number Diff line change
Expand Up @@ -805,8 +805,8 @@ private CompletableFuture<SendResult<K, V>> observeSend(final ProducerRecord<K,
this.observationConvention, DefaultKafkaTemplateObservationConvention.INSTANCE,
() -> new KafkaRecordSenderContext(producerRecord, this.beanName, this::clusterId),
this.observationRegistry);
observation.start();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... Agreed.
I see similar pattern in the ObservedAspect:

            observation.start();
            Observation.Scope scope = observation.openScope();
            try {
                Object result = pjp.proceed();

So, we indeed has to start an observation outside of the try..catch block.

More easier to understand how that supposed to be used is in the well-known Observation.observe() implementation:

    default void observe(Runnable runnable) {
        start();
        try (Scope scope = openScope()) {
            runnable.run();
        }
        catch (Throwable error) {
            error(error);
            throw error;
        }
        finally {
            stop();
        }
    }

try {
observation.start();
try (Observation.Scope ignored = observation.openScope()) {
return doSend(producerRecord, observation);
}
Expand Down