|
9 | 9 | import static org.mockito.Mockito.mock; |
10 | 10 | import static org.mockito.Mockito.when; |
11 | 11 |
|
| 12 | +import io.opentelemetry.api.common.Attributes; |
| 13 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 14 | +import io.opentelemetry.instrumentation.api.internal.SemconvStability; |
| 15 | +import io.opentelemetry.semconv.DbAttributes; |
| 16 | +import io.opentelemetry.semconv.incubating.DbIncubatingAttributes; |
12 | 17 | import java.util.stream.Stream; |
13 | 18 | import org.apache.camel.Exchange; |
14 | 19 | import org.apache.camel.Message; |
15 | 20 | import org.junit.jupiter.params.ParameterizedTest; |
16 | 21 | import org.junit.jupiter.params.provider.Arguments; |
17 | 22 | import org.junit.jupiter.params.provider.MethodSource; |
18 | 23 |
|
| 24 | +@SuppressWarnings("deprecation") // using deprecated semconv |
19 | 25 | class SanitizationTest { |
20 | 26 |
|
21 | 27 | @ParameterizedTest |
22 | 28 | @MethodSource("sanitizeCqlArgs") |
23 | | - void sanitizeCql(String original, String expected) { |
| 29 | + void sanitizeCql(String original, String expectedStatement, String expectedSummary) { |
24 | 30 | DbSpanDecorator decorator = new DbSpanDecorator("cql", ""); |
25 | 31 |
|
26 | 32 | Exchange exchange = mock(Exchange.class); |
27 | 33 | Message message = mock(Message.class); |
28 | 34 | when(message.getHeader("CamelCqlQuery")).thenReturn(original); |
29 | 35 | when(exchange.getIn()).thenReturn(message); |
30 | 36 |
|
31 | | - String actualSanitized = decorator.getStatement(exchange, null); |
32 | | - assertThat(actualSanitized).isEqualTo(expected); |
| 37 | + assertSanitizedStatement(decorator, exchange, expectedStatement, expectedSummary); |
33 | 38 | } |
34 | 39 |
|
35 | 40 | static Stream<Arguments> sanitizeCqlArgs() { |
36 | 41 | return Stream.of( |
37 | 42 | Arguments.of( |
38 | | - "SELECT * FROM users WHERE field>=-1234", "SELECT * FROM users WHERE field>=?"), |
| 43 | + "SELECT * FROM users WHERE field>=-1234", |
| 44 | + "SELECT * FROM users WHERE field>=?", |
| 45 | + "SELECT users"), |
39 | 46 | Arguments.of( |
40 | 47 | "SELECT name, phone FROM contact WHERE state = 'NY'", |
41 | | - "SELECT name, phone FROM contact WHERE state = ?"), |
42 | | - Arguments.of("SELECT * FROM col WHERE tag='Something'", "SELECT * FROM col WHERE tag=?")); |
| 48 | + "SELECT name, phone FROM contact WHERE state = ?", |
| 49 | + "SELECT contact"), |
| 50 | + Arguments.of( |
| 51 | + "SELECT * FROM col WHERE tag='Something'", |
| 52 | + "SELECT * FROM col WHERE tag=?", |
| 53 | + "SELECT col")); |
43 | 54 | } |
44 | 55 |
|
45 | 56 | @ParameterizedTest |
46 | 57 | @MethodSource("sanitizeJdbcArgs") |
47 | | - void sanitizeJdbc(String original, String expected) { |
| 58 | + void sanitizeJdbc(String original, String expectedStatement, String expectedSummary) { |
48 | 59 | DbSpanDecorator decorator = new DbSpanDecorator("jdbc", ""); |
49 | 60 |
|
50 | 61 | Exchange exchange = mock(Exchange.class); |
51 | 62 | Message message = mock(Message.class); |
52 | 63 | when(message.getBody()).thenReturn(original); |
53 | 64 | when(exchange.getIn()).thenReturn(message); |
54 | 65 |
|
55 | | - String actualSanitized = decorator.getStatement(exchange, null); |
56 | | - assertThat(actualSanitized).isEqualTo(expected); |
| 66 | + assertSanitizedStatement(decorator, exchange, expectedStatement, expectedSummary); |
57 | 67 | } |
58 | 68 |
|
59 | 69 | static Stream<Arguments> sanitizeJdbcArgs() { |
60 | 70 | return Stream.of( |
61 | | - Arguments.of("SELECT 3", "SELECT ?"), |
| 71 | + Arguments.of("SELECT 3", "SELECT ?", "SELECT"), |
| 72 | + Arguments.of( |
| 73 | + "SELECT * FROM TABLE WHERE FIELD = 1234", |
| 74 | + "SELECT * FROM TABLE WHERE FIELD = ?", |
| 75 | + "SELECT TABLE"), |
62 | 76 | Arguments.of( |
63 | | - "SELECT * FROM TABLE WHERE FIELD = 1234", "SELECT * FROM TABLE WHERE FIELD = ?"), |
64 | | - Arguments.of("SELECT * FROM TABLE WHERE FIELD<-1234", "SELECT * FROM TABLE WHERE FIELD<?"), |
| 77 | + "SELECT * FROM TABLE WHERE FIELD<-1234", |
| 78 | + "SELECT * FROM TABLE WHERE FIELD<?", |
| 79 | + "SELECT TABLE"), |
65 | 80 | Arguments.of( |
66 | 81 | "SELECT col1 AS col2 FROM users WHERE field=1234", |
67 | | - "SELECT col1 AS col2 FROM users WHERE field=?")); |
| 82 | + "SELECT col1 AS col2 FROM users WHERE field=?", |
| 83 | + "SELECT users")); |
68 | 84 | } |
69 | 85 |
|
70 | 86 | @ParameterizedTest |
71 | 87 | @MethodSource("sanitizeSqlArgs") |
72 | | - void sanitizeSql(String original, String expected) { |
| 88 | + void sanitizeSql(String original, String expectedStatement, String expectedSummary) { |
73 | 89 | DbSpanDecorator decorator = new DbSpanDecorator("sql", ""); |
74 | 90 |
|
75 | 91 | Exchange exchange = mock(Exchange.class); |
76 | 92 | Message message = mock(Message.class); |
77 | 93 | when(message.getHeader("CamelSqlQuery")).thenReturn(original); |
78 | 94 | when(exchange.getIn()).thenReturn(message); |
79 | 95 |
|
80 | | - String actualSanitized = decorator.getStatement(exchange, null); |
81 | | - assertThat(actualSanitized).isEqualTo(expected); |
| 96 | + assertSanitizedStatement(decorator, exchange, expectedStatement, expectedSummary); |
82 | 97 | } |
83 | 98 |
|
84 | 99 | static Stream<Arguments> sanitizeSqlArgs() { |
85 | 100 | return Stream.of( |
86 | 101 | Arguments.of( |
87 | 102 | "SELECT * FROM table WHERE col1=1234 AND col2>3", |
88 | | - "SELECT * FROM table WHERE col1=? AND col2>?"), |
89 | | - Arguments.of("UPDATE table SET col=12", "UPDATE table SET col=?"), |
90 | | - Arguments.of("insert into table where col=321", "insert into table where col=?")); |
| 103 | + "SELECT * FROM table WHERE col1=? AND col2>?", |
| 104 | + "SELECT table"), |
| 105 | + Arguments.of("UPDATE table SET col=12", "UPDATE table SET col=?", "UPDATE table"), |
| 106 | + Arguments.of( |
| 107 | + "insert into table where col=321", "insert into table where col=?", "INSERT table")); |
| 108 | + } |
| 109 | + |
| 110 | + private static void assertSanitizedStatement( |
| 111 | + DbSpanDecorator decorator, |
| 112 | + Exchange exchange, |
| 113 | + String expectedStatement, |
| 114 | + String expectedSummary) { |
| 115 | + AttributesBuilder attributesBuilder = Attributes.builder(); |
| 116 | + decorator.setQueryAttributes(attributesBuilder, exchange); |
| 117 | + Attributes attributes = attributesBuilder.build(); |
| 118 | + |
| 119 | + if (SemconvStability.emitStableDatabaseSemconv()) { |
| 120 | + assertThat(attributes.get(DbAttributes.DB_QUERY_TEXT)).isEqualTo(expectedStatement); |
| 121 | + assertThat(attributes.get(DbAttributes.DB_QUERY_SUMMARY)).isEqualTo(expectedSummary); |
| 122 | + } |
| 123 | + if (SemconvStability.emitOldDatabaseSemconv()) { |
| 124 | + assertThat(attributes.get(DbIncubatingAttributes.DB_STATEMENT)).isEqualTo(expectedStatement); |
| 125 | + } |
91 | 126 | } |
92 | 127 | } |
0 commit comments