-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RUM-5248 feat: send memory warning as RUM error
- Loading branch information
Showing
14 changed files
with
319 additions
and
12 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
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
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
30 changes: 30 additions & 0 deletions
30
DatadogRUM/Sources/Instrumentation/MemoryWarnings/MemoryWarning.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,30 @@ | ||
/* | ||
* 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 | ||
import UIKit | ||
|
||
/// Represents a memory warning | ||
internal struct MemoryWarning { | ||
/// The date when the memory warning was received. | ||
let date: Date | ||
|
||
/// The backtrace at the moment of memory warning. | ||
let backtrace: BacktraceReport? | ||
|
||
/// Creates a new instance of `MemoryWarning | ||
/// - Parameters: | ||
/// - date: Date when the memory warning was received. | ||
/// - backtrace: Backtrace at the moment of memory warning. | ||
init( | ||
date: Date, | ||
backtrace: BacktraceReport? | ||
) { | ||
self.date = date | ||
self.backtrace = backtrace | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
DatadogRUM/Sources/Instrumentation/MemoryWarnings/MemoryWarningMonitor.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 Foundation | ||
import DatadogInternal | ||
import UIKit | ||
|
||
/// Tracks the memory warnings history and publishes it to the subscribers. | ||
internal final class MemoryWarningMonitor { | ||
let notificationCenter: NotificationCenter | ||
let backtraceReporter: BacktraceReporting? | ||
let reporter: MemoryWarningReporting | ||
|
||
init( | ||
backtraceReporter: BacktraceReporting?, | ||
memoryWarningReporter: MemoryWarningReporting, | ||
notificationCenter: NotificationCenter = .default | ||
) { | ||
self.notificationCenter = notificationCenter | ||
self.backtraceReporter = backtraceReporter | ||
self.reporter = memoryWarningReporter | ||
} | ||
|
||
/// Starts monitoring memory warnings by subscribing to `UIApplication.didReceiveMemoryWarningNotification`. | ||
func start() { | ||
notificationCenter.addObserver(self, selector: #selector(didReceiveMemoryWarning), name: UIApplication.didReceiveMemoryWarningNotification, object: nil) | ||
} | ||
|
||
@objc | ||
func didReceiveMemoryWarning() { | ||
let date: Date = .init() | ||
let backtrace: BacktraceReport? | ||
do { | ||
backtrace = try backtraceReporter?.generateBacktrace() | ||
} catch { | ||
backtrace = nil | ||
} | ||
let warning = MemoryWarning(date: date, backtrace: backtrace) | ||
|
||
reporter.report(warning: warning) | ||
} | ||
|
||
/// Stops monitoring memory warnings. | ||
func stop() { | ||
notificationCenter.removeObserver(self) | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
DatadogRUM/Sources/Instrumentation/MemoryWarnings/MemoryWarningReporter.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-2020 Datadog, Inc. | ||
*/ | ||
|
||
import Foundation | ||
import DatadogInternal | ||
import UIKit | ||
|
||
/// Defines operations used for reporting memory warnings. | ||
internal protocol MemoryWarningReporting: RUMCommandPublisher { | ||
/// Reports the given memory warning. | ||
/// - Parameter warning: The memory warning to report. | ||
func report(warning: MemoryWarning) | ||
} | ||
|
||
/// Receives memory warnings and reports them as RUM errors. | ||
internal class MemoryWarningReporter: MemoryWarningReporting { | ||
enum Constants { | ||
/// The standardized `error.message` for RUM errors describing a memory warning. | ||
static let memoryWarningErrorMessage = "Memory Warning" | ||
/// The standardized `error.type` for RUM errors describing a memory warning. | ||
static let memoryWarningErrorType = "MemoryWarning" | ||
/// The standardized `error.stack` when backtrace generation was not available. | ||
static let memoryWarningStackNotAvailableErrorMessage = "Stack trace was not generated because `DatadogCrashReporting` had not been enabled." | ||
} | ||
|
||
private(set) weak var subscriber: RUMCommandSubscriber? | ||
|
||
/// Reports the given memory warning as a RUM error. | ||
/// - Parameter warning: The memory warning to report. | ||
func report(warning: MemoryWarning) { | ||
let command = RUMAddCurrentViewMemoryWarningCommand( | ||
time: warning.date, | ||
attributes: [:], | ||
message: Constants.memoryWarningErrorMessage, | ||
type: Constants.memoryWarningErrorType, | ||
stack: warning.backtrace?.stack ?? Constants.memoryWarningStackNotAvailableErrorMessage, | ||
threads: warning.backtrace?.threads, | ||
binaryImages: warning.backtrace?.binaryImages, | ||
isStackTraceTruncated: warning.backtrace?.wasTruncated | ||
) | ||
subscriber?.process(command: command) | ||
} | ||
|
||
func publish(to subscriber: any RUMCommandSubscriber) { | ||
self.subscriber = subscriber | ||
} | ||
} |
Oops, something went wrong.