|
5 | 5 |
|
6 | 6 | package io.opentelemetry.javaagent.instrumentation.jdbc.test; |
7 | 7 |
|
8 | | -import static org.assertj.core.api.Assertions.assertThat; |
9 | | - |
10 | | -import io.opentelemetry.instrumentation.api.internal.SemconvStability; |
11 | | -import io.opentelemetry.instrumentation.jdbc.TestConnection; |
12 | | -import io.opentelemetry.instrumentation.testing.internal.AutoCleanupExtension; |
| 8 | +import io.opentelemetry.instrumentation.jdbc.testing.AbstractSqlCommenterTest; |
13 | 9 | import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; |
14 | 10 | import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
15 | | -import java.sql.Connection; |
16 | | -import java.sql.PreparedStatement; |
17 | | -import java.sql.SQLException; |
18 | | -import java.sql.Statement; |
19 | | -import java.util.ArrayList; |
20 | | -import java.util.List; |
21 | | -import org.junit.jupiter.api.Test; |
22 | 11 | import org.junit.jupiter.api.extension.RegisterExtension; |
23 | | -import org.junit.jupiter.params.ParameterizedTest; |
24 | | -import org.junit.jupiter.params.provider.ValueSource; |
25 | 12 |
|
26 | | -class SqlCommenterTest { |
27 | | - @RegisterExtension static final AutoCleanupExtension cleanup = AutoCleanupExtension.create(); |
| 13 | +class SqlCommenterTest extends AbstractSqlCommenterTest { |
28 | 14 |
|
29 | 15 | @RegisterExtension |
30 | 16 | static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); |
31 | 17 |
|
32 | | - @Test |
33 | | - void testSqlCommenterStatement() throws SQLException { |
34 | | - List<String> executedSql = new ArrayList<>(); |
35 | | - Connection connection = new TestConnection(executedSql::add); |
36 | | - Statement statement = connection.createStatement(); |
37 | | - |
38 | | - cleanup.deferCleanup(statement); |
39 | | - cleanup.deferCleanup(connection); |
40 | | - |
41 | | - String query = "SELECT 1"; |
42 | | - testing.runWithSpan("parent", () -> statement.execute(query)); |
43 | | - |
44 | | - testing.waitAndAssertTraces( |
45 | | - trace -> |
46 | | - trace.hasSpansSatisfyingExactly( |
47 | | - span -> span.hasName("parent").hasNoParent(), |
48 | | - span -> span.hasName("SELECT dbname").hasParent(trace.getSpan(0)))); |
49 | | - |
50 | | - assertThat(executedSql).hasSize(1); |
51 | | - assertThat(executedSql.get(0)).contains(query).contains("traceparent"); |
52 | | - } |
53 | | - |
54 | | - @ParameterizedTest |
55 | | - @ValueSource(booleans = {true, false}) |
56 | | - void testSqlCommenterStatementUpdate(boolean largeUpdate) throws SQLException { |
57 | | - List<String> executedSql = new ArrayList<>(); |
58 | | - Connection connection = new TestConnection(executedSql::add); |
59 | | - Statement statement = connection.createStatement(); |
60 | | - |
61 | | - cleanup.deferCleanup(statement); |
62 | | - cleanup.deferCleanup(connection); |
63 | | - |
64 | | - String query = "INSERT INTO test VALUES(1)"; |
65 | | - testing.runWithSpan( |
66 | | - "parent", |
67 | | - () -> { |
68 | | - if (largeUpdate) { |
69 | | - statement.executeLargeUpdate(query); |
70 | | - } else { |
71 | | - statement.execute(query); |
72 | | - } |
73 | | - }); |
74 | | - |
75 | | - testing.waitAndAssertTraces( |
76 | | - trace -> |
77 | | - trace.hasSpansSatisfyingExactly( |
78 | | - span -> span.hasName("parent").hasNoParent(), |
79 | | - span -> span.hasName("INSERT dbname.test").hasParent(trace.getSpan(0)))); |
80 | | - |
81 | | - assertThat(executedSql).hasSize(1); |
82 | | - assertThat(executedSql.get(0)).contains(query).contains("traceparent"); |
83 | | - } |
84 | | - |
85 | | - @ParameterizedTest |
86 | | - @ValueSource(booleans = {true, false}) |
87 | | - void testSqlCommenterStatementBatch(boolean largeUpdate) throws SQLException { |
88 | | - List<String> executedSql = new ArrayList<>(); |
89 | | - Connection connection = new TestConnection(executedSql::add); |
90 | | - Statement statement = connection.createStatement(); |
91 | | - |
92 | | - cleanup.deferCleanup(statement); |
93 | | - cleanup.deferCleanup(connection); |
94 | | - |
95 | | - testing.runWithSpan( |
96 | | - "parent", |
97 | | - () -> { |
98 | | - statement.addBatch("INSERT INTO test VALUES(1)"); |
99 | | - statement.addBatch("INSERT INTO test VALUES(2)"); |
100 | | - if (largeUpdate) { |
101 | | - statement.executeLargeBatch(); |
102 | | - } else { |
103 | | - statement.executeBatch(); |
104 | | - } |
105 | | - }); |
106 | | - |
107 | | - testing.waitAndAssertTraces( |
108 | | - trace -> |
109 | | - trace.hasSpansSatisfyingExactly( |
110 | | - span -> span.hasName("parent").hasNoParent(), |
111 | | - span -> |
112 | | - span.hasName( |
113 | | - SemconvStability.emitStableDatabaseSemconv() |
114 | | - ? "BATCH INSERT dbname.test" |
115 | | - : "dbname") |
116 | | - .hasParent(trace.getSpan(0)))); |
117 | | - |
118 | | - assertThat(executedSql).hasSize(2); |
119 | | - assertThat(executedSql.get(0)).contains("INSERT INTO test VALUES(1)").contains("traceparent"); |
120 | | - assertThat(executedSql.get(1)).contains("INSERT INTO test VALUES(2)").contains("traceparent"); |
121 | | - } |
122 | | - |
123 | | - @Test |
124 | | - void testSqlCommenterPreparedStatement() throws SQLException { |
125 | | - List<String> executedSql = new ArrayList<>(); |
126 | | - Connection connection = new TestConnection(executedSql::add); |
127 | | - |
128 | | - String query = "SELECT 1"; |
129 | | - testing.runWithSpan( |
130 | | - "parent", |
131 | | - () -> { |
132 | | - PreparedStatement statement = connection.prepareStatement(query); |
133 | | - cleanup.deferCleanup(statement); |
134 | | - cleanup.deferCleanup(connection); |
135 | | - |
136 | | - statement.execute(); |
137 | | - }); |
138 | | - |
139 | | - testing.waitAndAssertTraces( |
140 | | - trace -> |
141 | | - trace.hasSpansSatisfyingExactly( |
142 | | - span -> span.hasName("parent").hasNoParent(), |
143 | | - span -> span.hasName("SELECT dbname").hasParent(trace.getSpan(0)))); |
144 | | - |
145 | | - assertThat(executedSql).hasSize(1); |
146 | | - assertThat(executedSql.get(0)).contains(query).contains("traceparent"); |
147 | | - } |
148 | | - |
149 | | - @ParameterizedTest |
150 | | - @ValueSource(booleans = {true, false}) |
151 | | - void testSqlCommenterPreparedStatementUpdate(boolean largeUpdate) throws SQLException { |
152 | | - List<String> executedSql = new ArrayList<>(); |
153 | | - Connection connection = new TestConnection(executedSql::add); |
154 | | - |
155 | | - String query = "INSERT INTO test VALUES(1)"; |
156 | | - testing.runWithSpan( |
157 | | - "parent", |
158 | | - () -> { |
159 | | - PreparedStatement statement = connection.prepareStatement(query); |
160 | | - cleanup.deferCleanup(statement); |
161 | | - cleanup.deferCleanup(connection); |
162 | | - |
163 | | - if (largeUpdate) { |
164 | | - statement.executeLargeUpdate(); |
165 | | - } else { |
166 | | - statement.executeUpdate(); |
167 | | - } |
168 | | - }); |
169 | | - |
170 | | - testing.waitAndAssertTraces( |
171 | | - trace -> |
172 | | - trace.hasSpansSatisfyingExactly( |
173 | | - span -> span.hasName("parent").hasNoParent(), |
174 | | - span -> span.hasName("INSERT dbname.test").hasParent(trace.getSpan(0)))); |
175 | | - |
176 | | - assertThat(executedSql).hasSize(1); |
177 | | - assertThat(executedSql.get(0)).contains(query).contains("traceparent"); |
| 18 | + @Override |
| 19 | + protected InstrumentationExtension testing() { |
| 20 | + return testing; |
178 | 21 | } |
179 | 22 | } |
0 commit comments