Room and SQLite

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 out of the active transaction, bound to the scope for 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:

Copied
buildscript {
  repositories {
    mavenCentral()
  }
}

plugins {
  id "io.sentry.android.gradle" version "3.1.2"
}

dependencies {
  implementation 'io.sentry:sentry-android:6.1.3'
}

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:

Copied
import io.sentry.android.gradle.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:

Copied
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:

Room and AndroidX SQLite performance instrumentation

Help improve this content
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").