|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.spring.autoconfigure.internal.instrumentation.jdbc; |
| 7 | + |
| 8 | +import static io.opentelemetry.instrumentation.testing.junit.db.SemconvStabilityUtil.maybeStable; |
| 9 | +import static io.opentelemetry.semconv.incubating.DbIncubatingAttributes.DB_STATEMENT; |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | + |
| 12 | +import io.opentelemetry.api.OpenTelemetry; |
| 13 | +import io.opentelemetry.instrumentation.api.incubator.config.internal.InstrumentationConfig; |
| 14 | +import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.ConfigPropertiesBridge; |
| 15 | +import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; |
| 16 | +import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; |
| 17 | +import java.sql.Connection; |
| 18 | +import java.sql.Statement; |
| 19 | +import java.util.Collections; |
| 20 | +import javax.sql.DataSource; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 23 | +import org.springframework.aop.framework.Advised; |
| 24 | +import org.springframework.aop.support.AopUtils; |
| 25 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 26 | +import org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration; |
| 27 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 28 | + |
| 29 | +class JdbcInstrumentationAutoConfigurationTest { |
| 30 | + |
| 31 | + @RegisterExtension |
| 32 | + static final LibraryInstrumentationExtension testing = LibraryInstrumentationExtension.create(); |
| 33 | + |
| 34 | + private final ApplicationContextRunner runner = |
| 35 | + new ApplicationContextRunner() |
| 36 | + .withBean( |
| 37 | + InstrumentationConfig.class, |
| 38 | + () -> |
| 39 | + new ConfigPropertiesBridge( |
| 40 | + DefaultConfigProperties.createFromMap(Collections.emptyMap()))) |
| 41 | + .withConfiguration( |
| 42 | + AutoConfigurations.of( |
| 43 | + JdbcInstrumentationSpringBoot4AutoConfiguration.class, |
| 44 | + DataSourceAutoConfiguration.class)) |
| 45 | + .withBean("openTelemetry", OpenTelemetry.class, testing::getOpenTelemetry); |
| 46 | + |
| 47 | + @SuppressWarnings("deprecation") // using deprecated semconv |
| 48 | + @Test |
| 49 | + void statementSanitizerEnabledByDefault() { |
| 50 | + runner.run( |
| 51 | + context -> { |
| 52 | + DataSource dataSource = context.getBean(DataSource.class); |
| 53 | + |
| 54 | + assertThat(AopUtils.isAopProxy(dataSource)).isTrue(); |
| 55 | + assertThat(dataSource.getClass().getSimpleName()).isNotEqualTo("HikariDataSource"); |
| 56 | + // unwrap the instrumented data source to get the original data source |
| 57 | + Object original = ((Advised) dataSource).getTargetSource().getTarget(); |
| 58 | + assertThat(AopUtils.isAopProxy(original)).isFalse(); |
| 59 | + assertThat(original.getClass().getSimpleName()).isEqualTo("HikariDataSource"); |
| 60 | + |
| 61 | + try (Connection connection = dataSource.getConnection()) { |
| 62 | + try (Statement statement = connection.createStatement()) { |
| 63 | + statement.execute("SELECT 1"); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + testing.waitAndAssertTraces( |
| 68 | + trace -> |
| 69 | + trace.hasSpansSatisfyingExactly( |
| 70 | + span -> span.hasAttribute(maybeStable(DB_STATEMENT), "SELECT ?"))); |
| 71 | + }); |
| 72 | + } |
| 73 | +} |
0 commit comments