Skip to content

Commit

Permalink
GH-3686: Fix observation scope closure in the `KafkaMessageListenerCo…
Browse files Browse the repository at this point in the history
…ntainer`

Fixes: #3686

According to our investigation around the `try-with-resource`,
it looks like the resource is already closed when we reach the `catch` block.

* Rework  `KafkaMessageListenerContainer.ListenerConsumer.doInvokeRecordListener()`
to `observation.openScope()` before the `try` and close it manually in the `finally` block
  • Loading branch information
artembilan committed Dec 17, 2024
1 parent 4716ce1 commit eb7fa48
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2750,7 +2750,6 @@ private void pauseForNackSleep() {
* @throws Error an error.
*/
@Nullable
@SuppressWarnings("try")
private RuntimeException doInvokeRecordListener(final ConsumerRecord<K, V> cRecord, // NOSONAR
Iterator<ConsumerRecord<K, V>> iterator) {

Expand All @@ -2763,7 +2762,9 @@ private RuntimeException doInvokeRecordListener(final ConsumerRecord<K, V> cReco
this.observationRegistry);

observation.start();
try (Observation.Scope ignored = observation.openScope()) {
Observation.Scope observationScope = observation.openScope();
// We cannot use 'try-with-resource' because the resource is closed just before catch block
try {
invokeOnMessage(cRecord);
successTimer(sample, cRecord);
recordInterceptAfter(cRecord, null);
Expand Down Expand Up @@ -2802,6 +2803,7 @@ private RuntimeException doInvokeRecordListener(final ConsumerRecord<K, V> cReco
if (!(this.listener instanceof RecordMessagingMessageListenerAdapter<K, V>)) {
observation.stop();
}
observationScope.close();
}
return null;
}
Expand Down Expand Up @@ -4020,6 +4022,6 @@ private static class StopAfterFenceException extends KafkaException {

}

private record FailedRecordTuple<K, V>(ConsumerRecord<K, V> record, RuntimeException ex) { };
private record FailedRecordTuple<K, V>(ConsumerRecord<K, V> record, RuntimeException ex) { }

}

0 comments on commit eb7fa48

Please sign in to comment.