Add Sentry transaction so we can check how long it takes to vacuum and if there were any errors when doing it

This commit is contained in:
Jorge Martín
2025-12-10 08:17:57 +01:00
committed by Jorge Martin Espinosa
parent 09cab10abc
commit 7e3acd6b58
4 changed files with 22 additions and 11 deletions

View File

@@ -21,6 +21,8 @@ import io.element.android.libraries.matrix.api.MatrixClientProvider
import io.element.android.libraries.matrix.api.core.SessionId import io.element.android.libraries.matrix.api.core.SessionId
import io.element.android.libraries.workmanager.api.di.MetroWorkerFactory import io.element.android.libraries.workmanager.api.di.MetroWorkerFactory
import io.element.android.libraries.workmanager.api.di.WorkerKey import io.element.android.libraries.workmanager.api.di.WorkerKey
import io.element.android.services.analytics.api.AnalyticsService
import io.element.android.services.analytics.api.recordTransaction
import timber.log.Timber import timber.log.Timber
@AssistedInject @AssistedInject
@@ -28,6 +30,7 @@ class VacuumDatabaseWorker(
@Assisted workerParams: WorkerParameters, @Assisted workerParams: WorkerParameters,
@ApplicationContext private val context: Context, @ApplicationContext private val context: Context,
private val matrixClientProvider: MatrixClientProvider, private val matrixClientProvider: MatrixClientProvider,
private val analyticsService: AnalyticsService,
) : CoroutineWorker(context, workerParams) { ) : CoroutineWorker(context, workerParams) {
companion object { companion object {
const val SESSION_ID_PARAM = "session_id" const val SESSION_ID_PARAM = "session_id"
@@ -37,17 +40,20 @@ class VacuumDatabaseWorker(
Timber.d("Starting database vacuuming...") Timber.d("Starting database vacuuming...")
val sessionId = inputData.getString(SESSION_ID_PARAM)?.let(::SessionId) ?: return Result.failure() val sessionId = inputData.getString(SESSION_ID_PARAM)?.let(::SessionId) ?: return Result.failure()
val client = matrixClientProvider.getOrRestore(sessionId).getOrNull() ?: return Result.failure() val client = matrixClientProvider.getOrRestore(sessionId).getOrNull() ?: return Result.failure()
return client.performDatabaseVacuum() return analyticsService.recordTransaction("Vacuuming DBs", "vacuuming") { transaction ->
.fold( client.performDatabaseVacuum()
onSuccess = { .fold(
Timber.d("Database vacuuming finished successfully") onSuccess = {
Result.success() Timber.d("Database vacuuming finished successfully")
}, Result.success()
onFailure = { },
Timber.e(it, "Database vacuuming failed") onFailure = { error ->
Result.failure() transaction.attachError(error)
} Timber.e(error, "Database vacuuming failed")
) Result.failure()
}
)
}
} }
@ContributesIntoMap(AppScope::class, binding = binding<MetroWorkerFactory.WorkerInstanceFactory<*>>()) @ContributesIntoMap(AppScope::class, binding = binding<MetroWorkerFactory.WorkerInstanceFactory<*>>())

View File

@@ -14,5 +14,6 @@ object NoopAnalyticsTransaction : AnalyticsTransaction {
override fun setData(key: String, value: Any) {} override fun setData(key: String, value: Any) {}
override fun isFinished(): Boolean = true override fun isFinished(): Boolean = true
override fun traceId(): String? = null override fun traceId(): String? = null
override fun attachError(throwable: Throwable) {}
override fun finish() {} override fun finish() {}
} }

View File

@@ -12,6 +12,7 @@ interface AnalyticsTransaction {
fun setData(key: String, value: Any) fun setData(key: String, value: Any)
fun isFinished(): Boolean fun isFinished(): Boolean
fun traceId(): String? fun traceId(): String?
fun attachError(throwable: Throwable)
fun finish() fun finish()
} }

View File

@@ -23,6 +23,9 @@ class SentryAnalyticsTransaction private constructor(span: ISpan) : AnalyticsTra
override fun setData(key: String, value: Any) = inner.setData(key, value) override fun setData(key: String, value: Any) = inner.setData(key, value)
override fun traceId(): String? = inner.toSentryTrace().value override fun traceId(): String? = inner.toSentryTrace().value
override fun isFinished(): Boolean = inner.isFinished override fun isFinished(): Boolean = inner.isFinished
override fun attachError(throwable: Throwable) {
inner.throwable = throwable
}
override fun finish() { override fun finish() {
val name = if (inner is ITransaction) inner.name else inner.operation val name = if (inner is ITransaction) inner.name else inner.operation
Timber.d("Finishing transaction: $name") Timber.d("Finishing transaction: $name")