Skip to content

Commit 4ca2ccc

Browse files
committed
refactor(android-sqlite): Rename classes instrumenting SQLite spans for consistency
1 parent 06b0d80 commit 4ca2ccc

16 files changed

Lines changed: 82 additions & 94 deletions

sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SQLiteSpanManager.kt renamed to sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/OpenHelperSpans.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import io.sentry.SpanStatus
1313

1414
private const val TRACE_ORIGIN = "auto.db.sqlite"
1515

16-
internal class SQLiteSpanManager(
16+
/** Span instrumentation for [SentrySupportSQLiteOpenHelper]. */
17+
internal class OpenHelperSpans(
1718
private val scopes: IScopes = ScopesAdapter.getInstance(),
1819
private val databaseName: String? = null,
1920
) {

sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SentryCrossProcessCursor.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import android.database.CursorWindow
1313
*/
1414
internal class SentryCrossProcessCursor(
1515
private val delegate: CrossProcessCursor,
16-
private val spanManager: SQLiteSpanManager,
16+
private val spans: OpenHelperSpans,
1717
private val sql: String,
1818
) : CrossProcessCursor by delegate {
1919
// We have to start the span only the first time, regardless of how many times its methods get
@@ -25,22 +25,22 @@ internal class SentryCrossProcessCursor(
2525
return delegate.count
2626
}
2727
isSpanStarted = true
28-
return spanManager.performSql(sql) { delegate.count }
28+
return spans.performSql(sql) { delegate.count }
2929
}
3030

3131
override fun onMove(oldPosition: Int, newPosition: Int): Boolean {
3232
if (isSpanStarted) {
3333
return delegate.onMove(oldPosition, newPosition)
3434
}
3535
isSpanStarted = true
36-
return spanManager.performSql(sql) { delegate.onMove(oldPosition, newPosition) }
36+
return spans.performSql(sql) { delegate.onMove(oldPosition, newPosition) }
3737
}
3838

3939
override fun fillWindow(position: Int, window: CursorWindow?) {
4040
if (isSpanStarted) {
4141
return delegate.fillWindow(position, window)
4242
}
4343
isSpanStarted = true
44-
return spanManager.performSql(sql) { delegate.fillWindow(position, window) }
44+
return spans.performSql(sql) { delegate.fillWindow(position, window) }
4545
}
4646
}

sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SentrySupportSQLiteDatabase.kt

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import androidx.sqlite.db.SupportSQLiteStatement
1414
* and it's created automatically by the [SentrySupportSQLiteOpenHelper].
1515
*
1616
* @param delegate The [SupportSQLiteDatabase] instance to delegate calls to.
17-
* @param sqLiteSpanManager The [SQLiteSpanManager] responsible for the creation of the spans.
17+
* @param spans The [OpenHelperSpans] manager responsible for the creation of the spans.
1818
*/
1919
internal class SentrySupportSQLiteDatabase(
2020
private val delegate: SupportSQLiteDatabase,
21-
private val sqLiteSpanManager: SQLiteSpanManager,
21+
private val spans: OpenHelperSpans,
2222
) : SupportSQLiteDatabase by delegate {
2323
/**
2424
* Compiles the given SQL statement. It will return Sentry's wrapper around
@@ -28,35 +28,34 @@ internal class SentrySupportSQLiteDatabase(
2828
* @return Compiled statement.
2929
*/
3030
override fun compileStatement(sql: String): SupportSQLiteStatement =
31-
SentrySupportSQLiteStatement(delegate.compileStatement(sql), sqLiteSpanManager, sql)
31+
SentrySupportSQLiteStatement(delegate.compileStatement(sql), spans, sql)
3232

3333
@Suppress("AcronymName") // To keep consistency with framework method name.
3434
override fun execPerConnectionSQL(
3535
sql: String,
3636
@SuppressLint("ArrayReturn") bindArgs: Array<out Any?>?,
3737
) {
38-
sqLiteSpanManager.performSql(sql) { delegate.execPerConnectionSQL(sql, bindArgs) }
38+
spans.performSql(sql) { delegate.execPerConnectionSQL(sql, bindArgs) }
3939
}
4040

41-
override fun query(query: String): Cursor =
42-
sqLiteSpanManager.performSql(query) { delegate.query(query) }
41+
override fun query(query: String): Cursor = spans.performSql(query) { delegate.query(query) }
4342

4443
override fun query(query: String, bindArgs: Array<out Any?>): Cursor =
45-
sqLiteSpanManager.performSql(query) { delegate.query(query, bindArgs) }
44+
spans.performSql(query) { delegate.query(query, bindArgs) }
4645

4746
override fun query(query: SupportSQLiteQuery): Cursor =
48-
sqLiteSpanManager.performSql(query.sql) { delegate.query(query) }
47+
spans.performSql(query.sql) { delegate.query(query) }
4948

5049
override fun query(query: SupportSQLiteQuery, cancellationSignal: CancellationSignal?): Cursor =
51-
sqLiteSpanManager.performSql(query.sql) { delegate.query(query, cancellationSignal) }
50+
spans.performSql(query.sql) { delegate.query(query, cancellationSignal) }
5251

5352
@Throws(SQLException::class)
5453
override fun execSQL(sql: String) {
55-
sqLiteSpanManager.performSql(sql) { delegate.execSQL(sql) }
54+
spans.performSql(sql) { delegate.execSQL(sql) }
5655
}
5756

5857
@Throws(SQLException::class)
5958
override fun execSQL(sql: String, bindArgs: Array<out Any?>) {
60-
sqLiteSpanManager.performSql(sql) { delegate.execSQL(sql, bindArgs) }
59+
spans.performSql(sql) { delegate.execSQL(sql, bindArgs) }
6160
}
6261
}

sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SentrySupportSQLiteOpenHelper.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ import androidx.sqlite.db.SupportSQLiteOpenHelper
3333
public class SentrySupportSQLiteOpenHelper
3434
private constructor(private val delegate: SupportSQLiteOpenHelper) :
3535
SupportSQLiteOpenHelper by delegate {
36-
private val sqLiteSpanManager = SQLiteSpanManager(databaseName = delegate.databaseName)
36+
private val spans = OpenHelperSpans(databaseName = delegate.databaseName)
3737

3838
private val sentryWritableDatabase: SupportSQLiteDatabase by lazy {
39-
SentrySupportSQLiteDatabase(delegate.writableDatabase, sqLiteSpanManager)
39+
SentrySupportSQLiteDatabase(delegate.writableDatabase, spans)
4040
}
4141

4242
private val sentryReadableDatabase: SupportSQLiteDatabase by lazy {
43-
SentrySupportSQLiteDatabase(delegate.readableDatabase, sqLiteSpanManager)
43+
SentrySupportSQLiteDatabase(delegate.readableDatabase, spans)
4444
}
4545

4646
override val writableDatabase: SupportSQLiteDatabase

sentry-android-sqlite/src/main/java/io/sentry/android/sqlite/SentrySupportSQLiteStatement.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,22 @@ import androidx.sqlite.db.SupportSQLiteStatement
99
* [SentrySupportSQLiteDatabase.compileStatement].
1010
*
1111
* @param delegate The [SupportSQLiteStatement] instance to delegate calls to.
12-
* @param sqLiteSpanManager The [SQLiteSpanManager] responsible for the creation of the spans.
12+
* @param spans The [OpenHelperSpans] manager responsible for the creation of the spans.
1313
* @param sql The query string.
1414
*/
1515
internal class SentrySupportSQLiteStatement(
1616
private val delegate: SupportSQLiteStatement,
17-
private val sqLiteSpanManager: SQLiteSpanManager,
17+
private val spans: OpenHelperSpans,
1818
private val sql: String,
1919
) : SupportSQLiteStatement by delegate {
20-
override fun execute() = sqLiteSpanManager.performSql(sql) { delegate.execute() }
20+
override fun execute() = spans.performSql(sql) { delegate.execute() }
2121

22-
override fun executeUpdateDelete(): Int =
23-
sqLiteSpanManager.performSql(sql) { delegate.executeUpdateDelete() }
22+
override fun executeUpdateDelete(): Int = spans.performSql(sql) { delegate.executeUpdateDelete() }
2423

25-
override fun executeInsert(): Long =
26-
sqLiteSpanManager.performSql(sql) { delegate.executeInsert() }
24+
override fun executeInsert(): Long = spans.performSql(sql) { delegate.executeInsert() }
2725

28-
override fun simpleQueryForLong(): Long =
29-
sqLiteSpanManager.performSql(sql) { delegate.simpleQueryForLong() }
26+
override fun simpleQueryForLong(): Long = spans.performSql(sql) { delegate.simpleQueryForLong() }
3027

3128
override fun simpleQueryForString(): String? =
32-
sqLiteSpanManager.performSql(sql) { delegate.simpleQueryForString() }
29+
spans.performSql(sql) { delegate.simpleQueryForString() }
3330
}

sentry-android-sqlite/src/main/java/io/sentry/sqlite/SQLiteSpanInstrumentation.kt renamed to sentry-android-sqlite/src/main/java/io/sentry/sqlite/DriverSpans.kt

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,17 @@ private const val SQLITE_TRACE_ORIGIN = "auto.db.sqlite"
2121
private val EMPTY_NANO_TIME = SentryNanotimeDate(Date(0), 0L)
2222

2323
/** Span instrumentation for [SentrySQLiteDriver]. */
24-
internal class SQLiteSpanInstrumentation(
25-
private val scopes: IScopes,
26-
private val dbMetadata: DbMetadata,
27-
) {
24+
internal class DriverSpans(private val scopes: IScopes, private val dbMetadata: DbMetadata) {
2825

2926
private val stackTraceFactory = SentryStackTraceFactory(scopes.options)
3027

3128
/**
32-
* Returns a timestamp in nanoseconds for use with [recordSpan]. Timestamp is ns-precise if the
33-
* active parent span uses a [SentryNanotimeDate] (the ordinary case); otherwise it's ms-precise.
29+
* Returns a timestamp in nanoseconds for use with [record]. Timestamp is ns-precise if the active
30+
* parent span uses a [SentryNanotimeDate] (the ordinary case); otherwise it's ms-precise.
3431
*
35-
* Note: Internalizing the start time in [recordSpan] would shift spans to end-of-work on the
36-
* trace timeline, which is less desirable; callers capture the start before doing database work
37-
* and pass it back to [recordSpan].
32+
* Note: Internalizing the start time in [record] would shift spans to end-of-work on the trace
33+
* timeline, which is less desirable; callers capture the start before doing database work and
34+
* pass it back to [record].
3835
*/
3936
fun startTimestamp(): Long =
4037
// Try to retain nanosecond precision + avoid SentryDate allocation...
@@ -43,7 +40,7 @@ internal class SQLiteSpanInstrumentation(
4340
?: scopes.options.dateProvider.now().nanoTimestamp()
4441

4542
/** Records a `db.sql.query` span. */
46-
fun recordSpan(
43+
fun record(
4744
sql: String,
4845
startTimestampNanos: Long,
4946
durationNanos: Long,
@@ -74,14 +71,11 @@ internal class SQLiteSpanInstrumentation(
7471
companion object {
7572

7673
/**
77-
* Returns [SQLiteSpanInstrumentation] based on the [fileName] argument passed to
74+
* Returns [DriverSpans] based on the [fileName] argument passed to
7875
* [SQLiteDriver.open][androidx.sqlite.SQLiteDriver.open].
7976
*/
80-
fun fromFileName(
81-
fileName: String,
82-
scopes: IScopes = ScopesAdapter.getInstance(),
83-
): SQLiteSpanInstrumentation =
84-
SQLiteSpanInstrumentation(scopes, dbMetadataFromFileName(fileName))
77+
fun fromFileName(fileName: String, scopes: IScopes = ScopesAdapter.getInstance()): DriverSpans =
78+
DriverSpans(scopes, dbMetadataFromFileName(fileName))
8579
}
8680
}
8781

sentry-android-sqlite/src/main/java/io/sentry/sqlite/SentrySQLiteConnection.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import androidx.sqlite.SQLiteStatement
55

66
internal class SentrySQLiteConnection(
77
private val delegate: SQLiteConnection,
8-
private val spans: SQLiteSpanInstrumentation,
8+
private val spans: DriverSpans,
99
) : SQLiteConnection by delegate {
1010

1111
override fun prepare(sql: String): SQLiteStatement {

sentry-android-sqlite/src/main/java/io/sentry/sqlite/SentrySQLiteDriver.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal class SentrySQLiteDriver private constructor(private val delegate: SQLi
4949
val connection = delegate.open(fileName)
5050

5151
return try {
52-
val spans = SQLiteSpanInstrumentation.fromFileName(fileName)
52+
val spans = DriverSpans.fromFileName(fileName)
5353
// create() ensures delegate is unwrapped, so we don't need to protect against double-wrapping
5454
// the connection.
5555
SentrySQLiteConnection(connection, spans)

sentry-android-sqlite/src/main/java/io/sentry/sqlite/SentrySQLiteStatement.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import io.sentry.SpanStatus
1616
*/
1717
internal class SentrySQLiteStatement(
1818
private val delegate: SQLiteStatement,
19-
private val spans: SQLiteSpanInstrumentation,
19+
private val spans: DriverSpans,
2020
private val sql: String,
2121
private val nanoTimeProvider: () -> Long = { System.nanoTime() },
2222
) : SQLiteStatement by delegate {
@@ -74,6 +74,6 @@ internal class SentrySQLiteStatement(
7474
val duration = accumulatedDbNanos
7575
firstStepTimestampNanos = null
7676
accumulatedDbNanos = 0L
77-
spans.recordSpan(sql, startNanos, duration, status, throwable)
77+
spans.record(sql, startNanos, duration, status, throwable)
7878
}
7979
}

sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/SQLiteSpanManagerTest.kt renamed to sentry-android-sqlite/src/test/java/io/sentry/android/sqlite/OpenHelperSpansTest.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,21 @@ import org.junit.Before
2121
import org.mockito.kotlin.mock
2222
import org.mockito.kotlin.whenever
2323

24-
class SQLiteSpanManagerTest {
24+
class OpenHelperSpansTest {
2525
private class Fixture {
2626
private val scopes = mock<IScopes>()
2727
lateinit var sentryTracer: SentryTracer
2828
lateinit var options: SentryOptions
2929

30-
fun getSut(isSpanActive: Boolean = true, databaseName: String? = null): SQLiteSpanManager {
30+
fun getSut(isSpanActive: Boolean = true, databaseName: String? = null): OpenHelperSpans {
3131
options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" }
3232
whenever(scopes.options).thenReturn(options)
3333
sentryTracer = SentryTracer(TransactionContext("name", "op"), scopes)
3434

3535
if (isSpanActive) {
3636
whenever(scopes.span).thenReturn(sentryTracer)
3737
}
38-
return SQLiteSpanManager(scopes, databaseName)
38+
return OpenHelperSpans(scopes, databaseName)
3939
}
4040
}
4141

0 commit comments

Comments
 (0)