Skip to content

Commit 4368029

Browse files
author
m.zharinova
committed
fix after review
1 parent dd6800f commit 4368029

File tree

9 files changed

+29
-36
lines changed

9 files changed

+29
-36
lines changed

spring-boot-3-demo-app-kotlin/src/main/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/service/KafkaReadingService.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import io.github.oshai.kotlinlogging.KotlinLogging
1212
import io.micrometer.tracing.Tracer
1313
import io.micrometer.tracing.propagation.Propagator
1414
import org.apache.kafka.clients.consumer.ConsumerRecord
15-
import org.springframework.dao.DataAccessException
16-
import org.springframework.kafka.KafkaException
1715
import org.springframework.kafka.annotation.KafkaListener
1816
import org.springframework.kafka.support.Acknowledgment
1917
import org.springframework.stereotype.Service
@@ -39,6 +37,7 @@ class KafkaReadingService(
3937
ack.acknowledge()
4038
}
4139

40+
@SuppressWarnings("IllegalCatch", "PMD.AvoidCatchingThrowable")
4241
@KafkaListener(
4342
id = "\${spring.kafka.consumer.additional-groupId}",
4443
topics = ["\${spring.kafka.template.additional-topic}"],
@@ -52,24 +51,25 @@ class KafkaReadingService(
5251
}
5352
records.forEach { record -> restoreContextAndProcessSingleRecordIfNeed(record) }
5453
ack.acknowledge()
55-
} catch (e: KafkaException) {
56-
batchSpan.error(e)
57-
throw e
54+
} catch (throwable: Throwable) {
55+
batchSpan.error(throwable)
56+
throw throwable
5857
} finally {
5958
batchSpan.end()
6059
}
6160
}
6261

62+
@SuppressWarnings("IllegalCatch", "PMD.AvoidCatchingThrowable")
6363
private fun restoreContextAndProcessSingleRecordIfNeed(record: ConsumerRecord<UUID, String>) {
6464
val builder = propagator.extract(record, KafkaHeadersGetter)
6565
val spanFromRecord = builder.name("processing-record-from-kafka").start()
6666
try {
6767
tracer.withSpan(spanFromRecord).use {
6868
dbSaver.processSingleRecord(record)
6969
}
70-
} catch (e: DataAccessException) {
71-
spanFromRecord.error(e)
72-
throw e
70+
} catch (throwable: Throwable) {
71+
spanFromRecord.error(throwable)
72+
throw throwable
7373
} finally {
7474
spanFromRecord.end()
7575
}

spring-boot-3-demo-app-kotlin/src/test/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/controllers/TimeControllerTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ class TimeControllerTest : TestBase() {
9797
String::class.java
9898
)
9999
messageFromDb.forEach {
100-
assertThat(it).isNotNull()
101100
assertThat(it).isEqualTo(received.value())
102101
}
103102
}

spring-boot-3-demo-app-kotlin/src/test/kotlin/io/github/mfvanek/spring/boot3/kotlin/test/service/PublicApiServiceTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ class PublicApiServiceTest : TestBase() {
9999
}
100100

