Skip to content

Commit 0b5c423

Browse files
Add [Tabs] Updates to tab logging to make it easier for sentry to group logs (backport #26236) (#26362)
Add [Tabs] Updates to tab logging to make it easier for sentry to group logs (#26236) * updates to tab logging to make it easier for sentry to group logs * Clean up task in restoreTabs * remove parameter from task (cherry picked from commit 50528bb) Co-authored-by: Carson Ramsden <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
1 parent 8a22e2d commit 0b5c423

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

firefox-ios/Client/TabManagement/TabManagerImplementation.swift

+13-9
Original file line numberDiff line numberDiff line change
@@ -609,15 +609,14 @@ class TabManagerImplementation: NSObject, TabManager, FeatureFlaggable {
609609

610610
private func restoreOnly() {
611611
tabs = [Tab]()
612-
Task { [weak self, windowUUID] in
612+
Task {
613613
// Only attempt a tab data store fetch if we know we should have tabs on disk (ignore new windows)
614-
let windowIsNew = self?.windowIsNew ?? false
615-
let windowData: WindowData? = windowIsNew ? nil : await self?.tabDataStore.fetchWindowData(uuid: windowUUID)
616-
await self?.buildTabRestore(window: windowData)
617-
Task { @MainActor in
614+
let windowData: WindowData? = windowIsNew ? nil : await tabDataStore.fetchWindowData(uuid: windowUUID)
615+
await buildTabRestore(window: windowData)
616+
await MainActor.run {
618617
// Log on main thread, where computed `tab` properties can be accessed without risk of races
619-
self?.logger.log("Tabs restore ended after fetching window data", level: .debug, category: .tabs)
620-
self?.logger.log("Normal tabs count; \(self?.normalTabs.count ?? 0), Inactive tabs count; \(self?.inactiveTabs.count ?? 0), Private tabs count; \(self?.privateTabs.count ?? 0)", level: .debug, category: .tabs)
618+
logger.log("Tabs restore ended after fetching window data", level: .debug, category: .tabs)
619+
logger.log("Normal tabs count; \(normalTabs.count), Inactive tabs count; \(inactiveTabs.count), Private tabs count; \(privateTabs.count)", level: .debug, category: .tabs)
621620
}
622621
}
623622
}
@@ -747,9 +746,14 @@ class TabManagerImplementation: NSObject, TabManager, FeatureFlaggable {
747746
newTab.metadataManager?.tabGroupData = groupData
748747

749748
if newTab.url == nil {
750-
logger.log("Tab restored has empty URL for tab id \(tabData.id.uuidString). It was last used \(tabData.lastUsedTime)",
749+
logger.log("Tab restored has empty URL",
751750
level: .debug,
752-
category: .tabs)
751+
category: .tabs,
752+
extra: [
753+
"tabID": tabData.id.uuidString,
754+
"lastUsedTime": tabData.lastUsedTime.description
755+
]
756+
)
753757
}
754758

755759
// Restore screenshot

firefox-ios/Client/Telemetry/TabErrorTelemetryHelper.swift

+27-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class TabErrorTelemetryHelper {
4444
private func recordTabCount(_ window: WindowUUID) {
4545
guard self.tabManagerAvailable(for: window) else { return }
4646
var tabCounts = defaults.object(forKey: defaultsKey) as? [String: Int] ?? [String: Int]()
47-
let tabCount = getTabCount(window: window)
47+
let tabCount = getTotalTabCount(window: window)
4848
tabCounts[window.uuidString] = tabCount
4949
defaults.set(tabCounts, forKey: defaultsKey)
5050
}
@@ -53,11 +53,16 @@ final class TabErrorTelemetryHelper {
5353
guard tabManagerAvailable(for: window) else { return }
5454
guard let tabCounts = defaults.object(forKey: defaultsKey) as? [String: Int],
5555
let expectedTabCount = tabCounts[window.uuidString] else { return }
56-
let currentTabCount = getTabCount(window: window)
56+
let currentTabCount = getTotalTabCount(window: window)
57+
let currentActiveTabCount = getNormalActiveTabCount(window: window)
5758

5859
if expectedTabCount > 1 && (expectedTabCount - currentTabCount) > 1 {
5960
// Potential tab loss bug detected. Log a MetricKit error.
60-
sendTelemetryTabLossDetectedEvent(expected: expectedTabCount, actual: currentTabCount)
61+
sendTelemetryTabLossDetectedEvent(
62+
expected: expectedTabCount,
63+
actual: currentTabCount,
64+
actualNormalActive: currentActiveTabCount
65+
)
6166
}
6267

6368
// After validating the tab count, we make sure to remove the count
@@ -83,15 +88,30 @@ final class TabErrorTelemetryHelper {
8388
return true
8489
}
8590

86-
private func getTabCount(window: WindowUUID) -> Int {
91+
private func getTotalTabCount(window: WindowUUID) -> Int {
8792
assert(tabManagerAvailable(for: window), "getTabCount() should not be called prior to TabManager config.")
8893
return windowManager.tabManager(for: window).normalTabs.count
8994
}
9095

91-
private func sendTelemetryTabLossDetectedEvent(expected: Int, actual: Int) {
92-
logger.log("Tab loss detected. Expected: \(expected). Actual: \(actual). Windows: \(windowManager.windows.count)",
96+
private func getNormalActiveTabCount(window: WindowUUID) -> Int {
97+
assert(
98+
tabManagerAvailable(for: window),
99+
"getNormalActiveTabCount() should not be called prior to TabManager config."
100+
)
101+
return windowManager.tabManager(for: window).normalActiveTabs.count
102+
}
103+
104+
private func sendTelemetryTabLossDetectedEvent(expected: Int, actual: Int, actualNormalActive: Int) {
105+
logger.log("Tab loss detected",
93106
level: .fatal,
94-
category: .tabs)
107+
category: .tabs,
108+
extra: [
109+
"expected": String(expected),
110+
"actual": String(actual),
111+
"actualNormalActive": String(actualNormalActive),
112+
"windows": String(windowManager.windows.count)
113+
]
114+
)
95115
telemetryWrapper.recordEvent(category: .information,
96116
method: .error,
97117
object: .app,

0 commit comments

Comments
 (0)