Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c10cb1c

Browse files
committedFeb 21, 2025··
add test
1 parent c79cfd7 commit c10cb1c

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed
 

‎instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlStatementSanitizer.java

+5
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ private static SqlStatementInfo sanitizeImpl(@Nullable String statement, SqlDial
5656
return AutoSqlSanitizer.sanitize(statement, dialect);
5757
}
5858

59+
// visible for tests
60+
static boolean isCached(String statement) {
61+
return sqlToStatementInfoCache.get(CacheKey.create(statement, SqlDialect.DEFAULT)) != null;
62+
}
63+
5964
@AutoValue
6065
abstract static class CacheKey {
6166

‎instrumentation-api-incubator/src/test/java/io/opentelemetry/instrumentation/api/incubator/semconv/db/SqlStatementSanitizerTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,27 @@ public void longInStatementDoesntCauseStackOverflow() {
137137
assertThat(sanitized).isEqualTo("select col from table where col in (?)");
138138
}
139139

140+
@Test
141+
public void largeStatementCached() {
142+
// test that short statement is cached
143+
String shortStatement = "SELECT * FROM TABLE WHERE FIELD = 1234";
144+
String sanitizedShort =
145+
SqlStatementSanitizer.create(true).sanitize(shortStatement).getFullStatement();
146+
assertThat(sanitizedShort).doesNotContain("1234");
147+
assertThat(SqlStatementSanitizer.isCached(shortStatement)).isTrue();
148+
149+
// test that large statement is not cached
150+
StringBuffer s = new StringBuffer();
151+
for (int i = 0; i < 10000; i++) {
152+
s.append("SELECT * FROM TABLE WHERE FIELD = 1234 AND ");
153+
}
154+
String largeStatement = s.toString();
155+
String sanitizedLarge =
156+
SqlStatementSanitizer.create(true).sanitize(largeStatement).getFullStatement();
157+
assertThat(sanitizedLarge).doesNotContain("1234");
158+
assertThat(SqlStatementSanitizer.isCached(largeStatement)).isFalse();
159+
}
160+
140161
static class SqlArgs implements ArgumentsProvider {
141162

142163
@Override

0 commit comments

Comments
 (0)
Please sign in to comment.