101101
@Test
102-
fun throwsJsonProcessingExceptionWithBdResponse(output: CapturedOutput) {
102+
fun throwsJsonProcessingExceptionWithBadResponse(output: CapturedOutput) {
103103
stubBadResponse()
104104
Observation.createNotStarted("test", observationRegistry).observe {
105105
val result = publicApiService.getZonedTime()

spring-boot-3-demo-app-reactive/src/main/java/io/github/mfvanek/spring/boot3/reactive/service/KafkaReadingService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import lombok.RequiredArgsConstructor;
1616
import lombok.extern.slf4j.Slf4j;
1717
import org.apache.kafka.clients.consumer.ConsumerRecord;
18-
import org.springframework.dao.DataAccessException;
19-
import org.springframework.kafka.KafkaException;
2018
import org.springframework.kafka.annotation.KafkaListener;
2119
import org.springframework.kafka.support.Acknowledgment;
2220
import org.springframework.stereotype.Service;
@@ -42,6 +40,7 @@ public void listen(ConsumerRecord<UUID, String> record, Acknowledgment ack) {
4240
ack.acknowledge();
4341
}
4442

43+
@SuppressWarnings({"IllegalCatch", "PMD.AvoidCatchingThrowable"})
4544
@KafkaListener(
4645
id = "${spring.kafka.consumer.additional-groupId}",
4746
topics = "${spring.kafka.template.additional-topic}",
@@ -55,22 +54,23 @@ public void listenAdditional(List<ConsumerRecord<UUID, String>> records, Acknowl
5554
);
5655
records.forEach(this::restoreContextAndProcessSingleRecordIfNeed);
5756
ack.acknowledge();
58-
} catch (KafkaException e) {
59-
batchSpan.error(e);
60-
throw e;
57+
} catch (Throwable throwable) {
58+
batchSpan.error(throwable);
59+
throw throwable;
6160
} finally {
6261
batchSpan.end();
6362
}
6463
}
6564

65+
@SuppressWarnings({"IllegalCatch", "PMD.AvoidCatchingThrowable"})
6666
private void restoreContextAndProcessSingleRecordIfNeed(ConsumerRecord<UUID, String> record) {
6767
final Span.Builder builder = propagator.extract(record, KAFKA_PROPAGATOR_GETTER);
6868
final Span spanFromRecord = builder.name("processing-record-from-kafka").start();
6969
try (Tracer.SpanInScope ignored = tracer.withSpan(spanFromRecord)) {
7070
dbSaver.processSingleRecord(record);
71-
} catch (DataAccessException e) {
72-
spanFromRecord.error(e);
73-
throw e;
71+
} catch (Throwable throwable) {
72+
spanFromRecord.error(throwable);
73+
throw throwable;
7474
} finally {
7575
spanFromRecord.end();
7676
}

spring-boot-3-demo-app-reactive/src/test/java/io/github/mfvanek/spring/boot3/reactive/controllers/TimeControllerTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,7 @@ void spanShouldBeReportedInLogs(@Nonnull final CapturedOutput output) throws Int
103103
.contains("\"tenant.name\":\"ru-a1-private\"");
104104
final List<String> messageFromDb = namedParameterJdbcTemplate.queryForList("select message from otel_demo.storage where trace_id = :traceId",
105105
Map.of("traceId", traceId), String.class);
106-
messageFromDb.forEach(it -> {
107-
assertThat(it).isNotNull();
108-
assertThat(it).isEqualTo(received.value());
109-
});
106+
messageFromDb.forEach(it -> assertThat(it).isEqualTo(received.value()));
110107
}
111108

112109
@Order(2)

spring-boot-3-demo-app-reactive/src/test/java/io/github/mfvanek/spring/boot3/reactive/service/PublicApiServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ void emptyResponseWhen500StatusWithStepVerifier() {
9898
}
9999

100100
@Test
101-
void emptyResponseWhen200StatusWithBadResposeWithStepVerifier(@Nonnull final CapturedOutput output) {
101+
void emptyResponseWhen200StatusWithBadResponseWithStepVerifier(@Nonnull final CapturedOutput output) {
102102
stubOkButNotCorrectResponse();
103103

104104
StepVerifier.create(publicApiService.getZonedTime())

spring-boot-3-demo-app/src/main/java/io/github/mfvanek/spring/boot3/test/service/KafkaReadingService.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import lombok.RequiredArgsConstructor;
1616
import lombok.extern.slf4j.Slf4j;
1717
import org.apache.kafka.clients.consumer.ConsumerRecord;
18-
import org.springframework.dao.DataAccessException;
19-
import org.springframework.kafka.KafkaException;
2018
import org.springframework.kafka.annotation.KafkaListener;
2119
import org.springframework.kafka.support.Acknowledgment;
2220
import org.springframework.stereotype.Service;
@@ -42,6 +40,7 @@ public void listen(ConsumerRecord<UUID, String> record, Acknowledgment ack) {
4240
ack.acknowledge();
4341
}
4442

43+
@SuppressWarnings({"IllegalCatch", "PMD.AvoidCatchingThrowable"})
4544
@KafkaListener(
4645
id = "${spring.kafka.consumer.additional-groupId}",
4746
topics = "${spring.kafka.template.additional-topic}",
@@ -55,22 +54,23 @@ public void listenAdditional(List<ConsumerRecord<UUID, String>> records, Acknowl
5554
);
5655
records.forEach(this::restoreContextAndProcessSingleRecordIfNeed);
5756
ack.acknowledge();
58-
} catch (KafkaException e) {
59-
batchSpan.error(e);
60-
throw e;
57+
} catch (Throwable throwable) {
58+
batchSpan.error(throwable);
59+
throw throwable;
6160
} finally {
6261
batchSpan.end();
6362
}
6463
}
6564

65+
@SuppressWarnings({"IllegalCatch", "PMD.AvoidCatchingThrowable"})
6666
private void restoreContextAndProcessSingleRecordIfNeed(ConsumerRecord<UUID, String> record) {
6767
final Span.Builder builder = propagator.extract(record, KAFKA_PROPAGATOR_GETTER);
6868
final Span spanFromRecord = builder.name("processing-record-from-kafka").start();
6969
try (Tracer.SpanInScope ignored = tracer.withSpan(spanFromRecord)) {
7070
dbSaver.processSingleRecord(record);
71-
} catch (DataAccessException e) {
72-
spanFromRecord.error(e);
73-
throw e;
71+
} catch (Throwable throwable) {
72+
spanFromRecord.error(throwable);
73+
throw throwable;
7474
} finally {
7575
spanFromRecord.end();
7676
}

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/controllers/TimeControllerTest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ void spanShouldBeReportedInLogs(@Nonnull final CapturedOutput output) throws Int
104104
.contains("\"tenant.name\":\"ru-a1-private\"");
105105
final List<String> messageFromDb = namedParameterJdbcTemplate.queryForList("select message from otel_demo.storage where trace_id = :traceId",
106106
Map.of("traceId", traceId), String.class);
107-
messageFromDb.forEach(it -> {
108-
assertThat(it).isNotNull();
109-
assertThat(it).isEqualTo(received.value());
110-
});
107+
messageFromDb.forEach(it -> assertThat(it).isEqualTo(received.value()));
111108
}
112109

113110
@Order(2)

spring-boot-3-demo-app/src/test/java/io/github/mfvanek/spring/boot3/test/service/PublicApiServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ void throwsJsonProcessingExceptionWithBdResponse(CapturedOutput output) {
9191
Observation.createNotStarted("test", observationRegistry).observe(() -> {
9292
final LocalDateTime result = publicApiService.getZonedTime();
9393
assertThat(result).isNull();
94-
assertThat(Objects.requireNonNull(tracer.currentSpan()).context().traceId()).isNotNull();
94+
assertThat(tracer.currentSpan().context().traceId()).isNotNull();
9595
assertThat(output.getAll()).contains("Failed to convert response");
9696
});
9797
verify(1, getRequestedFor(urlPathMatching("/" + zoneName)));

0 commit comments

Comments
 (0)