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

Conversation

cfredri4
Copy link
Contributor

@cfredri4 cfredri4 commented Mar 5, 2025

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]

@cfredri4
Copy link
Contributor Author

cfredri4 commented Mar 5, 2025

2025-03-03 22:04:37.235+0000 [foo] ERROR foo.Foo - [run] Exception occurred
java.lang.IllegalStateException: Span wasn't started - an observation must be started (not only created)
at io.micrometer.tracing.handler.TracingObservationHandler.getRequiredSpan(TracingObservationHandler.java:205)
at io.micrometer.tracing.handler.PropagatingSenderTracingObservationHandler.onError(PropagatingSenderTracingObservationHandler.java:94)
at io.micrometer.tracing.handler.PropagatingSenderTracingObservationHandler.onError(PropagatingSenderTracingObservationHandler.java:35)
at io.micrometer.observation.ObservationHandler$FirstMatchingCompositeObservationHandler.onError(ObservationHandler.java:157)
at io.micrometer.observation.SimpleObservation.notifyOnError(SimpleObservation.java:221)
at io.micrometer.observation.SimpleObservation.error(SimpleObservation.java:139)
at org.springframework.kafka.core.KafkaTemplate.observeSend(KafkaTemplate.java:811)
at org.springframework.kafka.core.KafkaTemplate.send(KafkaTemplate.java:608)
...

@@ -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();
        }
    }

Copy link
Member

@artembilan artembilan left a comment

Choose a reason for hiding this comment

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

Signed-off-by: cfredri4 [email protected]

While email might be real, we would prefer to see a real, official contributor name in the commit.

Any chances that you can fix your GH account and local Git respectively?
This is an Open Source, so everything has to be as open as possible with respective appreciation credits to contributors.

Thank you for the fix!

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]>
@cfredri4 cfredri4 force-pushed the fix-KafkaTemplate-hiding-exceptions-when-starting-observation branch from ec63436 to 91376de Compare March 5, 2025 13:53
@cfredri4 cfredri4 requested a review from artembilan March 5, 2025 13:54
@artembilan
Copy link
Member

The DCO check has failed with:

Commit sha: [91376de](https://github.com/spring-projects/spring-kafka/pull/3779/commits/91376de75c9b340fe52bdf4714c492fe26247cf7), Author: cfredri4, Committer: cfredri4; Expected "cfredri4 [[email protected]](mailto:[email protected])", but got "Christian Fredriksson [[email protected]](mailto:[email protected])".

So, some else is off between your GH account and local Git.

Either way that looks good to me and I'm accepting your contribution and merging it.

Thank you again!

@artembilan artembilan enabled auto-merge (squash) March 5, 2025 14:00
@artembilan artembilan merged commit bdd1fd3 into spring-projects:main Mar 5, 2025
3 checks passed
spring-builds pushed a commit that referenced this pull request Mar 5, 2025
Fixes: #3779

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]>

(cherry picked from commit bdd1fd3)
spring-builds pushed a commit that referenced this pull request Mar 5, 2025
Fixes: #3779

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]>

(cherry picked from commit bdd1fd3)
@cfredri4 cfredri4 deleted the fix-KafkaTemplate-hiding-exceptions-when-starting-observation branch March 6, 2025 19:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants