|
| 1 | +package io.sentry.sqlite |
| 2 | + |
| 3 | +import io.sentry.IScopes |
| 4 | +import io.sentry.Instrumenter |
| 5 | +import io.sentry.ScopesAdapter |
| 6 | +import io.sentry.SentryDate |
| 7 | +import io.sentry.SentryLongDate |
| 8 | +import io.sentry.SentryStackTraceFactory |
| 9 | +import io.sentry.SpanDataConvention |
| 10 | +import io.sentry.SpanStatus |
| 11 | + |
| 12 | +private const val SQLITE_TRACE_ORIGIN = "auto.db.sqlite" |
| 13 | + |
| 14 | +/** Shared span instrumentation for SQLite. */ |
| 15 | +internal class SQLiteSpanInstrumentation( |
| 16 | + private val scopes: IScopes, |
| 17 | + private val dbMetadata: DbMetadata, |
| 18 | +) { |
| 19 | + |
| 20 | + private val stackTraceFactory = SentryStackTraceFactory(scopes.options) |
| 21 | + |
| 22 | + /** |
| 23 | + * Returns a start timestamp for a `db.sql.query` span. |
| 24 | + * |
| 25 | + * Exposed so callers can capture a wall-clock start before accumulating database time. |
| 26 | + * Internalizing the start time in [recordSpan] would shift spans to end-of-work on the trace |
| 27 | + * timeline, which is less desirable. |
| 28 | + */ |
| 29 | + fun startTimestamp(): SentryDate = scopes.options.dateProvider.now() |
| 30 | + |
| 31 | + /** Records a `db.sql.query` span from [startTimestamp] to the moment of invocation. */ |
| 32 | + fun recordSpan( |
| 33 | + sql: String, |
| 34 | + startTimestamp: SentryDate, |
| 35 | + status: SpanStatus, |
| 36 | + throwable: Throwable? = null, |
| 37 | + ) { |
| 38 | + recordSpan(sql, startTimestamp, endTimestamp = null, status, throwable) |
| 39 | + } |
| 40 | + |
| 41 | + /** Records a `db.sql.query` span from [startTimestamp] to [startTimestamp] + [durationNanos]. */ |
| 42 | + fun recordSpan( |
| 43 | + sql: String, |
| 44 | + startTimestamp: SentryDate, |
| 45 | + durationNanos: Long, |
| 46 | + status: SpanStatus, |
| 47 | + throwable: Throwable? = null, |
| 48 | + ) { |
| 49 | + val endTimestamp = SentryLongDate(startTimestamp.nanoTimestamp() + durationNanos) |
| 50 | + recordSpan(sql, startTimestamp, endTimestamp, status, throwable) |
| 51 | + } |
| 52 | + |
| 53 | + private fun recordSpan( |
| 54 | + sql: String, |
| 55 | + startTimestamp: SentryDate, |
| 56 | + endTimestamp: SentryDate?, |
| 57 | + status: SpanStatus, |
| 58 | + throwable: Throwable?, |
| 59 | + ) { |
| 60 | + scopes.span?.startChild("db.sql.query", sql, startTimestamp, Instrumenter.SENTRY)?.apply { |
| 61 | + spanContext.origin = SQLITE_TRACE_ORIGIN |
| 62 | + throwable?.let { this.throwable = it } |
| 63 | + |
| 64 | + val isMainThread = scopes.options.threadChecker.isMainThread |
| 65 | + setData(SpanDataConvention.BLOCKED_MAIN_THREAD_KEY, isMainThread) |
| 66 | + |
| 67 | + if (isMainThread) { |
| 68 | + setData(SpanDataConvention.CALL_STACK_KEY, stackTraceFactory.inAppCallStack) |
| 69 | + } |
| 70 | + |
| 71 | + dbMetadata.name?.let { setData(SpanDataConvention.DB_NAME_KEY, it) } |
| 72 | + setData(SpanDataConvention.DB_SYSTEM_KEY, dbMetadata.system) |
| 73 | + finish(status, endTimestamp) |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + companion object { |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns [SQLiteSpanInstrumentation] based on the [fileName] argument passed to |
| 81 | + * [SQLiteDriver.open][androidx.sqlite.SQLiteDriver.open]. |
| 82 | + */ |
| 83 | + fun fromFileName( |
| 84 | + fileName: String, |
| 85 | + scopes: IScopes = ScopesAdapter.getInstance(), |
| 86 | + ): SQLiteSpanInstrumentation = |
| 87 | + SQLiteSpanInstrumentation(scopes, dbMetadataFromFileName(fileName)) |
| 88 | + |
| 89 | + /** |
| 90 | + * Returns [SQLiteSpanInstrumentation] based on |
| 91 | + * [SupportSQLiteOpenHelper.databaseName][androidx.sqlite.db.SupportSQLiteOpenHelper.databaseName]. |
| 92 | + */ |
| 93 | + fun fromDatabaseName( |
| 94 | + databaseName: String?, |
| 95 | + scopes: IScopes = ScopesAdapter.getInstance(), |
| 96 | + ): SQLiteSpanInstrumentation = |
| 97 | + SQLiteSpanInstrumentation(scopes, dbMetadataFromDatabaseName(databaseName)) |
| 98 | + } |
| 99 | +} |
0 commit comments