Skip to content

Commit

Permalink
Added FXIOS-7559 [v121] health metrics telemetry (#17844)
Browse files Browse the repository at this point in the history
* Add crashed last launch metric

* Add cpu exception telemetry

* Add hang exception

* Update link

* Fix warning

* Update links

(cherry picked from commit 0898ac8)

# Conflicts:
#	Client/Telemetry/TelemetryWrapper.swift
#	Client/metrics.yaml
  • Loading branch information
OrlaM authored and mergify[bot] committed Dec 21, 2023
1 parent 6606641 commit b181167
Show file tree
Hide file tree
Showing 5 changed files with 299 additions and 2 deletions.
11 changes: 9 additions & 2 deletions Client/Application/AppLaunchUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class AppLaunchUtil {
}

TelemetryWrapper.shared.setup(profile: profile)
recordUserPrefsTelemetry()
recordStartUpTelemetry()

// Need to get "settings.sendUsageData" this way so that Sentry can be initialized before getting the Profile.
let sendUsageData = NSUserDefaultsPrefs(prefix: "profile").boolForKey(AppConstants.prefSendUsageData) ?? true
Expand Down Expand Up @@ -191,12 +191,19 @@ class AppLaunchUtil {
}
}

private func recordUserPrefsTelemetry() {
private func recordStartUpTelemetry() {
let isEnabled: Bool = (profile.prefs.boolForKey(PrefsKeys.UserFeatureFlagPrefs.SponsoredShortcuts) ?? true) &&
(profile.prefs.boolForKey(PrefsKeys.UserFeatureFlagPrefs.TopSiteSection) ?? true)
TelemetryWrapper.recordEvent(category: .information,
method: .view,
object: .sponsoredShortcuts,
extras: [TelemetryWrapper.EventExtraKey.preference.rawValue: isEnabled])

if logger.crashedLastLaunch {
TelemetryWrapper.recordEvent(category: .information,
method: .error,
object: .app,
value: .crashedLastLaunch)
}
}
}
26 changes: 26 additions & 0 deletions Client/Telemetry/MetricKit/MetricKitWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ class MetricKitWrapper: NSObject, MXMetricManagerSubscriber {
payload.diskWriteExceptionDiagnostics?.forEach({ exception in
self.handleDiskWriteException(exception)
})
payload.cpuExceptionDiagnostics?.forEach({ exception in
self.handleCPUException(exception)
})
payload.hangDiagnostics?.forEach({ exception in
self.handleHangException(exception)
})
}
}

Expand All @@ -37,4 +43,24 @@ class MetricKitWrapper: NSObject, MXMetricManagerSubscriber {
value: .largeFileWrite,
extras: eventExtra)
}

private func handleCPUException(_ exception: MXCPUExceptionDiagnostic) {
let size = Int32(measurementFormatter.string(from: exception.totalCPUTime)) ?? -1
let eventExtra = [TelemetryWrapper.EventExtraKey.size.rawValue: size]
telemetryWrapper.recordEvent(category: .information,
method: .error,
object: .app,
value: .cpuException,
extras: eventExtra)
}

