From c85164e1c7cf0303035039bd1894c5e9316be8be Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Fri, 10 May 2024 14:03:32 +0200 Subject: [PATCH] fix kafka test --- .../AbstractJvmKafkaSpringStarterSmokeTest.java | 1 + .../AbstractKafkaSpringStarterSmokeTest.java | 11 ++++++++--- .../smoketest/AbstractSpringStarterSmokeTest.java | 6 ++++++ .../spring/smoketest/SpringSmokeTestRunner.java | 14 ++++++++++---- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java index cc1a229554d6..5b073c6625b7 100644 --- a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java +++ b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractJvmKafkaSpringStarterSmokeTest.java @@ -22,6 +22,7 @@ import org.testcontainers.containers.wait.strategy.Wait; import org.testcontainers.utility.DockerImageName; +/** Spring has a test container integration, but that doesn't work for Spring Boot 2 */ @DisabledInNativeImage // See GraalVmNativeKafkaSpringStarterSmokeTest for the GraalVM native test public class AbstractJvmKafkaSpringStarterSmokeTest extends AbstractKafkaSpringStarterSmokeTest { static KafkaContainer kafka; diff --git a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java index a15697e8fe78..ac81759395c4 100644 --- a/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java +++ b/smoke-tests-otel-starter/spring-boot-common/src/main/java/io/opentelemetry/spring/smoketest/AbstractKafkaSpringStarterSmokeTest.java @@ -27,8 +27,6 @@ abstract class AbstractKafkaSpringStarterSmokeTest extends AbstractSpringStarter @Autowired protected KafkaTemplate kafkaTemplate; - // In kafka 2 ops.send is deprecated. We are using it to avoid reflection because kafka 3 also has - // ops.send, although with different return type. @SuppressWarnings({"unchecked", "deprecation"}) @Test void shouldInstrumentProducerAndConsumer() { @@ -39,7 +37,14 @@ void shouldInstrumentProducerAndConsumer() { () -> { kafkaTemplate.executeInTransaction( ops -> { - ops.send("testTopic", "10", "testSpan"); + // return type is incompatible between Spring Boot 2 and 3 + try { + ops.getClass() + .getDeclaredMethod("send", String.class, Object.class, Object.class) + .invoke(ops, "testTopic", "10", "testSpan"); + } catch (Exception e) { + throw new IllegalStateException(e); + } return 0; }); }); diff --git a/smoke-tests-otel-starter/spring-smoke-testing/src/main/java/io/opentelemetry/spring/smoketest/AbstractSpringStarterSmokeTest.java b/smoke-tests-otel-starter/spring-smoke-testing/src/main/java/io/opentelemetry/spring/smoketest/AbstractSpringStarterSmokeTest.java index 131f3329dfed..5f6d407e0cad 100644 --- a/smoke-tests-otel-starter/spring-smoke-testing/src/main/java/io/opentelemetry/spring/smoketest/AbstractSpringStarterSmokeTest.java +++ b/smoke-tests-otel-starter/spring-smoke-testing/src/main/java/io/opentelemetry/spring/smoketest/AbstractSpringStarterSmokeTest.java @@ -9,6 +9,7 @@ import io.opentelemetry.api.OpenTelemetry; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; @@ -22,6 +23,11 @@ public abstract class AbstractSpringStarterSmokeTest { protected SpringSmokeTestRunner testing; + @BeforeAll + static void beforeAll() { + SpringSmokeTestRunner.resetExporters(); + } + @BeforeEach void setUpTesting() { if (openTelemetry != null) { diff --git a/smoke-tests-otel-starter/spring-smoke-testing/src/main/java/io/opentelemetry/spring/smoketest/SpringSmokeTestRunner.java b/smoke-tests-otel-starter/spring-smoke-testing/src/main/java/io/opentelemetry/spring/smoketest/SpringSmokeTestRunner.java index 49e1cdbe23bd..471016ee883e 100644 --- a/smoke-tests-otel-starter/spring-smoke-testing/src/main/java/io/opentelemetry/spring/smoketest/SpringSmokeTestRunner.java +++ b/smoke-tests-otel-starter/spring-smoke-testing/src/main/java/io/opentelemetry/spring/smoketest/SpringSmokeTestRunner.java @@ -22,16 +22,22 @@ */ public final class SpringSmokeTestRunner extends InstrumentationTestRunner { - static final InMemorySpanExporter testSpanExporter = InMemorySpanExporter.create(); - static final InMemoryMetricExporter testMetricExporter = - InMemoryMetricExporter.create(AggregationTemporality.DELTA); - static final InMemoryLogRecordExporter testLogRecordExporter = InMemoryLogRecordExporter.create(); + static InMemorySpanExporter testSpanExporter; + static InMemoryMetricExporter testMetricExporter; + static InMemoryLogRecordExporter testLogRecordExporter; + static OpenTelemetry openTelemetry; public SpringSmokeTestRunner(OpenTelemetry openTelemetry) { super(openTelemetry); } + static void resetExporters() { + testSpanExporter = InMemorySpanExporter.create(); + testMetricExporter = InMemoryMetricExporter.create(AggregationTemporality.DELTA); + testLogRecordExporter = InMemoryLogRecordExporter.create(); + } + @Override public void beforeTestClass() {}