-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FXIOS-10125 Telemetry event: tab error detection (#22221)
* [FXIOS-10125] Initial work to add a telemetry event for detecting potential tab loss issues in production * [FXIOS-10125] Comment * [FXIOS-10125] Record and validate on main thread only * [FXIOS-10125] Add tab-loss-detected to metrics.yaml * [FXIOS-10125] Add Glean telemetry test * [FXIOS-10125] Minor fixes for updated telemetry * [FXIOS-10125] Use UserDefaultsInterface * [FXIOS-10125] Use WindowManager protocol
- Loading branch information
1 parent
6dfff7e
commit 42301f0
Showing
6 changed files
with
99 additions
and
1 deletion.
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
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
65 changes: 65 additions & 0 deletions
65
firefox-ios/Client/Telemetry/TabErrorTelemetryHelper.swift
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,65 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/ | ||
|
||
import UIKit | ||
import Shared | ||
import Common | ||
|
||
/// Helper utility that aims to detect potential bugs in production which could result in tab loss. | ||
final class TabErrorTelemetryHelper { | ||
static let shared = TabErrorTelemetryHelper() | ||
private let defaultsKey = "TabErrorTelemetryHelper_Data" | ||
private let telemetryWrapper: TelemetryWrapperProtocol | ||
private let defaults: UserDefaultsInterface | ||
private let windowManager: WindowManager | ||
|
||
private init(telemetryWrapper: TelemetryWrapperProtocol = TelemetryWrapper.shared, | ||
windowManager: WindowManager = AppContainer.shared.resolve(), | ||
defaults: UserDefaultsInterface = UserDefaults.standard) { | ||
self.telemetryWrapper = telemetryWrapper | ||
self.defaults = defaults | ||
self.windowManager = windowManager | ||
} | ||
|
||
/// Records the scene (windows) tab count for the purposes of sanity-checking for | ||
/// potential tab-loss related errors. Such bugs can significantly impact users, so | ||
/// we attempt to detect any issues which could indicate potential tab loss. | ||
func recordTabCountForBackgroundedScene(_ window: WindowUUID) { | ||
ensureMainThread { | ||
var tabCounts = self.defaults.object(forKey: self.defaultsKey) as? [String: Int] ?? [String: Int]() | ||
let tabCount = self.getTabCount(window: window) | ||
tabCounts[window.uuidString] = tabCount | ||
UserDefaults.standard.set(tabCounts, forKey: self.defaultsKey) | ||
} | ||
} | ||
|
||
/// Validates the tab count against the recorded tab count. If this has decreased | ||
/// without any obvious cause (e.g. Close All Tabs action) then it is suggestive of | ||
/// a potential bug impacting users, and a MetricKit event is logged. | ||
func validateTabCountForForegroundedScene(_ window: WindowUUID) { | ||
ensureMainThread { | ||
guard let tabCounts = self.defaults.object(forKey: self.defaultsKey) as? [String: Int], | ||
let expectedTabCount = tabCounts[window.uuidString] else { return } | ||
let currentTabCount = self.getTabCount(window: window) | ||
|
||
if expectedTabCount > 1 && (expectedTabCount - currentTabCount) > 1 { | ||
// Potential tab loss bug detected. Log a MetricKit error. | ||
self.sendTelemetryTabLossDetectedEvent() | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Internal Utility | ||
|
||
private func getTabCount(window: WindowUUID) -> Int { | ||
return windowManager.tabManager(for: window).normalTabs.count | ||
} | ||
|
||
private func sendTelemetryTabLossDetectedEvent() { | ||
telemetryWrapper.recordEvent(category: .information, | ||
method: .error, | ||
object: .app, | ||
value: .tabLossDetected) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5704,6 +5704,17 @@ app_errors: | |
notification_emails: | ||
- [email protected] | ||
expires: never | ||
tab_loss_detected: | ||
type: event | ||
description: | | ||
Recorded when we detect potential tab loss | ||
bugs: | ||
- https://github.com/mozilla-mobile/firefox-ios/issues/22180 | ||
data_reviews: | ||
- https://github.com/mozilla-mobile/firefox-ios/pull/22221 | ||
notification_emails: | ||
- [email protected] | ||
expires: never | ||
|
||
webview: | ||
did_fail_provisional: | ||
|
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