@@ -25,15 +25,22 @@ import org.mockito.kotlin.whenever
2525
2626class SentryJdbcEventListenerTest {
2727 class Fixture {
28+ lateinit var options: SentryOptions
2829 val scopes =
29- mock<IScopes >().apply {
30- whenever(options)
31- .thenReturn(SentryOptions ().apply { sdkVersion = SdkVersion (" test" , " 1.2.3" ) })
32- }
30+ mock<IScopes >().apply { whenever(this .options).thenAnswer { this @Fixture.options } }
3331 lateinit var tx: SentryTracer
3432 val actualDataSource = JDBCDataSource ()
3533
36- fun getSut (withRunningTransaction : Boolean = true, existingRow : Int? = null): DataSource {
34+ fun getSut (
35+ withRunningTransaction : Boolean = true,
36+ existingRow : Int? = null,
37+ enableDatabaseTransactionTracing : Boolean = false,
38+ ): DataSource {
39+ options =
40+ SentryOptions ().apply {
41+ sdkVersion = SdkVersion (" test" , " 1.2.3" )
42+ isEnableDatabaseTransactionTracing = enableDatabaseTransactionTracing
43+ }
3744 tx = SentryTracer (TransactionContext (" name" , " op" ), scopes)
3845 if (withRunningTransaction) {
3946 whenever(scopes.span).thenReturn(tx)
@@ -171,8 +178,8 @@ class SentryJdbcEventListenerTest {
171178 }
172179
173180 @Test
174- fun `creates span for commit` () {
175- val sut = fixture.getSut()
181+ fun `creates span for commit when database transaction tracing is enabled ` () {
182+ val sut = fixture.getSut(enableDatabaseTransactionTracing = true )
176183
177184 sut.connection.use {
178185 it.autoCommit = false
@@ -187,8 +194,8 @@ class SentryJdbcEventListenerTest {
187194 }
188195
189196 @Test
190- fun `creates span for rollback` () {
191- val sut = fixture.getSut()
197+ fun `creates span for rollback when database transaction tracing is enabled ` () {
198+ val sut = fixture.getSut(enableDatabaseTransactionTracing = true )
192199
193200 sut.connection.use {
194201 it.autoCommit = false
@@ -204,7 +211,7 @@ class SentryJdbcEventListenerTest {
204211
205212 @Test
206213 fun `commit span has database details` () {
207- val sut = fixture.getSut()
214+ val sut = fixture.getSut(enableDatabaseTransactionTracing = true )
208215
209216 sut.connection.use {
210217 it.autoCommit = false
@@ -220,7 +227,7 @@ class SentryJdbcEventListenerTest {
220227
221228 @Test
222229 fun `rollback span has database details` () {
223- val sut = fixture.getSut()
230+ val sut = fixture.getSut(enableDatabaseTransactionTracing = true )
224231
225232 sut.connection.use {
226233 it.autoCommit = false
@@ -236,7 +243,8 @@ class SentryJdbcEventListenerTest {
236243
237244 @Test
238245 fun `does not create commit span when there is no running transaction` () {
239- val sut = fixture.getSut(withRunningTransaction = false )
246+ val sut =
247+ fixture.getSut(withRunningTransaction = false , enableDatabaseTransactionTracing = true )
240248
241249 sut.connection.use {
242250 it.autoCommit = false
@@ -250,7 +258,8 @@ class SentryJdbcEventListenerTest {
250258
251259 @Test
252260 fun `does not create rollback span when there is no running transaction` () {
253- val sut = fixture.getSut(withRunningTransaction = false )
261+ val sut =
262+ fixture.getSut(withRunningTransaction = false , enableDatabaseTransactionTracing = true )
254263
255264 sut.connection.use {
256265 it.autoCommit = false
@@ -263,8 +272,8 @@ class SentryJdbcEventListenerTest {
263272 }
264273
265274 @Test
266- fun `creates span for transaction begin when setAutoCommit false` () {
267- val sut = fixture.getSut()
275+ fun `creates span for transaction begin when setAutoCommit false and database transaction tracing is enabled ` () {
276+ val sut = fixture.getSut(enableDatabaseTransactionTracing = true )
268277
269278 sut.connection.use {
270279 it.autoCommit = false
@@ -280,7 +289,7 @@ class SentryJdbcEventListenerTest {
280289
281290 @Test
282291 fun `transaction begin span has database details` () {
283- val sut = fixture.getSut()
292+ val sut = fixture.getSut(enableDatabaseTransactionTracing = true )
284293
285294 sut.connection.use {
286295 it.autoCommit = false
@@ -296,7 +305,7 @@ class SentryJdbcEventListenerTest {
296305
297306 @Test
298307 fun `does not create begin span when already in manual commit mode` () {
299- val sut = fixture.getSut()
308+ val sut = fixture.getSut(enableDatabaseTransactionTracing = true )
300309
301310 sut.connection.use {
302311 it.autoCommit = false
@@ -311,7 +320,22 @@ class SentryJdbcEventListenerTest {
311320
312321 @Test
313322 fun `does not create begin span when there is no running transaction` () {
314- val sut = fixture.getSut(withRunningTransaction = false )
323+ val sut =
324+ fixture.getSut(withRunningTransaction = false , enableDatabaseTransactionTracing = true )
325+
326+ sut.connection.use {
327+ it.autoCommit = false
328+ it.prepareStatement(" INSERT INTO foo VALUES (1)" ).executeUpdate()
329+ it.commit()
330+ }
331+
332+ val beginSpans = fixture.tx.children.filter { it.operation == " db.sql.transaction.begin" }
333+ assertTrue(beginSpans.isEmpty())
334+ }
335+
336+ @Test
337+ fun `does not create transaction spans when database transaction tracing is disabled` () {
338+ val sut = fixture.getSut(enableDatabaseTransactionTracing = false )
315339
316340 sut.connection.use {
317341 it.autoCommit = false
@@ -320,6 +344,28 @@ class SentryJdbcEventListenerTest {
320344 }
321345
322346 val beginSpans = fixture.tx.children.filter { it.operation == " db.sql.transaction.begin" }
347+ val commitSpans = fixture.tx.children.filter { it.operation == " db.sql.transaction.commit" }
323348 assertTrue(beginSpans.isEmpty())
349+ assertTrue(commitSpans.isEmpty())
350+ // Query spans should still be created
351+ val querySpans = fixture.tx.children.filter { it.operation == " db.query" }
352+ assertEquals(1 , querySpans.size)
353+ }
354+
355+ @Test
356+ fun `does not create rollback span when database transaction tracing is disabled` () {
357+ val sut = fixture.getSut(enableDatabaseTransactionTracing = false )
358+
359+ sut.connection.use {
360+ it.autoCommit = false
361+ it.prepareStatement(" INSERT INTO foo VALUES (1)" ).executeUpdate()
362+ it.rollback()
363+ }
364+
365+ val rollbackSpans = fixture.tx.children.filter { it.operation == " db.sql.transaction.rollback" }
366+ assertTrue(rollbackSpans.isEmpty())
367+ // Query spans should still be created
368+ val querySpans = fixture.tx.children.filter { it.operation == " db.query" }
369+ assertEquals(1 , querySpans.size)
324370 }
325371}
0 commit comments