-
Notifications
You must be signed in to change notification settings - Fork 768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Track decryption failures #4856
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Analytics: Track Errors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
vector/src/main/java/im/vector/app/features/analytics/DecryptionFailureTracker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright (c) 2022 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 im.vector.app.features.analytics | ||
|
||
import im.vector.app.core.flow.tickerFlow | ||
import im.vector.app.core.time.Clock | ||
import im.vector.app.features.analytics.plan.Error | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.launch | ||
import org.matrix.android.sdk.api.session.crypto.MXCryptoError | ||
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
private data class DecryptionFailure( | ||
val timeStamp: Long, | ||
val roomId: String, | ||
val failedEventId: String, | ||
val error: MXCryptoError.ErrorType | ||
) | ||
|
||
private const val GRACE_PERIOD_MILLIS = 4_000 | ||
private const val CHECK_INTERVAL = 2_000L | ||
|
||
/** | ||
* Tracks decryption errors that are visible to the user. | ||
* When an error is reported it is not directly tracked via analytics, there is a grace period | ||
* that gives the app a few seconds to get the key to decrypt. | ||
*/ | ||
@Singleton | ||
class DecryptionFailureTracker @Inject constructor( | ||
private val vectorAnalytics: VectorAnalytics, | ||
private val clock: Clock | ||
) { | ||
|
||
private val scope: CoroutineScope = CoroutineScope(SupervisorJob()) | ||
private val failures = mutableListOf<DecryptionFailure>() | ||
private val alreadyReported = mutableListOf<String>() | ||
|
||
init { | ||
start() | ||
} | ||
|
||
fun start() { | ||
tickerFlow(scope, CHECK_INTERVAL) | ||
.onEach { | ||
checkFailures() | ||
}.launchIn(scope) | ||
} | ||
|
||
fun stop() { | ||
scope.cancel() | ||
} | ||
|
||
fun e2eEventDisplayedInTimeline(event: TimelineEvent) { | ||
scope.launch(Dispatchers.Default) { | ||
val mCryptoError = event.root.mCryptoError | ||
if (mCryptoError != null) { | ||
addDecryptionFailure(DecryptionFailure(clock.epochMillis(), event.roomId, event.eventId, mCryptoError)) | ||
} else { | ||
removeFailureForEventId(event.eventId) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Can be called when the timeline is disposed in order | ||
* to grace those events as they are not anymore displayed on screen | ||
* */ | ||
fun onTimeLineDisposed(roomId: String) { | ||
scope.launch(Dispatchers.Default) { | ||
synchronized(failures) { | ||
failures.removeIf { it.roomId == roomId } | ||
} | ||
} | ||
} | ||
|
||
private fun addDecryptionFailure(failure: DecryptionFailure) { | ||
// de duplicate | ||
synchronized(failures) { | ||
if (failures.none { it.failedEventId == failure.failedEventId }) { | ||
failures.add(failure) | ||
} | ||
} | ||
} | ||
|
||
private fun removeFailureForEventId(eventId: String) { | ||
synchronized(failures) { | ||
failures.removeIf { it.failedEventId == eventId } | ||
} | ||
} | ||
|
||
private fun checkFailures() { | ||
val now = clock.epochMillis() | ||
val aggregatedErrors: Map<Error.Name, List<String>> | ||
synchronized(failures) { | ||
val toReport = mutableListOf<DecryptionFailure>() | ||
failures.removeAll { failure -> | ||
(now - failure.timeStamp > GRACE_PERIOD_MILLIS).also { | ||
if (it) { | ||
toReport.add(failure) | ||
} | ||
} | ||
} | ||
|
||
aggregatedErrors = toReport | ||
.groupBy { it.error.toAnalyticsErrorName() } | ||
.mapValues { | ||
it.value.map { it.failedEventId } | ||
} | ||
} | ||
|
||
aggregatedErrors.forEach { aggregation -> | ||
// there is now way to send the total/sum in posthog, so iterating | ||
aggregation.value | ||
// for now we ignore events already reported even if displayed again? | ||
.filter { alreadyReported.contains(it).not() } | ||
.forEach { failedEventId -> | ||
vectorAnalytics.capture(Error(failedEventId, Error.Domain.E2EE, aggregation.key)) | ||
alreadyReported.add(failedEventId) | ||
} | ||
} | ||
} | ||
|
||
private fun MXCryptoError.ErrorType.toAnalyticsErrorName(): Error.Name { | ||
return when (this) { | ||
MXCryptoError.ErrorType.UNKNOWN_INBOUND_SESSION_ID -> Error.Name.OlmKeysNotSentError | ||
MXCryptoError.ErrorType.OLM -> { | ||
Error.Name.OlmUnspecifiedError | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a small remark: why a different formatting here? |
||
MXCryptoError.ErrorType.UNKNOWN_MESSAGE_INDEX -> Error.Name.OlmIndexError | ||
else -> Error.Name.UnknownError | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to myself: change to inject the new interface AnalyticsTracker from #4734