Skip to content

Commit

Permalink
Add more debug info around history, bg fetch and autoclear
Browse files Browse the repository at this point in the history
  • Loading branch information
dus7 committed Sep 4, 2024
1 parent 76b9d1d commit 7cedfd9
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Core/HistoryManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,11 @@ extension HistoryManager {

let database = HistoryDatabase.make()
var loadError: Error?

DebugDataCollector.current.loadingHistoryDB()
database.loadStore { _, error in
loadError = error
DebugDataCollector.current.finishedLoadingHistoryDB(error)
}

if let loadError {
Expand Down
2 changes: 2 additions & 0 deletions Core/Pixel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ public class Pixel {
newParams[PixelParameters.appVersion] = AppVersion.shared.versionAndBuildNumber
}

newParams.merge(DebugDataCollector.current.debugParameters, uniquingKeysWith: { k1, _ in k1 })

guard !isDryRun else {
Logger.general.debug("Pixel fired \(pixelName.replacingOccurrences(of: "_", with: "."), privacy: .public) \(params.count > 0 ? "\(params)" : "", privacy: .public)")
// simulate server response time for Dry Run mode
Expand Down
12 changes: 12 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
6F9FFE302C57B34800A238BE /* NewTabPageSectionsSettingsModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6F9FFE2F2C57B34800A238BE /* NewTabPageSectionsSettingsModel.swift */; };
6FA3438F2C3D3BC300470677 /* Favorite.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FA3438E2C3D3BC300470677 /* Favorite.swift */; };
6FA343922C3D3C3B00470677 /* FavoriteIconView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FA343912C3D3C3B00470677 /* FavoriteIconView.swift */; };
6FA563CE2C88AB0F00C6BE54 /* DebugDataCollector.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FA563CC2C888D6500C6BE54 /* DebugDataCollector.swift */; };
6FABAA692C6116FD003762EC /* NewTabPageShortcutsSettingsStorageTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FABAA682C6116FD003762EC /* NewTabPageShortcutsSettingsStorageTests.swift */; };
6FB1FE9E2C24D41D0075B68B /* NewTabPageSectionsDebugView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FB1FE9D2C24D41D0075B68B /* NewTabPageSectionsDebugView.swift */; };
6FB1FEA22C256ACD0075B68B /* NewTabPageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FB1FEA12C256ACD0075B68B /* NewTabPageManager.swift */; };
Expand Down Expand Up @@ -1550,6 +1551,7 @@
6F9FFE2F2C57B34800A238BE /* NewTabPageSectionsSettingsModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageSectionsSettingsModel.swift; sourceTree = "<group>"; };
6FA3438E2C3D3BC300470677 /* Favorite.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Favorite.swift; sourceTree = "<group>"; };
6FA343912C3D3C3B00470677 /* FavoriteIconView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoriteIconView.swift; sourceTree = "<group>"; };
6FA563CC2C888D6500C6BE54 /* DebugDataCollector.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = DebugDataCollector.swift; path = ../DuckDuckGo/DebugDataCollector.swift; sourceTree = "<group>"; };
6FABAA682C6116FD003762EC /* NewTabPageShortcutsSettingsStorageTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageShortcutsSettingsStorageTests.swift; sourceTree = "<group>"; };
6FB030C7234331B400A10DB9 /* Configuration.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = Configuration.xcconfig; path = Configuration/Configuration.xcconfig; sourceTree = "<group>"; };
6FB1FE9D2C24D41D0075B68B /* NewTabPageSectionsDebugView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageSectionsDebugView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3800,6 +3802,13 @@
name = Item;
sourceTree = "<group>";
};
6FA563CB2C888D4900C6BE54 /* Debug */ = {
isa = PBXGroup;
children = (
);
name = Debug;
sourceTree = "<group>";
};
6FB1FE9C2C24D4060075B68B /* NewTabPageSectionsDebugView */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -5365,6 +5374,7 @@
F1134EA91F3E2BA700B73467 /* Store */,
CB2A7EF028410DF700885F67 /* PixelEvent.swift */,
BDC234F62B27F51100D3C798 /* UniquePixel.swift */,
6FA563CC2C888D6500C6BE54 /* DebugDataCollector.swift */,
853A717520F62FE800FE60BC /* Pixel.swift */,
6F03CB062C32F173004179A8 /* PixelFiring.swift */,
6F03CB082C32F331004179A8 /* PixelFiringAsync.swift */,
Expand Down Expand Up @@ -5939,6 +5949,7 @@
F1C5ECF31E37812900C599A4 /* Application */ = {
isa = PBXGroup;
children = (
6FA563CB2C888D4900C6BE54 /* Debug */,
83BE9BC2215D69C1009844D9 /* AppConfigurationFetch.swift */,
CB24F70E29A3EB15006DCC58 /* AppConfigurationURLProvider.swift */,
84E341951E2F7EFB00BDBA6F /* AppDelegate.swift */,
Expand Down Expand Up @@ -8002,6 +8013,7 @@
85D2187624BF6164004373D2 /* FaviconSourcesProvider.swift in Sources */,
98B000532915C46E0034BCA0 /* LegacyBookmarksStoreMigration.swift in Sources */,
85200FA11FBC5BB5001AF290 /* DDGPersistenceContainer.swift in Sources */,
6FA563CE2C88AB0F00C6BE54 /* DebugDataCollector.swift in Sources */,
9FEA22322C3270BD006B03BF /* TimerInterface.swift in Sources */,
1E4DCF4C27B6A4CB00961E25 /* URLFileExtension.swift in Sources */,
EE50053029C3BA0800AE0773 /* InternalUserStore.swift in Sources */,
Expand Down
7 changes: 7 additions & 0 deletions DuckDuckGo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ import os.log

// swiftlint:disable:next function_body_length
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

DebugDataCollector.current.isLaunching(application, with: launchOptions)

// Attribution support
updateAttribution(conversionValue: 1)
Expand Down Expand Up @@ -387,6 +389,8 @@ import os.log
didCrashDuringCrashHandlersSetUp = false
}

DebugDataCollector.current.finishedLaunching()

return true
}

