Merge pull request #2544 from element-hq/feature/bma/trackUtd

Track UTD errors.
This commit is contained in:
Benoit Marty
2024-03-15 14:11:29 +01:00
committed by GitHub
9 changed files with 139 additions and 3 deletions

1
changelog.d/2544.misc Normal file
View File

@@ -0,0 +1 @@
Track UTD errors.

View File

@@ -176,7 +176,7 @@ kotlinpoet = "com.squareup:kotlinpoet:1.16.0"
# Analytics
posthog = "com.posthog:posthog-android:3.1.15"
sentry = "io.sentry:sentry-android:7.6.0"
matrix_analytics_events = "com.github.matrix-org:matrix-analytics-events:0.11.0"
matrix_analytics_events = "com.github.matrix-org:matrix-analytics-events:0.14.0"
# Emojibase
matrix_emojibase_bindings = "io.element.android:emojibase-bindings:1.1.3"

View File

@@ -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)
@@ -52,6 +53,7 @@ dependencies {
testImplementation(libs.test.junit)
testImplementation(libs.test.truth)
testImplementation(projects.libraries.matrix.test)
testImplementation(projects.services.analytics.test)
testImplementation(libs.coroutines.test)
testImplementation(libs.test.turbine)
}

View File

@@ -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(

View File

@@ -0,0 +1,43 @@
/*
* 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}")
val event = Error(
context = null,
// Keep cryptoModule for compatibility.
cryptoModule = Error.CryptoModule.Rust,
cryptoSDK = Error.CryptoSDK.Rust,
timeToDecryptMillis = info.timeToDecryptMs?.toInt() ?: -1,
domain = Error.Domain.E2EE,
// TODO get a more specific error name from `info`
name = Error.Name.OlmKeysNotSentError,
)
analyticsService.capture(event)
}
}

View File

@@ -0,0 +1,64 @@
/*
* 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 com.google.common.truth.Truth.assertThat
import im.vector.app.features.analytics.plan.Error
import io.element.android.libraries.matrix.test.AN_EVENT_ID
import io.element.android.services.analytics.test.FakeAnalyticsService
import org.junit.Test
import org.matrix.rustcomponents.sdk.UnableToDecryptInfo
class UtdTrackerTest {
@Test
fun `when onUtd is called with null timeToDecryptMs, the expected analytics Event is sent`() {
val fakeAnalyticsService = FakeAnalyticsService()
val sut = UtdTracker(fakeAnalyticsService)
sut.onUtd(UnableToDecryptInfo(eventId = AN_EVENT_ID.value, timeToDecryptMs = null))
assertThat(fakeAnalyticsService.capturedEvents).containsExactly(
Error(
context = null,
cryptoModule = Error.CryptoModule.Rust,
cryptoSDK = Error.CryptoSDK.Rust,
timeToDecryptMillis = -1,
domain = Error.Domain.E2EE,
name = Error.Name.OlmKeysNotSentError
)
)
assertThat(fakeAnalyticsService.screenEvents).isEmpty()
assertThat(fakeAnalyticsService.trackedErrors).isEmpty()
}
@Test
fun `when onUtd is called with timeToDecryptMs, the expected analytics Event is sent`() {
val fakeAnalyticsService = FakeAnalyticsService()
val sut = UtdTracker(fakeAnalyticsService)
sut.onUtd(UnableToDecryptInfo(eventId = AN_EVENT_ID.value, timeToDecryptMs = 123.toULong()))
assertThat(fakeAnalyticsService.capturedEvents).containsExactly(
Error(
context = null,
cryptoModule = Error.CryptoModule.Rust,
cryptoSDK = Error.CryptoSDK.Rust,
timeToDecryptMillis = 123,
domain = Error.Domain.E2EE,
name = Error.Name.OlmKeysNotSentError
)
)
assertThat(fakeAnalyticsService.screenEvents).isEmpty()
assertThat(fakeAnalyticsService.trackedErrors).isEmpty()
}
}

View File

@@ -29,10 +29,12 @@ import androidx.core.view.WindowCompat
import io.element.android.compound.theme.ElementTheme
import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService
import io.element.android.libraries.matrix.impl.RustMatrixClientFactory
import io.element.android.libraries.matrix.impl.analytics.UtdTracker
import io.element.android.libraries.matrix.impl.auth.RustMatrixAuthenticationService
import io.element.android.libraries.network.useragent.SimpleUserAgentProvider
import io.element.android.libraries.sessionstorage.api.LoggedInState
import io.element.android.libraries.sessionstorage.impl.memory.InMemorySessionStore
import io.element.android.services.analytics.noop.NoopAnalyticsService
import io.element.android.services.toolbox.impl.systemclock.DefaultSystemClock
import kotlinx.coroutines.runBlocking
import java.io.File
@@ -59,6 +61,7 @@ class MainActivity : ComponentActivity() {
userCertificatesProvider = userCertificatesProvider,
proxyProvider = proxyProvider,
clock = DefaultSystemClock(),
utdTracker = UtdTracker(NoopAnalyticsService()),
),
passphraseGenerator = NullPassphraseGenerator(),
buildMeta = Singleton.buildMeta,

View File

@@ -31,6 +31,7 @@ class FakeAnalyticsService(
private val isEnabledFlow = MutableStateFlow(isEnabled)
private val didAskUserConsentFlow = MutableStateFlow(didAskUserConsent)
val capturedEvents = mutableListOf<VectorAnalyticsEvent>()
val screenEvents = mutableListOf<VectorAnalyticsScreen>()
val trackedErrors = mutableListOf<Throwable>()
override fun getAvailableAnalyticsProviders(): Set<AnalyticsProvider> = emptySet()
@@ -60,6 +61,7 @@ class FakeAnalyticsService(
}
override fun screen(screen: VectorAnalyticsScreen) {
screenEvents += screen
}
override fun updateUserProperties(userProperties: UserProperties) {

View File

@@ -20,6 +20,7 @@ import com.posthog.PostHogInterface
import com.squareup.anvil.annotations.ContributesMultibinding
import im.vector.app.features.analytics.itf.VectorAnalyticsEvent
import im.vector.app.features.analytics.itf.VectorAnalyticsScreen
import im.vector.app.features.analytics.plan.Error
import im.vector.app.features.analytics.plan.UserProperties
import io.element.android.libraries.di.AppScope
import io.element.android.services.analyticsproviders.api.AnalyticsProvider
@@ -56,11 +57,17 @@ class PosthogAnalyticsProvider @Inject constructor(
}
override fun capture(event: VectorAnalyticsEvent) {
posthog?.capture(event.getName(), properties = event.getProperties()?.keepOnlyNonNullValues())
posthog?.capture(
event = event.getName(),
properties = event.getProperties()?.keepOnlyNonNullValues().withExtraProperties(),
)
}
override fun screen(screen: VectorAnalyticsScreen) {
posthog?.screen(screen.getName(), properties = screen.getProperties())
posthog?.screen(
screenTitle = screen.getName(),
properties = screen.getProperties().withExtraProperties(),
)
}
override fun updateUserProperties(userProperties: UserProperties) {
@@ -98,3 +105,14 @@ private fun Map<String, Any?>.keepOnlyNonNullValues(): Map<String, Any> {
}
return result
}
/**
* Properties which will be added to all Events and Screens.
*/
private val extraProperties: Map<String, Any> = mapOf(
"cryptoSDK" to Error.CryptoSDK.Rust
)
private fun Map<String, Any>?.withExtraProperties(): Map<String, Any> {
return orEmpty() + extraProperties
}