Skip to content

Commit

Permalink
Enrich database spans with blocked main thread info (#2760)
Browse files Browse the repository at this point in the history
  • Loading branch information
markushi authored Jun 2, 2023
1 parent 4966d7f commit ff49977
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- Add `lock` attribute to the `SentryStackFrame` protocol to better highlight offending frames in the UI ([#2761](https://github.com/getsentry/sentry-java/pull/2761))
- Enrich database spans with blocked main thread info ([#2760](https://github.com/getsentry/sentry-java/pull/2760))

## 6.21.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import android.database.SQLException
import io.sentry.HubAdapter
import io.sentry.IHub
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.SentryStackTraceFactory
import io.sentry.SpanStatus

internal class SQLiteSpanManager(
private val hub: IHub = HubAdapter.getInstance()
) {
private val stackTraceFactory = SentryStackTraceFactory(hub.options)

init {
SentryIntegrationPackageStorage.getInstance().addIntegration("SQLite")
Expand All @@ -34,7 +36,14 @@ internal class SQLiteSpanManager(
span?.throwable = e
throw e
} finally {
span?.finish()
span?.apply {
val isMainThread: Boolean = hub.options.mainThreadChecker.isMainThread
setData("blocked_main_thread", isMainThread)
if (isMainThread) {
setData("call_stack", stackTraceFactory.inAppCallStack)
}
finish()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@ import io.sentry.SentryOptions
import io.sentry.SentryTracer
import io.sentry.SpanStatus
import io.sentry.TransactionContext
import io.sentry.util.thread.IMainThreadChecker
import org.junit.Before
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

class SQLiteSpanManagerTest {
Expand All @@ -38,6 +41,11 @@ class SQLiteSpanManagerTest {

private val fixture = Fixture()

@Before
fun setup() {
SentryIntegrationPackageStorage.getInstance().clearStorage()
}

@Test
fun `add SQLite to the list of integrations`() {
assertFalse(SentryIntegrationPackageStorage.getInstance().integrations.contains("SQLite"))
Expand Down Expand Up @@ -81,4 +89,32 @@ class SQLiteSpanManagerTest {
assertEquals(e, span.throwable)
assertTrue(span.isFinished)
}

@Test
fun `when performSql runs in background blocked_main_thread is false and no stack trace is attached`() {
val sut = fixture.getSut()

fixture.options.mainThreadChecker = mock<IMainThreadChecker>()
whenever(fixture.options.mainThreadChecker.isMainThread).thenReturn(false)

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

assertFalse(span.getData("blocked_main_thread") as Boolean)
assertNull(span.getData("call_stack"))
}

@Test
fun `when performSql runs in foreground blocked_main_thread is true and a stack trace is attached`() {
val sut = fixture.getSut()

fixture.options.mainThreadChecker = mock<IMainThreadChecker>()
whenever(fixture.options.mainThreadChecker.isMainThread).thenReturn(true)

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

assertTrue(span.getData("blocked_main_thread") as Boolean)
assertNotNull(span.getData("call_stack"))
}
}

0 comments on commit ff49977

Please sign in to comment.