From 0a2fcea502f8cb8ee742bf31562f5829cee55fc5 Mon Sep 17 00:00:00 2001 From: brunobat Date: Fri, 3 Jan 2025 16:08:28 +0000 Subject: [PATCH] No jdbc spans if otel off ar build time or sdk is disabled at runtime --- .../agroal/deployment/AgroalProcessor.java | 11 ++-- .../agroal/runtime/AgroalRecorder.java | 6 ++- .../quarkus/agroal/runtime/DataSources.java | 6 ++- .../spi/OpenTelemetryInitBuildItem.java | 9 ---- .../deployment/OpenTelemetrySdkBuildItem.java | 27 ++++++++++ .../deployment/OpenTelemetryProcessor.java | 15 ++++-- .../logging/LogHandlerProcessor.java | 4 +- .../runtime/OpenTelemetryRecorder.java | 6 +++ .../pom.xml | 17 ++++++ .../it/opentelemetry/PingPongResource.java | 4 ++ .../it/opentelemetry/model/h2/H2Hit.java | 36 +++++++++++++ .../src/main/resources/application.properties | 8 +++ .../it/opentelemetry/Db2LifecycleManager.java | 1 + .../opentelemetry/H2DatabaseTestResource.java | 52 +++++++++++++++++++ ...enTelemetryOffJdbcInstrumentationTest.java | 20 +++++++ .../MariaDbLifecycleManager.java | 1 + .../OpenTelemetryJdbcInstrumentationTest.java | 12 +++++ .../opentelemetry/OracleLifecycleManager.java | 1 + .../PostgreSqlLifecycleManager.java | 1 + 19 files changed, 213 insertions(+), 24 deletions(-) delete mode 100644 extensions/agroal/spi/src/main/java/io/quarkus/agroal/spi/OpenTelemetryInitBuildItem.java create mode 100644 extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/OpenTelemetrySdkBuildItem.java create mode 100644 integration-tests/opentelemetry-jdbc-instrumentation/src/main/java/io/quarkus/it/opentelemetry/model/h2/H2Hit.java create mode 100644 integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/H2DatabaseTestResource.java create mode 100644 integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/H2OpenTelemetryOffJdbcInstrumentationTest.java diff --git a/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java b/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java index 7007893adadf4..0203d954780e9 100644 --- a/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java +++ b/extensions/agroal/deployment/src/main/java/io/quarkus/agroal/deployment/AgroalProcessor.java @@ -1,6 +1,7 @@ package io.quarkus.agroal.deployment; import static io.quarkus.agroal.deployment.AgroalDataSourceBuildUtil.qualifiers; +import static io.quarkus.arc.deployment.OpenTelemetrySdkBuildItem.isOtelSdkEnabled; import static io.quarkus.deployment.Capability.OPENTELEMETRY_TRACER; import java.sql.Driver; @@ -26,6 +27,7 @@ import io.agroal.api.AgroalPoolInterceptor; import io.quarkus.agroal.DataSource; import io.quarkus.agroal.runtime.AgroalDataSourceSupport; +import io.quarkus.agroal.runtime.AgroalOpenTelemetryWrapper; import io.quarkus.agroal.runtime.AgroalRecorder; import io.quarkus.agroal.runtime.DataSourceJdbcBuildTimeConfig; import io.quarkus.agroal.runtime.DataSources; @@ -34,9 +36,9 @@ import io.quarkus.agroal.runtime.TransactionIntegration; import io.quarkus.agroal.spi.JdbcDataSourceBuildItem; import io.quarkus.agroal.spi.JdbcDriverBuildItem; -import io.quarkus.agroal.spi.OpenTelemetryInitBuildItem; import io.quarkus.arc.BeanDestroyer; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; +import io.quarkus.arc.deployment.OpenTelemetrySdkBuildItem; import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.arc.deployment.UnremovableBeanBuildItem; import io.quarkus.arc.processor.DotNames; @@ -127,7 +129,7 @@ void build( // at least one datasource is using OpenTelemetry JDBC instrumentation, // therefore we register the OpenTelemetry data source wrapper bean additionalBeans.produce(new AdditionalBeanBuildItem.Builder() - .addBeanClass("io.quarkus.agroal.runtime.AgroalOpenTelemetryWrapper") + .addBeanClass(AgroalOpenTelemetryWrapper.class) .setDefaultScope(DotNames.SINGLETON).build()); } @@ -244,13 +246,13 @@ void generateDataSourceSupportBean(AgroalRecorder recorder, @Record(ExecutionTime.RUNTIME_INIT) @BuildStep - @Consume(OpenTelemetryInitBuildItem.class) @Consume(NarayanaInitBuildItem.class) void generateDataSourceBeans(AgroalRecorder recorder, DataSourcesRuntimeConfig dataSourcesRuntimeConfig, List aggregatedBuildTimeConfigBuildItems, SslNativeConfigBuildItem sslNativeConfig, Capabilities capabilities, + Optional openTelemetrySdkBuildItem, BuildProducer syntheticBeanBuildItemBuildProducer, BuildProducer jdbcDataSource) { if (aggregatedBuildTimeConfigBuildItems.isEmpty()) { @@ -275,7 +277,8 @@ void generateDataSourceBeans(AgroalRecorder recorder, .checkActive(recorder.agroalDataSourceCheckActiveSupplier(dataSourceName)) // pass the runtime config into the recorder to ensure that the DataSource related beans // are created after runtime configuration has been set up - .createWith(recorder.agroalDataSourceSupplier(dataSourceName, dataSourcesRuntimeConfig)) + .createWith(recorder.agroalDataSourceSupplier( + dataSourceName, dataSourcesRuntimeConfig, isOtelSdkEnabled(openTelemetrySdkBuildItem))) .destroyer(BeanDestroyer.AutoCloseableDestroyer.class); if (!DataSourceUtil.isDefault(dataSourceName)) { diff --git a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/AgroalRecorder.java b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/AgroalRecorder.java index 4a1b5b300b0f0..eeea464179304 100644 --- a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/AgroalRecorder.java +++ b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/AgroalRecorder.java @@ -55,13 +55,15 @@ public ActiveResult get() { public Function, AgroalDataSource> agroalDataSourceSupplier( String dataSourceName, - @SuppressWarnings("unused") DataSourcesRuntimeConfig dataSourcesRuntimeConfig) { + @SuppressWarnings("unused") DataSourcesRuntimeConfig dataSourcesRuntimeConfig, + Optional> otelEnabled) { return new Function<>() { @SuppressWarnings("deprecation") @Override public AgroalDataSource apply(SyntheticCreationalContext context) { DataSources dataSources = context.getInjectedReference(DataSources.class); - return dataSources.createDataSource(dataSourceName); + return dataSources.createDataSource(dataSourceName, + otelEnabled.isPresent() ? otelEnabled.get().getValue() : false); } }; } diff --git a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java index b8dd019dbe556..80079ba3b4897 100644 --- a/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java +++ b/extensions/agroal/runtime/src/main/java/io/quarkus/agroal/runtime/DataSources.java @@ -138,7 +138,7 @@ public AgroalDataSource getDataSource(String dataSourceName) { } @SuppressWarnings("resource") - public AgroalDataSource createDataSource(String dataSourceName) { + public AgroalDataSource createDataSource(String dataSourceName, boolean otelEnabled) { if (!agroalDataSourceSupport.entries.containsKey(dataSourceName)) { throw new IllegalArgumentException("No datasource named '" + dataSourceName + "' exists"); } @@ -221,7 +221,9 @@ public AgroalDataSource createDataSource(String dataSourceName) { dataSource.setPoolInterceptors(interceptorList); } - if (dataSourceJdbcBuildTimeConfig.telemetry() && dataSourceJdbcRuntimeConfig.telemetry().orElse(true)) { + if (dataSourceJdbcBuildTimeConfig.telemetry() && + dataSourceJdbcRuntimeConfig.telemetry().orElse(true) && + otelEnabled) { // activate OpenTelemetry JDBC instrumentation by wrapping AgroalDatasource // use an optional CDI bean as we can't reference optional OpenTelemetry classes here dataSource = agroalOpenTelemetryWrapper.get().apply(dataSource); diff --git a/extensions/agroal/spi/src/main/java/io/quarkus/agroal/spi/OpenTelemetryInitBuildItem.java b/extensions/agroal/spi/src/main/java/io/quarkus/agroal/spi/OpenTelemetryInitBuildItem.java deleted file mode 100644 index c9f818427ab24..0000000000000 --- a/extensions/agroal/spi/src/main/java/io/quarkus/agroal/spi/OpenTelemetryInitBuildItem.java +++ /dev/null @@ -1,9 +0,0 @@ -package io.quarkus.agroal.spi; - -import io.quarkus.builder.item.EmptyBuildItem; - -/** - * Marker build item that indicates that the OpenTelemetry extension has been initialized. - */ -public final class OpenTelemetryInitBuildItem extends EmptyBuildItem { -} diff --git a/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/OpenTelemetrySdkBuildItem.java b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/OpenTelemetrySdkBuildItem.java new file mode 100644 index 0000000000000..86c0ccdd3aa63 --- /dev/null +++ b/extensions/arc/deployment/src/main/java/io/quarkus/arc/deployment/OpenTelemetrySdkBuildItem.java @@ -0,0 +1,27 @@ +package io.quarkus.arc.deployment; + +import java.util.Optional; + +import io.quarkus.builder.item.SimpleBuildItem; +import io.quarkus.runtime.RuntimeValue; + +public final class OpenTelemetrySdkBuildItem extends SimpleBuildItem { + + private final RuntimeValue runtimeEnabled; + + public OpenTelemetrySdkBuildItem(RuntimeValue sdkEnabled) { + this.runtimeEnabled = sdkEnabled; + } + + /** + * True if the OpenTelemetry SDK is enabled at build and runtime. + */ + public RuntimeValue isRuntimeEnabled() { + return runtimeEnabled; + } + + public static Optional> isOtelSdkEnabled(Optional buildItem) { + // optional is empty if the extension is disabled at build time + return buildItem.isPresent() ? Optional.of(buildItem.get().isRuntimeEnabled()) : Optional.empty(); + } +} diff --git a/extensions/opentelemetry/deployment/src/main/java/io/quarkus/opentelemetry/deployment/OpenTelemetryProcessor.java b/extensions/opentelemetry/deployment/src/main/java/io/quarkus/opentelemetry/deployment/OpenTelemetryProcessor.java index 4b1846f90a857..23b2f45372657 100644 --- a/extensions/opentelemetry/deployment/src/main/java/io/quarkus/opentelemetry/deployment/OpenTelemetryProcessor.java +++ b/extensions/opentelemetry/deployment/src/main/java/io/quarkus/opentelemetry/deployment/OpenTelemetryProcessor.java @@ -36,11 +36,11 @@ import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSamplerProvider; import io.opentelemetry.sdk.autoconfigure.spi.traces.ConfigurableSpanExporterProvider; import io.quarkus.agroal.spi.JdbcDataSourceBuildItem; -import io.quarkus.agroal.spi.OpenTelemetryInitBuildItem; import io.quarkus.arc.deployment.AdditionalBeanBuildItem; import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem; import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.InterceptorBindingRegistrarBuildItem; +import io.quarkus.arc.deployment.OpenTelemetrySdkBuildItem; import io.quarkus.arc.deployment.SyntheticBeanBuildItem; import io.quarkus.arc.deployment.ValidationPhaseBuildItem.ValidationErrorBuildItem; import io.quarkus.arc.processor.InterceptorBindingRegistrar; @@ -107,8 +107,11 @@ AdditionalBeanBuildItem ensureProducerIsRetained() { @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - SyntheticBeanBuildItem openTelemetryBean(OpenTelemetryRecorder recorder, OTelRuntimeConfig oTelRuntimeConfig) { - return SyntheticBeanBuildItem.configure(OpenTelemetry.class) + void openTelemetryBean(OpenTelemetryRecorder recorder, + OTelRuntimeConfig oTelRuntimeConfig, + BuildProducer syntheticProducer, + BuildProducer openTelemetrySdkBuildItemBuildProducer) { + syntheticProducer.produce(SyntheticBeanBuildItem.configure(OpenTelemetry.class) .defaultBean() .setRuntimeInit() .unremovable() @@ -122,7 +125,10 @@ SyntheticBeanBuildItem openTelemetryBean(OpenTelemetryRecorder recorder, OTelRun null)) .createWith(recorder.opentelemetryBean(oTelRuntimeConfig)) .destroyer(OpenTelemetryDestroyer.class) - .done(); + .done()); + + openTelemetrySdkBuildItemBuildProducer.produce( + new OpenTelemetrySdkBuildItem(recorder.isOtelSdkEnabled(oTelRuntimeConfig))); } @BuildStep @@ -258,7 +264,6 @@ void transformWithSpan(BuildProducer annotation @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - @Produce(OpenTelemetryInitBuildItem.class) void createOpenTelemetry( OpenTelemetryRecorder recorder, CoreVertxBuildItem vertx, diff --git a/extensions/opentelemetry/deployment/src/main/java/io/quarkus/opentelemetry/deployment/logging/LogHandlerProcessor.java b/extensions/opentelemetry/deployment/src/main/java/io/quarkus/opentelemetry/deployment/logging/LogHandlerProcessor.java index b552448cf0327..0b3c103b29261 100644 --- a/extensions/opentelemetry/deployment/src/main/java/io/quarkus/opentelemetry/deployment/logging/LogHandlerProcessor.java +++ b/extensions/opentelemetry/deployment/src/main/java/io/quarkus/opentelemetry/deployment/logging/LogHandlerProcessor.java @@ -3,12 +3,12 @@ import java.util.function.BooleanSupplier; import java.util.function.Function; +import io.quarkus.arc.deployment.OpenTelemetrySdkBuildItem; import org.jboss.jandex.DotName; import io.opentelemetry.sdk.autoconfigure.spi.logs.ConfigurableLogRecordExporterProvider; import io.opentelemetry.sdk.logs.LogRecordProcessor; import io.opentelemetry.sdk.logs.export.LogRecordExporter; -import io.quarkus.agroal.spi.OpenTelemetryInitBuildItem; import io.quarkus.arc.deployment.BeanContainerBuildItem; import io.quarkus.arc.deployment.UnremovableBeanBuildItem; import io.quarkus.deployment.annotations.BuildProducer; @@ -45,7 +45,7 @@ void nativeSupport(BuildProducer servicesProducer) { @BuildStep @Record(ExecutionTime.RUNTIME_INIT) - @Consume(OpenTelemetryInitBuildItem.class) + @Consume(OpenTelemetrySdkBuildItem.class) LogHandlerBuildItem build(OpenTelemetryLogRecorder recorder, OTelRuntimeConfig config, BeanContainerBuildItem beanContainerBuildItem) { diff --git a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryRecorder.java b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryRecorder.java index 852258c4a4f51..8b969870377cc 100644 --- a/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryRecorder.java +++ b/extensions/opentelemetry/runtime/src/main/java/io/quarkus/opentelemetry/runtime/OpenTelemetryRecorder.java @@ -19,6 +19,7 @@ import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk; import io.quarkus.arc.SyntheticCreationalContext; import io.quarkus.opentelemetry.runtime.config.runtime.OTelRuntimeConfig; +import io.quarkus.runtime.RuntimeValue; import io.quarkus.runtime.annotations.Recorder; import io.quarkus.runtime.annotations.RuntimeInit; import io.quarkus.runtime.annotations.StaticInit; @@ -38,6 +39,11 @@ public void resetGlobalOpenTelemetryForDevMode() { GlobalEventLoggerProvider.resetForTest(); } + @RuntimeInit + public RuntimeValue isOtelSdkEnabled(OTelRuntimeConfig oTelRuntimeConfig) { + return new RuntimeValue<>(!oTelRuntimeConfig.sdkDisabled()); + } + @RuntimeInit public void eagerlyCreateContextStorage() { ContextStorage.get(); diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/pom.xml b/integration-tests/opentelemetry-jdbc-instrumentation/pom.xml index 6fbe77dbd028d..1e43a0fcc8a6c 100644 --- a/integration-tests/opentelemetry-jdbc-instrumentation/pom.xml +++ b/integration-tests/opentelemetry-jdbc-instrumentation/pom.xml @@ -56,6 +56,10 @@ io.quarkus quarkus-hibernate-orm-panache + + io.quarkus + quarkus-jdbc-h2 + @@ -215,6 +219,19 @@ + + io.quarkus + quarkus-jdbc-h2-deployment + ${project.version} + pom + test + + + * + * + + + diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/main/java/io/quarkus/it/opentelemetry/PingPongResource.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/main/java/io/quarkus/it/opentelemetry/PingPongResource.java index a8219d6cb4b62..8921ad4ff64ed 100644 --- a/integration-tests/opentelemetry-jdbc-instrumentation/src/main/java/io/quarkus/it/opentelemetry/PingPongResource.java +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/main/java/io/quarkus/it/opentelemetry/PingPongResource.java @@ -13,6 +13,7 @@ import io.quarkus.it.opentelemetry.model.Hit; import io.quarkus.it.opentelemetry.model.db2.Db2Hit; +import io.quarkus.it.opentelemetry.model.h2.H2Hit; import io.quarkus.it.opentelemetry.model.mariadb.MariaDbHit; import io.quarkus.it.opentelemetry.model.oracle.OracleHit; import io.quarkus.it.opentelemetry.model.pg.PgHit; @@ -39,6 +40,9 @@ public Hit createHit(@QueryParam("id") Long id, @PathParam("tenant") String tena case "db2": persist(Db2Hit::new, id); return Db2Hit.findById(id); + case "h2": + persist(H2Hit::new, id); + return H2Hit.findById(id); default: throw new IllegalArgumentException(); } diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/main/java/io/quarkus/it/opentelemetry/model/h2/H2Hit.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/main/java/io/quarkus/it/opentelemetry/model/h2/H2Hit.java new file mode 100644 index 0000000000000..5a67d9384d6b2 --- /dev/null +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/main/java/io/quarkus/it/opentelemetry/model/h2/H2Hit.java @@ -0,0 +1,36 @@ +package io.quarkus.it.opentelemetry.model.h2; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; + +import io.quarkus.hibernate.orm.panache.PanacheEntityBase; +import io.quarkus.it.opentelemetry.model.Hit; + +@Entity +public class H2Hit extends PanacheEntityBase implements Hit { + + @Id + public Long id; + + public String message; + + @Override + public Long getId() { + return id; + } + + @Override + public String getMessage() { + return message; + } + + @Override + public void setId(Long id) { + this.id = id; + } + + @Override + public void setMessage(String message) { + this.message = message; + } +} diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/main/resources/application.properties b/integration-tests/opentelemetry-jdbc-instrumentation/src/main/resources/application.properties index 93429306dc891..5f77730e8e29a 100644 --- a/integration-tests/opentelemetry-jdbc-instrumentation/src/main/resources/application.properties +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/main/resources/application.properties @@ -41,6 +41,14 @@ quarkus.datasource.db2.db-kind=db2 quarkus.datasource.db2.jdbc.max-size=1 quarkus.datasource.db2.jdbc.telemetry=true +# H2 data source +quarkus.hibernate-orm.h2.datasource=h2 +quarkus.hibernate-orm.h2.packages=${model-base-dir}h2 +quarkus.hibernate-orm.h2.database.generation=none +quarkus.datasource.h2.db-kind=h2 +quarkus.datasource.h2.jdbc.max-size=1 +quarkus.datasource.h2.jdbc.telemetry=true + # speed up build quarkus.otel.bsp.schedule.delay=100 quarkus.otel.bsp.export.timeout=5s diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/Db2LifecycleManager.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/Db2LifecycleManager.java index d5e4a52407e41..aa42a443553fe 100644 --- a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/Db2LifecycleManager.java +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/Db2LifecycleManager.java @@ -31,6 +31,7 @@ public Map start() { properties.put("quarkus.hibernate-orm.oracle.active", "false"); properties.put("quarkus.hibernate-orm.postgresql.active", "false"); properties.put("quarkus.hibernate-orm.mariadb.active", "false"); + properties.put("quarkus.hibernate-orm.h2.active", "false"); return properties; } diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/H2DatabaseTestResource.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/H2DatabaseTestResource.java new file mode 100644 index 0000000000000..f0861138e222b --- /dev/null +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/H2DatabaseTestResource.java @@ -0,0 +1,52 @@ +package io.quarkus.it.opentelemetry; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.Map; + +import org.h2.tools.Server; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public class H2DatabaseTestResource implements QuarkusTestResourceLifecycleManager { + + public static final String QUARKUS_OTEL_SDK_DISABLED = "quarkus.otel.sdk.disabled"; + private Server tcpServer; + private Map initProperties; + + @Override + public void init(Map initArgs) { + initProperties = initArgs; + } + + @Override + public Map start() { + + try { + tcpServer = Server.createTcpServer("-ifNotExists"); + tcpServer.start(); + System.out.println("[INFO] H2 database started in TCP server mode; server status: " + tcpServer.getStatus()); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + Map properties = new HashMap<>(initProperties); + properties.put("quarkus.datasource.h2.jdbc.url", "jdbc:h2:tcp://localhost/mem:test"); + properties.put("quarkus.hibernate-orm.h2.database.generation", "drop-and-create"); + properties.put("quarkus.hibernate-orm.postgresql.active", "false"); + properties.put("quarkus.hibernate-orm.oracle.active", "false"); + properties.put("quarkus.hibernate-orm.mariadb.active", "false"); + properties.put("quarkus.hibernate-orm.db2.active", "false"); + + return properties; + } + + @Override + public synchronized void stop() { + if (tcpServer != null) { + tcpServer.stop(); + System.out.println("[INFO] H2 database was shut down; server status: " + tcpServer.getStatus()); + tcpServer = null; + } + } +} diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/H2OpenTelemetryOffJdbcInstrumentationTest.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/H2OpenTelemetryOffJdbcInstrumentationTest.java new file mode 100644 index 0000000000000..bd780de873ef2 --- /dev/null +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/H2OpenTelemetryOffJdbcInstrumentationTest.java @@ -0,0 +1,20 @@ +package io.quarkus.it.opentelemetry; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.common.ResourceArg; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +@QuarkusTestResource(value = H2DatabaseTestResource.class, restrictToAnnotatedClass = true, initArgs = { + @ResourceArg(name = H2DatabaseTestResource.QUARKUS_OTEL_SDK_DISABLED, value = "true") +}) +public class H2OpenTelemetryOffJdbcInstrumentationTest extends OpenTelemetryJdbcInstrumentationTest { + + @Test + void testSqlQueryNotTraced() { + // When OpenTelemetry is disabled at runtime, the queries should not be traced + testQueryNotTraced("h2"); + } +} diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/MariaDbLifecycleManager.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/MariaDbLifecycleManager.java index 6325338571caf..bd76ab64d29cd 100644 --- a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/MariaDbLifecycleManager.java +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/MariaDbLifecycleManager.java @@ -31,6 +31,7 @@ public Map start() { properties.put("quarkus.hibernate-orm.oracle.active", "false"); properties.put("quarkus.hibernate-orm.postgresql.active", "false"); properties.put("quarkus.hibernate-orm.db2.active", "false"); + properties.put("quarkus.hibernate-orm.h2.active", "false"); return properties; } diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/OpenTelemetryJdbcInstrumentationTest.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/OpenTelemetryJdbcInstrumentationTest.java index 8dde3f5b0ac02..f3f751d7e5397 100644 --- a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/OpenTelemetryJdbcInstrumentationTest.java +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/OpenTelemetryJdbcInstrumentationTest.java @@ -68,4 +68,16 @@ protected void testQueryTraced(String dbKind, String expectedTable) { }); } + protected void testQueryNotTraced(String dbKind) { + given() + .queryParam("id", 1) + .when().post("/hit/" + dbKind) + .then() + .statusCode(200) + .body("message", Matchers.equalTo("Hit message.")); + + Awaitility.await().atMost(Duration.ofSeconds(2)).untilAsserted(() -> { + assertTrue(getSpans().isEmpty(), "No spans should be recorded when OpenTelemetry is disabled."); + }); + } } diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/OracleLifecycleManager.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/OracleLifecycleManager.java index 0bd02392b3c3f..67a23c6cca77a 100644 --- a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/OracleLifecycleManager.java +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/OracleLifecycleManager.java @@ -18,6 +18,7 @@ public Map start() { properties.put("quarkus.hibernate-orm.mariadb.active", "false"); properties.put("quarkus.hibernate-orm.postgresql.active", "false"); properties.put("quarkus.hibernate-orm.db2.active", "false"); + properties.put("quarkus.hibernate-orm.h2.active", "false"); return properties; } diff --git a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/PostgreSqlLifecycleManager.java b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/PostgreSqlLifecycleManager.java index 762fa477746a0..1db85f65ce8c5 100644 --- a/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/PostgreSqlLifecycleManager.java +++ b/integration-tests/opentelemetry-jdbc-instrumentation/src/test/java/io/quarkus/it/opentelemetry/PostgreSqlLifecycleManager.java @@ -31,6 +31,7 @@ public Map start() { properties.put("quarkus.hibernate-orm.oracle.active", "false"); properties.put("quarkus.hibernate-orm.mariadb.active", "false"); properties.put("quarkus.hibernate-orm.db2.active", "false"); + properties.put("quarkus.hibernate-orm.h2.active", "false"); return properties; }