-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1685 from DataDog/ncreated/RUM-2925/report-non-fa…
…tal-app-hangs-in-RUM RUM-2925 feat: Report non-fatal App Hangs as RUM errors
- Loading branch information
Showing
12 changed files
with
445 additions
and
11 deletions.
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
50 changes: 50 additions & 0 deletions
50
Datadog/IntegrationUnitTests/RUM/AppHangsMonitoringTests.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,50 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import XCTest | ||
import DatadogInternal | ||
@testable import DatadogRUM | ||
|
||
/// Test case covering scenarios of App Hangs monitoring in RUM. | ||
class AppHangsMonitoringTests: XCTestCase { | ||
private var core: DatadogCoreProxy! // swiftlint:disable:this implicitly_unwrapped_optional | ||
private var rumConfig = RUM.Configuration(applicationID: .mockAny()) | ||
|
||
override func setUp() { | ||
core = DatadogCoreProxy() | ||
} | ||
|
||
override func tearDown() { | ||
core.flushAndTearDown() | ||
core = nil | ||
} | ||
|
||
func testWhenMainThreadHangsAboveThreshold_itTracksAppHang() throws { | ||
let mainQueue = DispatchQueue(label: "main-queue", qos: .userInteractive) | ||
rumConfig.mainQueue = mainQueue | ||
|
||
// Given | ||
RUM.enable(with: rumConfig, in: core) | ||
|
||
// When | ||
let beforeHang = Date() | ||
mainQueue.sync { | ||
Thread.sleep(forTimeInterval: self.rumConfig.defaultAppHangThreshold * 1.5) | ||
} | ||
|
||
// Then | ||
Thread.sleep(forTimeInterval: 0.5) // wait to make sure watchdog thread completes hang tracking | ||
RUMMonitor.shared(in: core).dd.flush() // flush RUM monitor to await hang processing | ||
|
||
let errors = core.waitAndReturnEvents(ofFeature: RUMFeature.name, ofType: RUMErrorEvent.self) | ||
let appHangError = try XCTUnwrap(errors.first) | ||
|
||
XCTAssertEqual(appHangError.error.message, "App Hang") | ||
XCTAssertEqual(appHangError.error.type, "AppHang") | ||
XCTAssertEqual(appHangError.error.source, .source) | ||
XCTAssertGreaterThanOrEqual(appHangError.date, beforeHang.timeIntervalSince1970.toInt64Milliseconds) | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
DatadogRUM/Sources/Instrumentation/AppHangs/AppHangsObserver.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,59 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
import DatadogInternal | ||
|
||
internal class AppHangsObserver: RUMCommandPublisher { | ||
/// Watchdog thread that monitors the main queue for App Hangs. | ||
private let watchdogThread: AppHangsWatchdogThread | ||
/// Weak reference to RUM monitor for sending App Hang events. | ||
private(set) weak var subscriber: RUMCommandSubscriber? | ||
|
||
init( | ||
appHangThreshold: TimeInterval, | ||
observedQueue: DispatchQueue, | ||
dateProvider: DateProvider, | ||
telemetry: Telemetry | ||
) { | ||
watchdogThread = AppHangsWatchdogThread( | ||
appHangThreshold: appHangThreshold, | ||
queue: observedQueue, | ||
dateProvider: dateProvider, | ||
telemetry: telemetry | ||
) | ||
watchdogThread.onHangEnded = { [weak self] appHang in | ||
// called on watchdog thread | ||
self?.report(appHang: appHang) | ||
} | ||
} | ||
|
||
func start() { | ||
watchdogThread.start() | ||
} | ||
|
||
func stop() { | ||
watchdogThread.cancel() | ||
} | ||
|
||
func publish(to subscriber: RUMCommandSubscriber) { | ||
self.subscriber = subscriber | ||
} | ||
|
||
private func report(appHang: AppHang) { | ||
let addHangCommand = RUMAddCurrentViewErrorCommand( | ||
time: appHang.date, | ||
message: "App Hang", | ||
type: "AppHang", | ||
stack: nil, // TODO: RUM-2925 Add hang stack trace | ||
source: .source, | ||
attributes: [ | ||
"hang_duration": appHang.duration | ||
] | ||
) | ||
subscriber?.process(command: addHangCommand) | ||
} | ||
} |
Oops, something went wrong.