private func handleHangException(_ exception: MXHangDiagnostic) {
let size = Int32(measurementFormatter.string(from: exception.hangDuration)) ?? -1
let eventExtra = [TelemetryWrapper.EventExtraKey.size.rawValue: size]
telemetryWrapper.recordEvent(category: .information,
method: .error,
object: .app,
value: .hangException,
extras: eventExtra)
}
}
53 changes: 53 additions & 0 deletions Client/Telemetry/TelemetryWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,14 @@ extension TelemetryWrapper {
case shoppingCFRsDisplayed = "shopping-cfrs-displayed"
case awesomebarShareTap = "awesomebar-share-tap"
case largeFileWrite = "large-file-write"
<<<<<<< HEAD:Client/Telemetry/TelemetryWrapper.swift
=======
case crashedLastLaunch = "crashed_last_launch"
case cpuException = "cpu_exception"
case hangException = "hang-exception"
case fxSuggestionClickInfo = "fx-suggestion-click-info"
case fxSuggestionPosition = "fx-suggestion-position"
>>>>>>> 0898ac8e9 (Added FXIOS-7559 [v121] health metrics telemetry (#17844)):firefox-ios/Client/Telemetry/TelemetryWrapper.swift
}

public enum EventExtraKey: String, CustomStringConvertible {
Expand Down Expand Up @@ -1807,6 +1815,51 @@ extension TelemetryWrapper {
let properties = GleanMetrics.AppErrors.LargeFileWriteExtra(size: quantity)
GleanMetrics.AppErrors.largeFileWrite.record(properties)
}
<<<<<<< HEAD:Client/Telemetry/TelemetryWrapper.swift
=======
case(.information, .error, .app, .crashedLastLaunch, _):
GleanMetrics.AppErrors.crashedLastLaunch.record()
case(.information, .error, .app, .cpuException, let extras):
if let quantity = extras?[EventExtraKey.size.rawValue] as? Int32 {
let properties = GleanMetrics.AppErrors.CpuExceptionExtra(size: quantity)
GleanMetrics.AppErrors.cpuException.record(properties)
}
case(.information, .error, .app, .hangException, let extras):
if let quantity = extras?[EventExtraKey.size.rawValue] as? Int32 {
let properties = GleanMetrics.AppErrors.HangExceptionExtra(size: quantity)
GleanMetrics.AppErrors.hangException.record(properties)
}

// MARK: - FX Suggest
case(.action, .tap, .fxSuggest, _, let extras ):
guard let contextIdString = TelemetryContextualIdentifier.contextId,
let contextId = UUID(uuidString: contextIdString),
let interactionInfo = extras?[EventValue.fxSuggestionClickInfo.rawValue] as? RustFirefoxSuggestionInteractionInfo else {
return recordUninstrumentedMetrics(category: category, method: method, object: object, value: value, extras: extras)
}
switch interactionInfo {
case let .amp(blockId, advertiser, iabCategory, reportingURL):
GleanMetrics.FxSuggest.contextId.set(contextId)
GleanMetrics.FxSuggest.pingType.set("fxsuggest-click")
GleanMetrics.FxSuggest.blockId.set(blockId)
GleanMetrics.FxSuggest.advertiser.set(advertiser)
GleanMetrics.FxSuggest.iabCategory.set(iabCategory)
if let reportingURL {
GleanMetrics.FxSuggest.reportingUrl.set(url: reportingURL)
}
if let position = extras?[EventValue.fxSuggestionPosition.rawValue] as? Int {
GleanMetrics.FxSuggest.position.set(Int64(position))
}
case .wikipedia:
GleanMetrics.FxSuggest.pingType.set("fxsuggest-click")
GleanMetrics.FxSuggest.contextId.set(contextId)
GleanMetrics.FxSuggest.advertiser.set("wikipedia")
if let position = extras?[EventValue.fxSuggestionPosition.rawValue] as? Int {
GleanMetrics.FxSuggest.position.set(Int64(position))
}
}
GleanMetrics.Pings.shared.fxSuggest.submit()
>>>>>>> 0898ac8e9 (Added FXIOS-7559 [v121] health metrics telemetry (#17844)):firefox-ios/Client/Telemetry/TelemetryWrapper.swift
default:
recordUninstrumentedMetrics(category: category, method: method, object: object, value: value, extras: extras)
}
Expand Down
179 changes: 179 additions & 0 deletions Client/metrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4689,3 +4689,182 @@ app_errors:
notification_emails:
- [email protected]
expires: never
<<<<<<< HEAD:Client/metrics.yaml
=======
crashed_last_launch:
type: event
description: |
Recorded when the previous session ended as a result of a crash
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16827
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17844
notification_emails:
- [email protected]
expires: never
cpu_exception:
type: event
description: |
Recorded when a cpu exception is triggered
extra_keys:
size:
description: |
Total CPU time consumed in the scope of the exception
type: quantity
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16828
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17844
notification_emails:
- [email protected]
expires: never
hang_exception:
type: event
description: |
Recorded when the main thread hangs
extra_keys:
size:
description: |
Total time the main thread was blocked
type: quantity
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16829
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17844
notification_emails:
- [email protected]
expires: never

fx_suggest:
ping_type:
type: string
description: >
The ping's type. Either "fxsuggest-click" or "fxsuggest-impression".
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16589
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17556
data_sensitivity:
- interaction
notification_emails:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
expires: never
send_in_pings:
- fx-suggest
position:
type: quantity
unit: non-negative integer
description: >
The position (1-based) of this suggestion in the full list of suggestions, relative to the
top of the awesomebar.
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16589
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17556
data_sensitivity:
- interaction
notification_emails:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
expires: never
send_in_pings:
- fx-suggest
block_id:
type: quantity
description: |
A unique identifier for a sponsored suggestion. Not set for non-sponsored suggestions.
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16589
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17556
data_sensitivity:
- interaction
notification_emails:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
expires: never
unit: integer
send_in_pings:
- fx-suggest
advertiser:
type: string
description: |
The name of the advertiser providing the sponsored suggestion
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16589
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17556
data_sensitivity:
- interaction
notification_emails:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
expires: never
send_in_pings:
- fx-suggest
reporting_url:
type: url
description: |
If this ping is for a sponsored suggestion, the partner URL for reporting this interaction.
Not set for non-sponsored suggestions.
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16589
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17556
data_sensitivity:
- interaction
notification_emails:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
expires: never
send_in_pings:
- fx-suggest
context_id:
type: uuid
description: |
An identifier to identify users for Contextual Services user interaction pings.
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16589
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17556
data_sensitivity:
- interaction
notification_emails:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
expires: never
send_in_pings:
- fx-suggest
iab_category:
type: string
description: |
The suggestion's advertising category. "22 - Shopping" for sponsored suggestions.
Not set for non-sponsored suggestions.
bugs:
- https://github.com/mozilla-mobile/firefox-ios/issues/16589
data_reviews:
- https://github.com/mozilla-mobile/firefox-ios/pull/17556
data_sensitivity:
- interaction
notification_emails:
- [email protected]
- [email protected]
- [email protected]
- [email protected]
expires: never
send_in_pings:
- fx-suggest
>>>>>>> 0898ac8e9 (Added FXIOS-7559 [v121] health metrics telemetry (#17844)):firefox-ios/Client/metrics.yaml
32 changes: 32 additions & 0 deletions firefox-ios/Tests/ClientTests/TelemetryWrapperTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,38 @@ class TelemetryWrapperTests: XCTestCase {

testEventMetricRecordingSuccess(metric: GleanMetrics.AppErrors.largeFileWrite)
}

func test_error_crashedLastLaunchIsCalled() {
TelemetryWrapper.recordEvent(category: .information,
method: .error,
object: .app,
value: .crashedLastLaunch)

testEventMetricRecordingSuccess(metric: GleanMetrics.AppErrors.crashedLastLaunch)
}

func test_error_cpuExceptionIsCalled() {
let eventExtra = [TelemetryWrapper.EventExtraKey.size.rawValue: Int32(1000)]
TelemetryWrapper.recordEvent(category: .information,
method: .error,
object: .app,
value: .cpuException,
extras: eventExtra)

testEventMetricRecordingSuccess(metric: GleanMetrics.AppErrors.cpuException)
}

func test_error_hangExceptionIsCalled() {
let eventExtra = [TelemetryWrapper.EventExtraKey.size.rawValue: Int32(1000)]
TelemetryWrapper.recordEvent(category: .information,
method: .error,
object: .app,
value: .hangException,
extras: eventExtra)

testEventMetricRecordingSuccess(metric: GleanMetrics.AppErrors.hangException)
}

// MARK: - RecordSearch
func test_RecordSearch_GleanIsCalledSearchSuggestion() {
let extras = [TelemetryWrapper.EventExtraKey.recordSearchLocation.rawValue: "suggestion",
Expand Down

0 comments on commit b181167

Please sign in to comment.