Skip to content

feat: Add Room and SQLite integration page #4574

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 12, 2022
Merged
2 changes: 1 addition & 1 deletion src/includes/getting-started-primer/android.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ The SDK builds a crash report that persists to disk and tries to send the report
- Cold and warm app start spans and measurements with [Automatic Instrumentation](/platforms/android/performance/instrumentation/automatic-instrumentation/#app-start-instrumentation)
- Slow and frozen frames measurements with [Automatic Instrumentation](/platforms/android/performance/instrumentation/automatic-instrumentation/#slow-and-frozen-frames)
- OkHttp request spans with [OkHttp Integration](/platforms/android/configuration/integrations/okhttp/)
- SQLite and Room query spans with [Sentry Android Gradle Plugin](/platforms/android/performance/instrumentation/automatic-instrumentation/#sqlite-and-room-instrumentation)
- SQLite and Room query spans with [Room and SQLite Integration](/platforms/android/configuration/integrations/room-and-sqlite/)
- Apollo request spans with [Apollo Integration](/platforms/android/configuration/integrations/apollo/)
- Distributed tracing through [OkHttp](/platforms/android/configuration/integrations/okhttp/) and [Apollo](/platforms/android/configuration/integrations/apollo/) integrations
- [Application Not Responding (ANR)](/platforms/android/configuration/app-not-respond/) reported if the application is blocked for more than five seconds
Expand Down
2 changes: 2 additions & 0 deletions src/platforms/android/common/gradle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ enum class InstrumentationFeature {
/**
* When enabled the SDK will create spans for any CRUD operation performed by 'androidx.sqlite'
* and 'androidx.room'. This feature uses bytecode manipulation.
*
* Requires sentry-android SDK version 4.0.0 and above
*/
DATABASE,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,6 @@ For more information see our [Apollo integration](/platforms/android/configurati

### SQLite and Room Instrumentation

<Note>

Supported in Sentry's Android SDK version `4.0.0` and above.

Supported in Sentry Android Gradle Plugin version `3.0.0` and above.

</Note>

The [Sentry Android Gradle Plugin](/platforms/android/gradle/) does the tracing auto instrumentation using bytecode manipulation for `androidx.sqlite` and `androidx.room` libraries.

The Plugin injects a code snippet that starts a span out of the active span bound to the scope for each `CRUD` operation. The SDK sets the span `operation` to `db` and `description` to the SQL Query if available.
Expand All @@ -217,14 +209,4 @@ The span finishes once the operation has been executed. The span `status` is set

When the operation throws an `Exception`, Sentry's SDK associates this exception to the running span. If you haven't set the SDK to swallow the exception and capture it, the span and SentryEvent will be linked when viewing it on the **Issue Details** page in sentry.io.

<Note>

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.

</Note>

<Note>

It's recommended to use `androidx.room` version `2.0.0` or above, since it supports incremental builds.

</Note>
For more information see our [Room and SQLite integration](/platforms/android/configuration/integrations/room-and-sqlite/).
155 changes: 155 additions & 0 deletions src/platforms/android/configuration/integrations/room-and-sqlite.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
---
title: Room and SQLite
caseStyle: camelCase
supportLevel: production
categories:
- mobile
---

<Note>

Supported in Sentry's Android SDK version `4.0.0` and above.

Supported in Sentry Android Gradle Plugin version `3.0.0` and above.

</Note>

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).

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

```groovy
buildscript {
repositories {
mavenCentral()
}
}

plugins {
id "io.sentry.android.gradle" version "3.0.0-beta.3"
}

dependencies {
implementation 'io.sentry:sentry-android:{{ packages.version('sentry.java.android', '5.0.0') }}'
}
```

```kotlin
buildscript {
repositories {
mavenCentral()
}
}

plugins {
id("io.sentry.android.gradle") version "3.0.0-beta.3"
}

dependencies {
implementation("io.sentry:sentry-android:{{ packages.version('sentry.java.android', '5.0.0') }}")
}
```

<Note>

Make sure, that [performance monitoring](/platforms/android/performance/#configure-the-sample-rate) is enabled.

</Note>

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

```groovy
import io.sentry.android.gradle.InstrumentationFeature

sentry {
tracingInstrumentation {
enabled = true
features = EnumSet.allOf(InstrumentationFeature) - InstrumentationFeature.DATABASE
}
}
```

```kotlin
import java.util.EnumSet
import io.sentry.android.gradle.InstrumentationFeature

sentry {
tracingInstrumentation {
enabled.set(true)
features.set(EnumSet.allOf(InstrumentationFeature::class.java) - InstrumentationFeature.DATABASE)
}
}
```

## Verify

Assuming you have the following (reduced) code snippet performing a database query on a Room Dao:

```kotlin
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](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:

![Room and AndroidX SQLite performance instrumentation](room-sqlite-instrumentation.png)

<Note>

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.

</Note>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.