diff --git a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts index a96d3962e447..004694694419 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts +++ b/instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts @@ -71,6 +71,8 @@ dependencies { } testImplementation("javax.servlet:javax.servlet-api:3.1.0") testImplementation("jakarta.servlet:jakarta.servlet-api:5.0.0") + testRuntimeOnly("com.h2database:h2:1.4.197") + testRuntimeOnly("io.r2dbc:r2dbc-h2:1.0.0.RELEASE") testImplementation(project(":testing-common")) testImplementation("io.opentelemetry:opentelemetry-sdk") diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/r2dbc/R2DbcInstrumentationAutoConfigurationTest.java b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/r2dbc/R2DbcInstrumentationAutoConfigurationTest.java new file mode 100644 index 000000000000..b7bc9a25b997 --- /dev/null +++ b/instrumentation/spring/spring-boot-autoconfigure/src/test/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/r2dbc/R2DbcInstrumentationAutoConfigurationTest.java @@ -0,0 +1,63 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.r2dbc; + +import io.opentelemetry.api.OpenTelemetry; +import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; +import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; +import java.util.Collections; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.RegisterExtension; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.r2dbc.core.DatabaseClient; + +class R2DbcInstrumentationAutoConfigurationTest { + + @RegisterExtension + static final LibraryInstrumentationExtension testing = LibraryInstrumentationExtension.create(); + + private final ApplicationContextRunner runner = + new ApplicationContextRunner() + .withBean( + ConfigProperties.class, + () -> DefaultConfigProperties.createFromMap(Collections.emptyMap())) + .withConfiguration( + AutoConfigurations.of( + R2dbcInstrumentationAutoConfiguration.class, R2dbcAutoConfiguration.class)) + .withBean("openTelemetry", OpenTelemetry.class, testing::getOpenTelemetry); + + @Test + void statementSanitizerEnabledByDefault() { + runner.run( + context -> { + DatabaseClient client = context.getBean(DatabaseClient.class); + client + .sql( + "CREATE TABLE IF NOT EXISTS player(id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255), age INT, PRIMARY KEY (id))") + .fetch() + .all() + .blockLast(); + client.sql("SELECT * FROM player WHERE id = 1").fetch().all().blockLast(); + testing.waitAndAssertTraces( + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasAttribute( + DbIncubatingAttributes.DB_STATEMENT, + "CREATE TABLE IF NOT EXISTS player(id INT NOT NULL AUTO_INCREMENT, name VARCHAR(?), age INT, PRIMARY KEY (id))")), + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasAttribute( + DbIncubatingAttributes.DB_STATEMENT, + "SELECT * FROM player WHERE id = ?"))); + }); + } +}