Skip to content

Commit b359808

Browse files
authored
feat: Add Room and SQLite integration page (#4574)
1 parent a7d36aa commit b359808

File tree

5 files changed

+159
-20
lines changed

5 files changed

+159
-20
lines changed

src/includes/getting-started-primer/android.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ The SDK builds a crash report that persists to disk and tries to send the report
2929
- Cold and warm app start spans and measurements with [Automatic Instrumentation](/platforms/android/performance/instrumentation/automatic-instrumentation/#app-start-instrumentation)
3030
- Slow and frozen frames measurements with [Automatic Instrumentation](/platforms/android/performance/instrumentation/automatic-instrumentation/#slow-and-frozen-frames)
3131
- OkHttp request spans with [OkHttp Integration](/platforms/android/configuration/integrations/okhttp/)
32-
- SQLite and Room query spans with [Sentry Android Gradle Plugin](/platforms/android/performance/instrumentation/automatic-instrumentation/#sqlite-and-room-instrumentation)
32+
- SQLite and Room query spans with [Room and SQLite Integration](/platforms/android/configuration/integrations/room-and-sqlite/)
3333
- Apollo request spans with [Apollo Integration](/platforms/android/configuration/integrations/apollo/)
3434
- Distributed tracing through [OkHttp](/platforms/android/configuration/integrations/okhttp/) and [Apollo](/platforms/android/configuration/integrations/apollo/) integrations
3535
- [Application Not Responding (ANR)](/platforms/android/configuration/app-not-respond/) reported if the application is blocked for more than five seconds

src/platforms/android/common/gradle.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,8 @@ enum class InstrumentationFeature {
222222
/**
223223
* When enabled the SDK will create spans for any CRUD operation performed by 'androidx.sqlite'
224224
* and 'androidx.room'. This feature uses bytecode manipulation.
225+
*
226+
* Requires sentry-android SDK version 4.0.0 and above
225227
*/
226228
DATABASE,
227229

src/platforms/android/common/performance/instrumentation/automatic-instrumentation.mdx

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,6 @@ For more information see our [Apollo integration](/platforms/android/configurati
201201

202202
### SQLite and Room Instrumentation
203203

204-
<Note>
205-
206-
Supported in Sentry's Android SDK version `4.0.0` and above.
207-
208-
Supported in Sentry Android Gradle Plugin version `3.0.0` and above.
209-
210-
</Note>
211-
212204
The [Sentry Android Gradle Plugin](/platforms/android/gradle/) does the tracing auto instrumentation using bytecode manipulation for `androidx.sqlite` and `androidx.room` libraries.
213205

214206
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.
@@ -217,14 +209,4 @@ The span finishes once the operation has been executed. The span `status` is set
217209

218210
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.
219211

220-
<Note>
221-
222-
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.
223-
224-
</Note>
225-
226-
<Note>
227-
228-
It's recommended to use `androidx.room` version `2.0.0` or above, since it supports incremental builds.
229-
230-
</Note>
212+
For more information see our [Room and SQLite integration](/platforms/android/configuration/integrations/room-and-sqlite/).
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
![Room and AndroidX SQLite performance instrumentation](room-sqlite-instrumentation.png)
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>
Loading

0 commit comments

Comments
 (0)