|
| 1 | +--- |
| 2 | +title: Room and SQLite |
| 3 | +caseStyle: camelCase |
| 4 | +supportLevel: production |
| 5 | +categories: |
| 6 | + - mobile |
| 7 | +--- |
| 8 | + |
| 9 | +<Note> |
| 10 | + |
| 11 | +Supported in Sentry's Android SDK version `4.0.0` and above. |
| 12 | + |
| 13 | +Supported in Sentry Android Gradle Plugin version `3.0.0` and above. |
| 14 | + |
| 15 | +</Note> |
| 16 | + |
| 17 | +The [Sentry Android Gradle Plugin](/platforms/android/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). |
| 18 | + |
| 19 | +On this page, we get you up and running with Sentry's Room and SQLite Integration, so that it will automatically start a span out of the active transaction, bound to the scope for each sqlite/dao query. |
| 20 | + |
| 21 | +## Install |
| 22 | + |
| 23 | +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`: |
| 24 | + |
| 25 | +```groovy |
| 26 | +buildscript { |
| 27 | + repositories { |
| 28 | + mavenCentral() |
| 29 | + } |
| 30 | +} |
| 31 | +
|
| 32 | +plugins { |
| 33 | + id "io.sentry.android.gradle" version "3.0.0-beta.3" |
| 34 | +} |
| 35 | +
|
| 36 | +dependencies { |
| 37 | + implementation 'io.sentry:sentry-android:{{ packages.version('sentry.java.android', '5.0.0') }}' |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +```kotlin |
| 42 | +buildscript { |
| 43 | + repositories { |
| 44 | + mavenCentral() |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +plugins { |
| 49 | + id("io.sentry.android.gradle") version "3.0.0-beta.3" |
| 50 | +} |
| 51 | + |
| 52 | +dependencies { |
| 53 | + implementation("io.sentry:sentry-android:{{ packages.version('sentry.java.android', '5.0.0') }}") |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +<Note> |
| 58 | + |
| 59 | +Make sure, that [performance monitoring](/platforms/android/performance/#configure-the-sample-rate) is enabled. |
| 60 | + |
| 61 | +</Note> |
| 62 | + |
| 63 | +## Configure |
| 64 | + |
| 65 | +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: |
| 66 | + |
| 67 | +```groovy |
| 68 | +import io.sentry.android.gradle.InstrumentationFeature |
| 69 | +
|
| 70 | +sentry { |
| 71 | + tracingInstrumentation { |
| 72 | + enabled = true |
| 73 | + features = EnumSet.allOf(InstrumentationFeature) - InstrumentationFeature.DATABASE |
| 74 | + } |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +```kotlin |
| 79 | +import java.util.EnumSet |
| 80 | +import io.sentry.android.gradle.InstrumentationFeature |
| 81 | + |
| 82 | +sentry { |
| 83 | + tracingInstrumentation { |
| 84 | + enabled.set(true) |
| 85 | + features.set(EnumSet.allOf(InstrumentationFeature::class.java) - InstrumentationFeature.DATABASE) |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +## Verify |
| 91 | + |
| 92 | +Assuming you have the following (reduced) code snippet performing a database query on a Room Dao: |
| 93 | + |
| 94 | +```kotlin |
| 95 | +import android.os.Bundle |
| 96 | +import android.widget.Button |
| 97 | +import androidx.activity.ComponentActivity |
| 98 | +import androidx.room.Database |
| 99 | +import androidx.room.Dao |
| 100 | +import androidx.room.Insert |
| 101 | +import androidx.room.OnConflictStrategy |
| 102 | +import androidx.room.RoomDatabase |
| 103 | +import io.sentry.Sentry |
| 104 | +import io.sentry.SpanStatus |
| 105 | +import kotlinx.coroutines.withContext |
| 106 | + |
| 107 | +@Dao |
| 108 | +abstract class TracksDao { |
| 109 | + @Insert(onConflict = OnConflictStrategy.REPLACE) |
| 110 | + abstract suspend fun insert(track: Track): Long |
| 111 | +} |
| 112 | + |
| 113 | +@Database( |
| 114 | + entities = [Track::class], |
| 115 | + version = 1, |
| 116 | + exportSchema = false |
| 117 | +) |
| 118 | +abstract class TracksDatabase : RoomDatabase() { |
| 119 | + abstract fun tracksDao(): TracksDao |
| 120 | +} |
| 121 | + |
| 122 | +class EditActivity : ComponentActivity() { |
| 123 | + private lateinit var database: TracksDatabase |
| 124 | + |
| 125 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 126 | + super.onCreate(savedInstanceState) |
| 127 | + database = TODO("initialize database...") |
| 128 | + |
| 129 | + findViewById<Button>(R.id.editTrack).setOnClickListener { |
| 130 | + val transaction = Sentry.startTransaction( |
| 131 | + name = "Track Interaction", |
| 132 | + operation = "ui.action.edit", |
| 133 | + bindToScope = true |
| 134 | + ) |
| 135 | + |
| 136 | + val newTrack = Track(/* fill in track values */) |
| 137 | + |
| 138 | + withContext(Dispatchers.IO) { |
| 139 | + database.tracksDao().insert(newTrack) |
| 140 | + transaction.finish(SpanStatus.OK) |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +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: |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +<Note> |
| 152 | + |
| 153 | +At the moment, we only support standard `androidx.room` usage. That is, the SDK will not report SQL queries for any `SupportSQLiteOpenHelper.Factory` other than [androidx.sqlite](https://github.com/androidx/androidx/tree/androidx-main/sqlite). However, if you are using a different `SupportSQLiteOpenHelper.Factory`, please report any [issues on Github](https://github.com/getsentry/sentry-android-gradle-plugin/issues), so we are aware and can possibly work on them. |
| 154 | + |
| 155 | +</Note> |
0 commit comments