Skip to content

Commit 3fca76c

Browse files
committed
Implement same behaviour in InstrumentedBatchInterceptor as in InstrumentedRecordInterceptor.
Close trace in case of exceptions.
1 parent 9b15525 commit 3fca76c

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

instrumentation/spring/spring-kafka-2.7/library/src/main/java/io/opentelemetry/instrumentation/spring/kafka/v2_7/InstrumentedBatchInterceptor.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,23 @@ private static Context getParentContext(ConsumerRecords<?, ?> records) {
7171

7272
@Override
7373
public void success(ConsumerRecords<K, V> records, Consumer<K, V> consumer) {
74-
end(records, null);
75-
if (decorated != null) {
76-
decorated.success(records, consumer);
74+
try {
75+
if (decorated != null) {
76+
decorated.success(records, consumer);
77+
}
78+
} finally {
79+
end(records, null);
7780
}
7881
}
7982

8083
@Override
8184
public void failure(ConsumerRecords<K, V> records, Exception exception, Consumer<K, V> consumer) {
82-
end(records, exception);
83-
if (decorated != null) {
84-
decorated.failure(records, exception, consumer);
85+
try {
86+
if (decorated != null) {
87+
decorated.failure(records, exception, consumer);
88+
}
89+
} finally {
90+
end(records, exception);
8591
}
8692
}
8793

instrumentation/spring/spring-kafka-2.7/library/src/main/java/io/opentelemetry/instrumentation/spring/kafka/v2_7/InstrumentedRecordInterceptor.java

+12-6
Original file line numberDiff line numberDiff line change
@@ -69,18 +69,24 @@ private static Context getParentContext(ConsumerRecord<?, ?> record) {
6969

7070
@Override
7171
public void success(ConsumerRecord<K, V> record, Consumer<K, V> consumer) {
72-
if (decorated != null) {
73-
decorated.success(record, consumer);
72+
try {
73+
if (decorated != null) {
74+
decorated.success(record, consumer);
75+
}
76+
} finally {
77+
end(record, null);
7478
}
75-
end(record, null);
7679
}
7780

7881
@Override
7982
public void failure(ConsumerRecord<K, V> record, Exception exception, Consumer<K, V> consumer) {
80-
if (decorated != null) {
81-
decorated.failure(record, exception, consumer);
83+
try {
84+
if (decorated != null) {
85+
decorated.failure(record, exception, consumer);
86+
}
87+
} finally {
88+
end(record, exception);
8289
}
83-
end(record, exception);
8490
}
8591

8692
private void end(ConsumerRecord<K, V> record, @Nullable Throwable error) {

0 commit comments

Comments
 (0)