Skip to content

Commit c2b2a17

Browse files
committed
add test that statement sanitizer is enabled by default
1 parent c53fe0c commit c2b2a17

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ dependencies {
7171
}
7272
testImplementation("javax.servlet:javax.servlet-api:3.1.0")
7373
testImplementation("jakarta.servlet:jakarta.servlet-api:5.0.0")
74+
testRuntimeOnly("com.h2database:h2:1.4.197")
75+
testRuntimeOnly("io.r2dbc:r2dbc-h2:1.0.0.RELEASE")
7476

7577
testImplementation(project(":testing-common"))
7678
testImplementation("io.opentelemetry:opentelemetry-sdk")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)