-
-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathSentrySQLiteDriver.kt
More file actions
79 lines (71 loc) · 2.33 KB
/
Copy pathSentrySQLiteDriver.kt
File metadata and controls
79 lines (71 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package io.sentry.sqlite
import androidx.sqlite.SQLiteConnection
import androidx.sqlite.SQLiteDriver
import io.sentry.ScopesAdapter
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.SentryLevel
/**
* Wraps a [SQLiteDriver] and automatically adds spans for each SQL statement it executes.
*
* Example usage:
* ```
* val driver = SentrySQLiteDriver.create(AndroidSQLiteDriver())
* ```
*
* If you use Room:
* ```
* val database = Room.databaseBuilder(context, MyDatabase::class.java, "dbName")
* .setDriver(SentrySQLiteDriver.create(AndroidSQLiteDriver()))
* .build()
* ```
*
* **Warning:** Do not use [SentrySQLiteDriver] together with
* [SentrySupportSQLiteOpenHelper][io.sentry.android.sqlite.SentrySupportSQLiteOpenHelper] on the
* same database file. Both wrappers instrument at different layers and combining them will produce
* duplicate spans.
*
* @param delegate The [SQLiteDriver] instance to delegate calls to.
*/
internal class SentrySQLiteDriver private constructor(private val delegate: SQLiteDriver) :
SQLiteDriver {
init {
SentryIntegrationPackageStorage.getInstance().addIntegration("SQLiteDriver")
}
override val hasConnectionPool: Boolean
get() =
try {
delegate.hasConnectionPool
} catch (_: LinkageError) {
// Delegates on androidx.sqlite < 2.6.0 won't have a hasConnectionPool property.
false
}
@Suppress("TooGenericExceptionCaught")
override fun open(fileName: String): SQLiteConnection {
val connection = delegate.open(fileName)
return try {
val spans = DriverSpans.fromFileName(fileName)
// create() ensures delegate is unwrapped, so we don't need to protect against double-wrapping
// the connection.
SentrySQLiteConnection(connection, spans)
} catch (t: Throwable) {
ScopesAdapter.getInstance()
.options
.logger
.log(
SentryLevel.ERROR,
"Failed to instrument SQLite connection; returning uninstrumented connection.",
t,
)
connection
}
}
companion object {
/**
* Wraps the provided delegate in a [SentrySQLiteDriver]. Returns the delegate as-is if already
* wrapped.
*/
@JvmStatic
fun create(delegate: SQLiteDriver): SQLiteDriver =
delegate as? SentrySQLiteDriver ?: SentrySQLiteDriver(delegate)
}
}