MESSENGER-5265 add decryption time events matomo

This commit is contained in:
JanNiklas Grabowski
2023-11-09 16:47:43 +01:00
parent 3909b30382
commit 63f3b96ad3
2 changed files with 91 additions and 0 deletions

View File

@@ -22,6 +22,10 @@
// Call `checkFailures` every `CHECK_INTERVAL`
#define CHECK_INTERVAL 2
// bwi: 5265
// Call 'checkDidDecrypt' every 'CHECK_SUCCESS_INTERVAL'
#define CHECK_DECRYPTED_EVENT_INTERVAL 10
// Give events a chance to be decrypted by waiting `GRACE_PERIOD` before counting
// and reporting them as failures
#define GRACE_PERIOD 4
@@ -35,12 +39,23 @@ NSString *const kDecryptionFailureTrackerAnalyticsCategory = @"e2e.failure";
// Every `CHECK_INTERVAL`, this list is checked for failures that happened
// more than`GRACE_PERIOD` ago. Those that did are reported to the delegate.
NSMutableDictionary<NSString* /* eventId */, DecryptionFailure*> *reportedFailures;
// bwi: 5265
// Reported temporary failures that did decrypt
// Every 'CHECK_DECRYPTED_EVENT_INTERVAL', this list is checked for temporary (inbound_session_id only) failures that did decrypt.
// Those that did will be aggregated and reported to the delegate.
NSMutableDictionary<NSString* /* eventId */, DecryptedEvent*> *decryptedEvents;
// Reported temporary failures
NSMutableDictionary<NSString* /* eventId */, DecryptionFailure*> *temporaryFailures;
// Event ids of failures that were tracked previously
NSMutableSet<NSString*> *trackedEvents;
// Timer for periodic check
NSTimer *checkFailuresTimer;
// bwi: 5265
NSTimer *checkDecryptedEventsTimer;
}
@end
@@ -65,6 +80,10 @@ NSString *const kDecryptionFailureTrackerAnalyticsCategory = @"e2e.failure";
{
reportedFailures = [NSMutableDictionary dictionary];
trackedEvents = [NSMutableSet set];
// bwi: 5265
decryptedEvents = [NSMutableDictionary dictionary];
temporaryFailures = [NSMutableDictionary dictionary];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(eventDidDecrypt:) name:kMXEventDidDecryptNotification object:nil];
@@ -73,6 +92,13 @@ NSString *const kDecryptionFailureTrackerAnalyticsCategory = @"e2e.failure";
selector:@selector(checkFailures)
userInfo:nil
repeats:YES];
// bwi: 5265 add tracking for decryption duration (only for MXDecryptingErrorUnknownInboundSessionIdCode)
checkDecryptedEventsTimer = [NSTimer scheduledTimerWithTimeInterval:CHECK_DECRYPTED_EVENT_INTERVAL
target:self
selector:@selector(checkDecryptedEvents)
userInfo:nil
repeats:YES];
}
return self;
}
@@ -120,11 +146,23 @@ NSString *const kDecryptionFailureTrackerAnalyticsCategory = @"e2e.failure";
errorCode:event.decryptionError.code
deviceCount:deviceCount
unspecifiedErrorMessage:event.decryptionError.localizedDescription];
// bwi: 5265 add tracking for decryption duration (only for MXDecryptingErrorUnknownInboundSessionIdCode)
if (!temporaryFailures[event.eventId] && event.decryptionError.code == MXDecryptingErrorUnknownInboundSessionIdCode)
{
temporaryFailures[event.eventId] = [[DecryptionFailure alloc] initWithFailedEventId:failedEventId
reason:reason
context:context
errorCode:event.decryptionError.code
deviceCount:deviceCount
unspecifiedErrorMessage:event.decryptionError.localizedDescription];
}
}
- (void)dispatch
{
[self checkFailures];
[self checkDecryptedEvents];
}
#pragma mark - Private methods
@@ -176,10 +214,57 @@ NSString *const kDecryptionFailureTrackerAnalyticsCategory = @"e2e.failure";
}
}
// bwi: 5265 add tracking for decryption duration (only for MXDecryptingErrorUnknownInboundSessionIdCode)
- (void)checkDecryptedEvents
{
if (!_delegate)
{
return;
}
if (decryptedEvents.count > 0)
{
NSInteger eventsCount = decryptedEvents.count;
NSTimeInterval eventMaxTimeToDecrypt = 0;
NSTimeInterval eventAvgTimeToDecrypt = 0;
for (DecryptedEvent *decryptedEvent in decryptedEvents.allValues)
{
if (decryptedEvent.decryptedAt > 0 && decryptedEvent.ts > 0)
{
NSTimeInterval timeToDecrypt = decryptedEvent.decryptedAt - decryptedEvent.ts;
if (timeToDecrypt > eventMaxTimeToDecrypt)
{
eventMaxTimeToDecrypt = timeToDecrypt;
}
eventAvgTimeToDecrypt += timeToDecrypt;
}
else {
eventsCount = eventsCount - 1;
}
}
eventAvgTimeToDecrypt = eventAvgTimeToDecrypt / eventsCount;
[BWIAnalytics.sharedTracker trackE2EEDecryptionTimeWithCount:eventsCount
max: (eventMaxTimeToDecrypt)
avg: (eventAvgTimeToDecrypt)];
[decryptedEvents removeAllObjects];
}
}
- (void)eventDidDecrypt:(NSNotification *)notif
{
// Could be an event in the reportedFailures, remove it
MXEvent *event = notif.object;
// bwi: 5265 add tracking for decryption duration (only for MXDecryptingErrorUnknownInboundSessionIdCode)
if (temporaryFailures[event.eventId])
{
NSString *eventId = temporaryFailures[event.eventId].failedEventId;
NSTimeInterval ts = temporaryFailures[event.eventId].ts;
decryptedEvents[event.eventId] = [[DecryptedEvent alloc] initWithEventId:eventId ts:ts];
[temporaryFailures removeObjectForKey:event.eventId];
}
[reportedFailures removeObjectForKey:event.eventId];
}

View File

@@ -296,5 +296,11 @@ extension BWIAnalytics : MXAnalyticsDelegate {
self.trackBwiValue(NSNumber(value: count), "Encryption", "ViewMessage", AnalyticsE2EEErrorCode(rawValue: 4)!.description)
}
}
func trackE2EEDecryptionTime(count: Int, max: Int, avg: Int) {
self.trackBwiValue(NSNumber(value: count), "Encryption", "DecryptionTime", "unknown_inbound_sessionid_count")
self.trackBwiValue(NSNumber(value: max), "Encryption", "DecryptionTime", "unknown_inbound_sessionid_max")
self.trackBwiValue(NSNumber(value: avg), "Encryption", "DecryptionTime", "unknown_inbound_sessionid_avg")
}
}