-
-
Notifications
You must be signed in to change notification settings - Fork 472
Expand file tree
/
Copy pathSQLiteSpanManager.kt
More file actions
51 lines (45 loc) · 1.72 KB
/
Copy pathSQLiteSpanManager.kt
File metadata and controls
51 lines (45 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package io.sentry.android.sqlite
import android.database.CrossProcessCursor
import android.database.SQLException
import io.sentry.IScopes
import io.sentry.ScopesAdapter
import io.sentry.SentryIntegrationPackageStorage
import io.sentry.SpanStatus
import io.sentry.sqlite.SQLiteSpanInstrumentation
internal class SQLiteSpanManager(
private val scopes: IScopes = ScopesAdapter.getInstance(),
databaseName: String? = null,
) {
private val spans = SQLiteSpanInstrumentation.fromDatabaseName(databaseName, scopes)
init {
SentryIntegrationPackageStorage.getInstance().addIntegration("SQLite")
}
/**
* Performs a sql operation, creates a span and handles exceptions in case of occurrence.
*
* @param sql The sql query
* @param operation The sql operation to execute. In case of an error the surrounding span will
* have its status set to INTERNAL_ERROR
*/
@Suppress("TooGenericExceptionCaught", "UNCHECKED_CAST")
@Throws(SQLException::class)
fun <T> performSql(sql: String, operation: () -> T): T {
val startTimestamp = spans.startTimestamp()
return try {
val result = operation()
/*
* SQLiteCursor - that extends CrossProcessCursor - executes the query lazily, when one of
* getCount() or onMove() is called. In this case we don't have to start the span here.
* Otherwise we start the span with the timestamp taken before the operation started.
*/
if (result is CrossProcessCursor) {
return SentryCrossProcessCursor(result, this, sql) as T
}
spans.recordCoarseSpan(sql, startTimestamp, SpanStatus.OK)
result
} catch (e: Throwable) {
spans.recordCoarseSpan(sql, startTimestamp, SpanStatus.INTERNAL_ERROR, e)
throw e
}
}
}