Expand Down Expand Up @@ -741,6 +745,7 @@ import os.log
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {

Logger.lifecycle.debug(#function)
DebugDataCollector.current.startedBackgroundFetch()

AppConfigurationFetch().start(isBackgroundFetch: true) { result in
switch result {
Expand All @@ -749,6 +754,8 @@ import os.log
case .assetsUpdated:
completionHandler(.newData)
}

DebugDataCollector.current.finishedBackgroundFetch()
}
}

Expand Down
4 changes: 4 additions & 0 deletions DuckDuckGo/AutoClear.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class AutoClear {
func clearDataIfEnabled(launching: Bool = false, applicationState: DataStoreWarmup.ApplicationState = .unknown) async {
guard let settings = AutoClearSettingsModel(settings: appSettings) else { return }

DebugDataCollector.current.startingAutoclear()

worker.willStartClearing(self)

if settings.action.contains(.clearTabs) {
Expand All @@ -62,6 +64,8 @@ class AutoClear {
await worker.forgetData(applicationState: applicationState)
}

DebugDataCollector.current.finishedAutoclear()

worker.autoClearDidFinishClearing(self, isLaunching: launching)
}

Expand Down
136 changes: 136 additions & 0 deletions DuckDuckGo/DebugDataCollector.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
//

Check failure on line 1 in DuckDuckGo/DebugDataCollector.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Header comments should be consistent with project patterns (file_header)
// DebugDataHolderActor.swift
// DuckDuckGo
//
// Copyright © 2024 DuckDuckGo. All rights reserved.
//
// 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.
//

import Foundation
import Persistence

public final class DebugDataCollector {

public static let current = DebugDataCollector()

private var application: UIApplication?
private var launchOptions: [UIApplication.LaunchOptionsKey: Any]?
private var isRunningBGFetch: Bool = false
private var didRunBGFetch: Bool = false
private var instanceUUID = UUID()

public var debugParameters: [String: String] {

let getParams: () -> [String: String] = {
var params = [String: String]()
if let application = self.application {
params[PixelParameters.applicationState] = "\(application.applicationState.rawValue)"
params[PixelParameters.isDataProtected] = "\(!application.isProtectedDataAvailable)"
params[PixelParameters.backgroundRefreshStatus] = "\(application.backgroundRefreshStatus.rawValue)"
}

params[PixelParameters.didRunBGFetch] = "\(self.didRunBGFetch)"
params[PixelParameters.isRunningBGFetch] = "\(self.isRunningBGFetch)"
params[PixelParameters.launchOptions] = self.launchOptions?.description

params[PixelParameters.instanceUUID] = self.instanceUUID.uuidString

return params
}

if Thread.isMainThread {
return getParams()
} else {
return DispatchQueue.main.sync {
getParams()
}
}
}

public func isLaunching(_ application: UIApplication, with options: [UIApplication.LaunchOptionsKey: Any]?) {
self.application = application
self.launchOptions = options
firePixel(PixelName.launchingEntry)
}

public func finishedLaunching() {
firePixel(PixelName.launchingReturn)
}

public func startedBackgroundFetch() {
isRunningBGFetch = true
didRunBGFetch = true
firePixel(PixelName.startedBGFetch)
}

public func finishedBackgroundFetch() {
isRunningBGFetch = false
firePixel(PixelName.finishedBGFetch)
}

public func loadingHistoryDB() {
firePixel(PixelName.startedLoadingHistory)
}

public func finishedLoadingHistoryDB(_ error: Error?) {
var params = [String: String]()
if let error {
let errorInfo = CoreDataErrorsParser.parse(error: error as NSError)
params["cd_error"] = errorInfo.debugDescription
}
firePixel(PixelName.finishedLoadingHistory, params: params)
}

public func startingAutoclear() {
firePixel(PixelName.staredAutoclear)
}

public func finishedAutoclear() {
firePixel(PixelName.finishedAutoclear)
}

private func firePixel(_ named: String, params: [String: String] = [:]) {
Pixel.fire(pixelNamed: named, withAdditionalParameters: params)
}

private struct PixelName {
static let launchingEntry = "debug_app_launching_entry"
static let launchingReturn = "debug_app_launching_return"

static let startedBGFetch = "debug_bg_fetch_start"
static let finishedBGFetch = "debug_bg_fetch_start"

static let staredAutoclear = "debug_ac_start"
static let finishedAutoclear = "debug_ac_finish"

static let startedLoadingHistory = "debug_loading_history_db_start"
static let finishedLoadingHistory = "debug_loading_history_db_finish"
}

private struct PixelParameters {
static let backgroundRefreshStatus = "bg_refresh_status"
static let applicationState = "app_state"
static let isDataProtected = Core.PixelParameters.isDataProtected
static let launchOptions = "launch_options"
static let instanceUUID = "instance_uuid"
static let didRunBGFetch = "did_run_bg_fetch"
static let isRunningBGFetch = "is_running_bg_fetch"
}
}

private extension CoreDataErrorsParser.ErrorInfo {
var debugDescription: String {
"code: \(code); domain: \(domain); entity: \(entity); property: \(property))"

Check warning on line 134 in DuckDuckGo/DebugDataCollector.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

string interpolation produces a debug description for an optional value; did you mean to make this explicit?

Check warning on line 134 in DuckDuckGo/DebugDataCollector.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

string interpolation produces a debug description for an optional value; did you mean to make this explicit?

Check warning on line 134 in DuckDuckGo/DebugDataCollector.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

string interpolation produces a debug description for an optional value; did you mean to make this explicit?

Check warning on line 134 in DuckDuckGo/DebugDataCollector.swift

View workflow job for this annotation

GitHub Actions / Make Release Build

string interpolation produces a debug description for an optional value; did you mean to make this explicit?

Check warning on line 134 in DuckDuckGo/DebugDataCollector.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

string interpolation produces a debug description for an optional value; did you mean to make this explicit?

Check warning on line 134 in DuckDuckGo/DebugDataCollector.swift

View workflow job for this annotation

GitHub Actions / Unit Tests

string interpolation produces a debug description for an optional value; did you mean to make this explicit?
}
}

0 comments on commit 7cedfd9

Please sign in to comment.