From 070200546843f7d06b5983a3e7cb711add3436e4 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Thu, 14 Mar 2024 16:17:54 +0100 Subject: [PATCH] Track UTD errors. --- changelog.d/2544.misc | 1 + libraries/matrix/impl/build.gradle.kts | 1 + .../matrix/impl/RustMatrixClientFactory.kt | 3 ++ .../matrix/impl/analytics/UtdTracker.kt | 41 +++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 changelog.d/2544.misc create mode 100644 libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/analytics/UtdTracker.kt diff --git a/changelog.d/2544.misc b/changelog.d/2544.misc new file mode 100644 index 0000000000..2d64ba871e --- /dev/null +++ b/changelog.d/2544.misc @@ -0,0 +1 @@ + Track UTD errors. diff --git a/libraries/matrix/impl/build.gradle.kts b/libraries/matrix/impl/build.gradle.kts index 8815214dd6..1f807a0120 100644 --- a/libraries/matrix/impl/build.gradle.kts +++ b/libraries/matrix/impl/build.gradle.kts @@ -39,6 +39,7 @@ dependencies { implementation(projects.libraries.di) implementation(projects.libraries.androidutils) implementation(projects.libraries.network) + implementation(projects.services.analytics.api) implementation(projects.services.toolbox.api) implementation(projects.libraries.featureflag.api) api(projects.libraries.matrix.api) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt index 64f152cd9c..80302933d5 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt @@ -18,6 +18,7 @@ package io.element.android.libraries.matrix.impl import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.di.CacheDirectory +import io.element.android.libraries.matrix.impl.analytics.UtdTracker import io.element.android.libraries.matrix.impl.certificates.UserCertificatesProvider import io.element.android.libraries.matrix.impl.proxy.ProxyProvider import io.element.android.libraries.network.useragent.UserAgentProvider @@ -42,6 +43,7 @@ class RustMatrixClientFactory @Inject constructor( private val userCertificatesProvider: UserCertificatesProvider, private val proxyProvider: ProxyProvider, private val clock: SystemClock, + private val utdTracker: UtdTracker, ) { suspend fun create(sessionData: SessionData): RustMatrixClient = withContext(coroutineDispatchers.io) { val client = ClientBuilder() @@ -68,6 +70,7 @@ class RustMatrixClientFactory @Inject constructor( client.restoreSession(sessionData.toSession()) val syncService = client.syncService() + .withUtdHook(utdTracker) .finish() RustMatrixClient( diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/analytics/UtdTracker.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/analytics/UtdTracker.kt new file mode 100644 index 0000000000..8c9180a8ee --- /dev/null +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/analytics/UtdTracker.kt @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2024 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.element.android.libraries.matrix.impl.analytics + +import im.vector.app.features.analytics.plan.Error +import io.element.android.services.analytics.api.AnalyticsService +import org.matrix.rustcomponents.sdk.UnableToDecryptDelegate +import org.matrix.rustcomponents.sdk.UnableToDecryptInfo +import timber.log.Timber +import javax.inject.Inject + +class UtdTracker @Inject constructor( + private val analyticsService: AnalyticsService, +) : UnableToDecryptDelegate { + override fun onUtd(info: UnableToDecryptInfo) { + Timber.d("onUtd for event ${info.eventId}, timeToDecryptMs: ${info.timeToDecryptMs}") + // TODO info will contain more information in the future, so that the app can report more precise data to the analytics. + val event = Error( + context = null, + cryptoModule = Error.CryptoModule.Rust, + domain = Error.Domain.E2EE, + // TODO get a more specific error name from `info` + name = Error.Name.OlmKeysNotSentError, + ) + analyticsService.capture(event) + } +}