Repository navigation
Expand file tree
/
Copy pathSentrySQLiteDriver.kt
More file actions
120 lines (110 loc) · 4.21 KB
/
Copy pathSentrySQLiteDriver.kt
File metadata and controls
120 lines (110 loc) · 4.21 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package io.sentry.sqlite
import androidx.sqlite.SQLiteConnection
import androidx.sqlite.SQLiteDriver
import io.sentry.ScopesAdapter
import io.sentry.SentryLevel
import io.sentry.util.ExceptionUtils
import io.sentry.util.IntegrationUtils.addIntegrationToSdkVersion
/**
* 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()
* ```
*
* If you're using the Sentry Android Gradle Plugin (SAGP) 6.13.0+, wrapping will be performed
* automatically for Room.
*
* @param delegate The [SQLiteDriver] instance to delegate calls to.
*/
public class SentrySQLiteDriver private constructor(private val delegate: SQLiteDriver) :
SQLiteDriver {
init {
addIntegrationToSdkVersion("SQLiteDriver")
}
@Suppress("INAPPLICABLE_JVM_NAME")
@get:JvmName("hasConnectionPool")
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 (e: LinkageError) {
// androidx.sqlite is compileOnly, so the app's version may not match the SQLiteConnection
// that SentrySQLiteConnection implements. The connection is returned uninstrumented.
logInstrumentationFailure(e)
connection
} catch (t: Throwable) {
// Instrumenting must never stop the app from opening its database, so any other failure
// also returns the connection uninstrumented.
ExceptionUtils.rethrowIfFatal(t)
logInstrumentationFailure(t)
connection
}
}
private fun logInstrumentationFailure(t: Throwable) {
ScopesAdapter.getInstance()
.options
.logger
.log(
SentryLevel.ERROR,
"Failed to instrument SQLite connection; returning uninstrumented connection.",
t,
)
}
public companion object {
/**
* Name of the bridge adapter often used with Room 2.7+. It implements the `SQLiteDriver`
* interface and its constructor consumes a `SupportSQLiteOpenHelper`. (Users of the Sentry
* Android Gradle Plugin will have the `SupportSQLiteOpenHelper` wrapped for them
* automatically.) We deliberately avoid wrapping the adapter to prevent duplicate spans.
*
* String (rather than an `is` check) lets us avoid a compile-time dependency on
* androidx.sqlite:sqlite-framework.
*/
private const val SUPPORT_SQLITE_DRIVER_FQN = "androidx.sqlite.driver.SupportSQLiteDriver"
/**
* Wraps the provided delegate in a [SentrySQLiteDriver].
*
* To avoid duplicate spans, returns the delegate as-is if:
* 1. it's already wrapped, or
* 2. it's an `androidx.sqlite.driver.SupportSQLiteDriver`.
*
* In the case of (2), wrap the open helper passed to the `SupportSQLiteDriver` constructor via
* `SentrySupportSQLiteOpenHelper` instead.
*
* Note that wrapping will be performed if the delegate isn't a `SupportSQLiteDriver` itself but
* wraps or subclasses one. In that case, ensure the open helper passed to the support driver
* constructor is *not* wrapped.
*/
// Warning! The SAGP depends on this method's ABI.
@JvmStatic
public fun create(delegate: SQLiteDriver): SQLiteDriver =
// FQN check simplifies our SAGP implementation, allowing it to naively instrument all
// RoomDatabase.Builder.setDriver() call sites.
if (delegate is SentrySQLiteDriver || delegate.javaClass.name == SUPPORT_SQLITE_DRIVER_FQN) {
delegate
} else {
SentrySQLiteDriver(delegate)
}
}
}