You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
docs(android): Add SQLiteDriver support to SQLite docs
Rewrites the Room and SQLite integration page to accommodate the introduction of the new SentrySQLiteDriver (see [sentry-java #5466](getsentry/sentry-java#5466)). Page is now called simply "SQLite" in order to emphasize the fact that we support Room, SQLDelight, and direct driver use – without making the title so long as to be overwhelming.
The `sentry-android-sqlite` library generates spans for your SQLite queries, whether you use [Room](https://developer.android.com/training/data-storage/room/), [SQLDelight](https://sqldelight.github.io/sqldelight/), or another persistence library. It does so by wrapping your existing `SQLiteDriver` or `SupportSQLiteOpenHelper`.
15
14
16
-
Supported in Sentry's Android SDK version `4.0.0` and above.
15
+
If you're using the Sentry Android Gradle Plugin, wrapping is performed automatically. (See [Auto-Instrumentation](#auto-instrumentation) for more details.)
17
16
18
-
Supported in Sentry Android Gradle Plugin version `3.0.0` and above.
17
+
Room users can also take advantage of our [auto-instrumentation of DAO methods](#room-dao-methods).
19
18
20
-
Custom instrumentation is supported in Sentry's Android SDK, version `6.21.0` and above.
19
+
<Alert>
21
20
22
-
</Alert>
21
+
Check out our [migration advice](#migrating-from-supportsqliteopenhelper-to-sqlitedriver) if you're migrating from `SupportSQLiteOpenHelper` to `SQLiteDriver`.
23
22
24
-
## Auto-Installation With the Sentry Android Gradle Plugin
23
+
Spans require [tracing to be enabled](/platforms/android/tracing/#configure-the-sample-rate).
25
24
26
-
The [Sentry Android Gradle Plugin](/platforms/android/configuration/gradle/) provides Room and AndroidX SQLite support through bytecode manipulation. The source can be found [on GitHub](https://github.com/getsentry/sentry-android-gradle-plugin/tree/main/plugin-build/src/main/kotlin/io/sentry/android/gradle/instrumentation).
25
+
</Alert>
27
26
28
-
On this page, we get you up and running with Sentry's Room and SQLite Integration, so that it will automatically start a span from an active transaction that's bound to the scope of each sqlite/dao query.
27
+
## Auto-Instrumentation
29
28
30
29
### Install
31
30
32
-
To use the Room and AndroidX SQLite integration, add the Sentry Android Gradle plugin and the Sentry Android SDK (version `4.0.0` or above) in `build.gradle`:
31
+
The [Sentry Android Gradle Plugin](/platforms/android/configuration/gradle/) (SAGP) uses bytecode manipulation to automatically wrap your driver and/or open helper.<sup>†</sup>
32
+
33
+
To use the SAGP, apply it alongside the Sentry Android SDK:
33
34
34
-
```groovy
35
+
```groovy {filename:build.gradle}
35
36
plugins {
36
-
id "io.sentry.android.gradle" version "{{@inject packages.version('sentry.java.android.gradle-plugin', '3.0.0') }}"
37
+
id "io.sentry.android.gradle" version "{{@inject packages.version('sentry.java.android.gradle-plugin', '6.13.0') }}"
|`SupportSQLiteOpenHelper`|`≥3.11.0`| Any `SupportSQLiteOpenHelper.Factory` (SQLDelight, custom factories, etc.) |
59
68
60
69
### Configure
61
70
62
-
In general, no further configuration is required as the auto-instrumentation is enabled by default. If you would like to disable the database instrumentation feature, we expose a configuration option for that:
71
+
No further configuration is required as database auto-instrumentation is enabled by default. To disable it – including `SQLiteDriver` wrapping, `SupportSQLiteOpenHelper` wrapping, and Room DAO spans – use the `DATABASE` instrumentation feature:
See our [Gradle](/platforms/android/configuration/gradle/) page for other SAGP configuration options.
88
97
89
-
<Alert>
90
-
91
-
Supported in Sentry's Android SDK, version `6.21.0` and above.
92
-
93
-
</Alert>
98
+
## Manual Instrumentation
94
99
95
100
### Install
96
101
97
-
Sentry captures data by wrapping a `SupportSQLiteOpenHelper.Factory`. To add the SQLite integration, initialize the [Android SDK](/platforms/android/), then add the `sentry-android-sqlite`dependency using Gradle:
102
+
If you don't use the SAGP, you can always wrap your driver or open helper by hand. Add the Sentry Android SDK and the `sentry-android-sqlite`artifact:
Wrap your driver or open helper directly with `SentrySQLiteDriver.create(...)` or `SentrySupportSQLiteOpenHelper.create(...)`. Use the wrapped instance wherever you would have used the unwrapped one. For most teams, that means wiring it into Room or SQLDelight:
Room is supported when using the default `FrameworkSQLiteOpenHelperFactory` provided by the `androidx.sqlite` package, but any custom `SupportSQLiteOpenHelper` can be used.
136
+
val database =Room.databaseBuilder(context, MyDatabase::class.java, "dbName")
val database =Room.databaseBuilder(context, MyDatabase::class.java, "dbName")
@@ -132,92 +150,128 @@ val database = Room.databaseBuilder(context, MyDatabase::class.java, "dbName")
132
150
.build()
133
151
```
134
152
153
+
**Note**: If you're using the AndroidX [`SupportSQLiteDriver`](https://developer.android.com/reference/kotlin/androidx/sqlite/driver/SupportSQLiteDriver), you'll want to make sure you're wrapping the open helper but ***not*** the support driver itself. See the alert under the [migration section](#migrating-from-supportsqliteopenhelper-to-sqlitedriver) for more details.
**Note**: SQLDelight doesn't currently support `SQLiteDriver`.
191
+
192
+
## Migrating from SupportSQLiteOpenHelper to SQLiteDriver
193
+
194
+
<Alert>
195
+
196
+
This section doesn't apply to migrations to AndroidX's [`SupportSQLiteDriver`](https://developer.android.com/reference/kotlin/androidx/sqlite/driver/SupportSQLiteDriver) (the bridge adapter for Room `[2.7, 3.0)`).
197
+
198
+
The support driver consumes your existing `SupportSQLiteOpenHelper`, so you should continue to use `SentrySupportSQLiteOpenHelper` to wrap your helper as before.
199
+
200
+
</Alert>
201
+
202
+
If you're switching your app's SQLite API from `SupportSQLiteOpenHelper` to `SQLiteDriver`, you may need to update your Sentry dependencies:
† SAGP installs `sentry-android-sqlite` transitively when your project depends
211
+
on `androidx.sqlite:sqlite`, so you typically don't need to add it. But if your
212
+
project explicitly pins `sentry-android-sqlite`, be sure to bump it to `8.45.0+`.
213
+
</small>
214
+
215
+
Replace `SentrySupportSQLiteOpenHelper.create(openHelper)` with `SentrySQLiteDriver.create(driver)`– and you're all set! (See [Manual Instrumentation → Room](#room) for the full call-site context.)
216
+
217
+
**Note**: Due to underlying API differences, `SQLiteDriver` and `SupportSQLiteOpenHelper` spans can sometimes differ. The driver's spans are tailored to work performed by the database itself during statement execution, while the open helper may also track time spent on statement preparation and work your app performs consuming native SQLite output.
218
+
146
219
## Verify
147
220
148
-
Assuming you have the following (reduced) code snippet performing a database query on a Room Dao:
221
+
To confirm that your wrapped driver or open helper is producing spans, execute a query inside a Sentry transaction and check for the SQL span in [sentry.io](https://sentry.io).
149
222
150
-
```kotlin
151
-
importandroid.os.Bundle
152
-
importandroid.widget.Button
153
-
importandroidx.activity.ComponentActivity
154
-
importandroidx.room.Database
155
-
importandroidx.room.Dao
156
-
importandroidx.room.Insert
157
-
importandroidx.room.OnConflictStrategy
158
-
importandroidx.room.RoomDatabase
223
+
```kotlin {tabTitle:SQLiteDriver} {mdExpandTabs}
159
224
importio.sentry.Sentry
160
225
importio.sentry.SpanStatus
161
-
importkotlinx.coroutines.withContext
226
+
importio.sentry.TransactionOptions
162
227
163
-
@Dao
164
-
abstractclassTracksDao {
165
-
@Insert(onConflict =OnConflictStrategy.REPLACE)
166
-
abstractsuspendfuninsert(track:Track): Long
167
-
}
168
-
169
-
@Database(
170
-
entities = [Track::class],
171
-
version =1,
172
-
exportSchema =false
228
+
// `driver` is your SentrySQLiteDriver-wrapped instance (see Configure section above).
To view the recorded transaction, log into [sentry.io](https://sentry.io) and open your project. Clicking on **Performance** will open a page with transactions, where you can select the just recorded transaction with the name `Track Interaction`. The event will look similar to this:
204
-
205
-

259
+
To view the recorded transaction, log into [sentry.io](https://sentry.io) and open the [Traces](https://sentry.io/orgredirect/organizations/:orgslug/traces/) page. Filter by `transaction:"DB Smoke Test"`, then open the trace. You should see a SQL-level span emitted by the Sentry wrapper around your driver or open helper, with the executed query as its description.
206
260
207
261
<Alert>
208
262
209
-
The Sentry Android Gradle plugin will report SQL queries for any `SupportSQLiteOpenHelper.Factory`, starting with version `3.11.0`.
263
+
Sentry captures SQL strings as span descriptions. If you execute SQL with values interpolated directly into the query string (for example, `db.query("SELECT * FROM users WHERE id = $id")`), those values will be sent to Sentry. Prefer parameterized queries – `query(sql, bindArgs)`, `execSQL(sql, bindArgs)`, or Room `@Query` placeholders – so only the SQL skeleton is captured. See [Scrubbing Sensitive Data](/platforms/android/data-management/sensitive-data/) for additional controls.
210
264
211
-
Earlier versions will only support standard `androidx.room` usage and won't report SQL queries for any `SupportSQLiteOpenHelper.Factory` other than [androidx.sqlite](https://github.com/androidx/androidx/tree/androidx-main/sqlite).
265
+
</Alert>
212
266
213
-
If you're having trouble with this SDK, we want to hear about it. Create an [issue on GitHub](https://github.com/getsentry/sentry-android-gradle-plugin/issues) and describe your experience.
267
+
## Room DAO Methods
214
268
215
-
</Alert>
269
+
For users of Room, the SAGP will also auto-instrument your DAO classes, adding a span around each DAO method invocation. This gives you a higher-level span for the DAO operation on top of the SQL-level spans produced by the underlying driver or open helper wrapper, making it easier to attribute queries back to the code that issued them.
216
270
217
-
<Alert>
271
+
DAO methods are instrumented via bytecode manipulation. We don't currently support manual instrumentation.
218
272
219
-
If you are directly using [SupportSQLiteDatabase#query](<https://developer.android.com/reference/androidx/sqlite/db/SupportSQLiteDatabase#query(java.lang.String)>) or [SupportSQLiteDatabase#execSQL](<https://developer.android.com/reference/androidx/sqlite/db/SupportSQLiteDatabase#execSQL(java.lang.String)>) methods through the Room's SQLiteOpenHelper, consider switching to their alternatives that accept `bindArgs` as a second parameter.
273
+
<Alert>
220
274
221
-
Because Sentry captures SQL queries as Span description, there is a risk of leaking sensitive data when not using an SQL string with placeholders.
275
+
**Known limitation:** DAO auto-instrumentation doesn't work on Room `≥2.7.0` (including Room `≥3.0.0`) due to internal API changes, although you'll still see SQL-level spans from the underlying `SQLiteDriver` or `SupportSQLiteOpenHelper` wrapper. (See [#1304](https://github.com/getsentry/sentry-android-gradle-plugin/issues/1304).)
0 commit comments