Room and SQLite
Supported in Sentry's Android SDK version 4.0.0
and above.
Supported in Sentry Android Gradle Plugin version 3.0.0
and above.
The Sentry Android Gradle Plugin provides Room and AndroidX SQLite support through bytecode manipulation. The source can be found on GitHub.
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.
Install
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
:
buildscript {
repositories {
mavenCentral()
}
}
plugins {
id "io.sentry.android.gradle" version "3.4.3"
}
dependencies {
implementation 'io.sentry:sentry-android:6.17.0'
}
Make sure, that performance monitoring is enabled.
Configure
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:
import io.sentry.android.gradle.extensions.InstrumentationFeature
sentry {
tracingInstrumentation {
enabled = true
features = EnumSet.allOf(InstrumentationFeature) - InstrumentationFeature.DATABASE
}
}
Verify
Assuming you have the following (reduced) code snippet performing a database query on a Room Dao:
import android.os.Bundle
import android.widget.Button
import androidx.activity.ComponentActivity
import androidx.room.Database
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.RoomDatabase
import io.sentry.Sentry
import io.sentry.SpanStatus
import kotlinx.coroutines.withContext
@Dao
abstract class TracksDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
abstract suspend fun insert(track: Track): Long
}
@Database(
entities = [Track::class],
version = 1,
exportSchema = false
)
abstract class TracksDatabase : RoomDatabase() {
abstract fun tracksDao(): TracksDao
}
class EditActivity : ComponentActivity() {
private lateinit var database: TracksDatabase
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
database = TODO("initialize database...")
findViewById<Button>(R.id.editTrack).setOnClickListener {
val transaction = Sentry.startTransaction(
name = "Track Interaction",
operation = "ui.action.edit",
bindToScope = true
)
val newTrack = Track(/* fill in track values */)
withContext(Dispatchers.IO) {
database.tracksDao().insert(newTrack)
transaction.finish(SpanStatus.OK)
}
}
}
}
To view the recorded transaction, log into 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:
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. However, if you are using a different SupportSQLiteOpenHelper.Factory
, please report any issues on Github, so we are aware and can possibly work on them.
If you are directly using SupportSQLiteDatabase#query or SupportSQLiteDatabase#execSQL methods through the Room's SQLiteOpenHelper, consider switching to their alternatives that accept bindArgs
as a second parameter.
Because Sentry captures SQL queries as Span description, there is a risk of leaking sensitive data when not using an SQL string with placeholders.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- maven:io.sentry:sentry-android
- Version:
- 6.17.0
- Repository:
- https://github.com/getsentry/sentry-java