Skip to content
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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send db.system and db.name in span data for androidx.sqlite spans #2928

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Send `db.system` and `db.name` in span data for androidx.sqlite spans ([#2928](https://github.com/getsentry/sentry-java/pull/2928))

### Dependencies

- Bump Gradle from v8.2.1 to v8.3.0 ([#2900](https://github.com/getsentry/sentry-java/pull/2900))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import io.sentry.SpanStatus
private const val TRACE_ORIGIN = "auto.db.sqlite"

internal class SQLiteSpanManager(
private val hub: IHub = HubAdapter.getInstance()
private val hub: IHub = HubAdapter.getInstance(),
private val databaseName: String? = null
) {
private val stackTraceFactory = SentryStackTraceFactory(hub.options)

Expand Down Expand Up @@ -46,6 +47,15 @@ internal class SQLiteSpanManager(
if (isMainThread) {
setData(SpanDataConvention.CALL_STACK_KEY, stackTraceFactory.inAppCallStack)
}
// if db name is null, then it's an in-memory database as per
// https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:sqlite/sqlite/src/main/java/androidx/sqlite/db/SupportSQLiteOpenHelper.kt;l=38-42
if (databaseName != null) {
setData(SpanDataConvention.DB_SYSTEM_KEY, "sqlite")
setData(SpanDataConvention.DB_NAME_KEY, databaseName)
} else {
setData(SpanDataConvention.DB_SYSTEM_KEY, "in-memory")
}

finish()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SentrySupportSQLiteOpenHelper private constructor(
private val delegate: SupportSQLiteOpenHelper
) : SupportSQLiteOpenHelper by delegate {

private val sqLiteSpanManager = SQLiteSpanManager()
private val sqLiteSpanManager = SQLiteSpanManager(databaseName = delegate.databaseName)

private val sentryWritableDatabase: SupportSQLiteDatabase by lazy {
SentrySupportSQLiteDatabase(delegate.writableDatabase, sqLiteSpanManager)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SQLiteSpanManagerTest {
lateinit var sentryTracer: SentryTracer
lateinit var options: SentryOptions

fun getSut(isSpanActive: Boolean = true): SQLiteSpanManager {
fun getSut(isSpanActive: Boolean = true, databaseName: String? = null): SQLiteSpanManager {
options = SentryOptions().apply {
dsn = "https://key@sentry.io/proj"
}
Expand All @@ -36,7 +36,7 @@ class SQLiteSpanManagerTest {
if (isSpanActive) {
whenever(hub.span).thenReturn(sentryTracer)
}
return SQLiteSpanManager(hub)
return SQLiteSpanManager(hub, databaseName)
}
}

Expand Down Expand Up @@ -119,4 +119,25 @@ class SQLiteSpanManagerTest {
assertTrue(span.getData(SpanDataConvention.BLOCKED_MAIN_THREAD_KEY) as Boolean)
assertNotNull(span.getData(SpanDataConvention.CALL_STACK_KEY))
}

@Test
fun `when databaseName is provided, sets system and name as span data`() {
val sut = fixture.getSut(databaseName = "tracks.db")

sut.performSql("sql") {}
val span = fixture.sentryTracer.children.first()

assertEquals(span.data[SpanDataConvention.DB_SYSTEM_KEY], "sqlite")
assertEquals(span.data[SpanDataConvention.DB_NAME_KEY], "tracks.db")
}

@Test
fun `when databaseName is null, sets system to in-memory`() {
val sut = fixture.getSut()

sut.performSql("sql") {}
val span = fixture.sentryTracer.children.first()

assertEquals(span.data[SpanDataConvention.DB_SYSTEM_KEY], "in-memory")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.sentry.android.sqlite

import androidx.sqlite.db.SupportSQLiteOpenHelper
import org.mockito.kotlin.mock
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import kotlin.test.Test
Expand Down Expand Up @@ -37,7 +38,7 @@ class SentrySupportSQLiteOpenHelperTest {
verify(fixture.mockOpenHelper).readableDatabase

openHelper.databaseName
verify(fixture.mockOpenHelper).databaseName
verify(fixture.mockOpenHelper, times(2)).databaseName

openHelper.close()
verify(fixture.mockOpenHelper).close()
Expand Down