|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.instrumentation.spring.autoconfigure.instrumentation.r2dbc; |
| 7 | + |
| 8 | +import io.opentelemetry.api.OpenTelemetry; |
| 9 | +import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; |
| 10 | +import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; |
| 11 | +import io.opentelemetry.sdk.autoconfigure.spi.internal.DefaultConfigProperties; |
| 12 | +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; |
| 13 | +import java.util.Collections; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 16 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 17 | +import org.springframework.boot.autoconfigure.r2dbc.R2dbcAutoConfiguration; |
| 18 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 19 | +import org.springframework.r2dbc.core.DatabaseClient; |
| 20 | + |
| 21 | +class R2DbcInstrumentationAutoConfigurationTest { |
| 22 | + |
| 23 | + @RegisterExtension |
| 24 | + static final LibraryInstrumentationExtension testing = LibraryInstrumentationExtension.create(); |
| 25 | + |
| 26 | + private final ApplicationContextRunner runner = |
| 27 | + new ApplicationContextRunner() |
| 28 | + .withBean( |
| 29 | + ConfigProperties.class, |
| 30 | + () -> DefaultConfigProperties.createFromMap(Collections.emptyMap())) |
| 31 | + .withConfiguration( |
| 32 | + AutoConfigurations.of( |
| 33 | + R2dbcInstrumentationAutoConfiguration.class, R2dbcAutoConfiguration.class)) |
| 34 | + .withBean("openTelemetry", OpenTelemetry.class, testing::getOpenTelemetry); |
| 35 | + |
| 36 | + @Test |
| 37 | + void statementSanitizerEnabledByDefault() { |
| 38 | + runner.run( |
| 39 | + context -> { |
| 40 | + DatabaseClient client = context.getBean(DatabaseClient.class); |
| 41 | + client |
| 42 | + .sql( |
| 43 | + "CREATE TABLE IF NOT EXISTS player(id INT NOT NULL AUTO_INCREMENT, name VARCHAR(255), age INT, PRIMARY KEY (id))") |
| 44 | + .fetch() |
| 45 | + .all() |
| 46 | + .blockLast(); |
| 47 | + client.sql("SELECT * FROM player WHERE id = 1").fetch().all().blockLast(); |
| 48 | + testing.waitAndAssertTraces( |
| 49 | + trace -> |
| 50 | + trace.hasSpansSatisfyingExactly( |
| 51 | + span -> |
| 52 | + span.hasAttribute( |
| 53 | + DbIncubatingAttributes.DB_STATEMENT, |
| 54 | + "CREATE TABLE IF NOT EXISTS player(id INT NOT NULL AUTO_INCREMENT, name VARCHAR(?), age INT, PRIMARY KEY (id))")), |
| 55 | + trace -> |
| 56 | + trace.hasSpansSatisfyingExactly( |
| 57 | + span -> |
| 58 | + span.hasAttribute( |
| 59 | + DbIncubatingAttributes.DB_STATEMENT, |
| 60 | + "SELECT * FROM player WHERE id = ?"))); |
| 61 | + }); |
| 62 | + } |
| 63 | +} |
0 commit comments