Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal(hybrid): Add Replay Mask options to dict init #4495

Merged
merged 16 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- Log a warning when dropping envelopes due to rate-limiting (#4463)
- Expose `SentrySessionReplayIntegration-Hybrid.h` as `private` (#4486)
- Add `maskedViewClasses` and `unmaskedViewClasses` to SentryReplayOptions init via dict (#4492)
- Add `quality` to SentryReplayOptions init via dict (#4495)
krystofwoldrich marked this conversation as resolved.
Show resolved Hide resolved

## 8.39.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,53 @@

@objcMembers
public class SentryReplayOptions: NSObject, SentryRedactOptions {

/**
* Enum to define the quality of the session replay.
*/
@objc
public enum SentryReplayQuality: Int {
public enum SentryReplayQuality: Int, CustomStringConvertible {
fileprivate static let defaultQuality: SentryReplayQuality = .medium

/**
* Video Scale: 80%
* Bit Rate: 20.000
*/
case low

/**
* Video Scale: 100%
* Bit Rate: 40.000
*/
case medium

/**
* Video Scale: 100%
* Bit Rate: 60.000
*/
case high

public var description: String {
switch self {
case .low: return "low"
case .medium: return "medium"
case .high: return "high"
}
}

Check warning on line 37 in Sources/Swift/Integrations/SessionReplay/SentryReplayOptions.swift

View check run for this annotation

Codecov / codecov/patch

Sources/Swift/Integrations/SessionReplay/SentryReplayOptions.swift#L36-L37

Added lines #L36 - L37 were not covered by tests

/**
* Used by Hybrid SDKs.
*/
static func fromName(_ name: String) -> SentryReplayOptions.SentryReplayQuality {
switch name {
case "low": return .low
case "medium": return .medium
case "high": return .high
default: return defaultQuality
}
}
}

/**
* Indicates the percentage in which the replay for the session will be created.
* - Specifying @c 0 means never, @c 1.0 means always.
Expand All @@ -44,60 +66,60 @@
* - note: The default is 0.
*/
public var onErrorSampleRate: Float

/**
* Indicates whether session replay should redact all text in the app
* by drawing a black rectangle over it.
*
* - note: The default is true
*/
public var maskAllText = true

/**
* Indicates whether session replay should redact all non-bundled image
* in the app by drawing a black rectangle over it.
*
* - note: The default is true
*/
public var maskAllImages = true

/**
* Indicates the quality of the replay.
* The higher the quality, the higher the CPU and bandwidth usage.
*/
public var quality = SentryReplayQuality.medium
public var quality = SentryReplayQuality.defaultQuality

/**
* A list of custom UIView subclasses that need
* A list of custom UIView subclasses that need
* to be masked during session replay.
* By default Sentry already mask text and image elements from UIKit
* Every child of a view that is redacted will also be redacted.
*/
public var maskedViewClasses = [AnyClass]()

/**
* A list of custom UIView subclasses to be ignored
* during masking step of the session replay.
* The views of given classes will not be redacted but their children may be.
* This property has precedence over `redactViewTypes`.
*/
public var unmaskedViewClasses = [AnyClass]()

/**
* Defines the quality of the session replay.
* Higher bit rates better quality, but also bigger files to transfer.
*/
var replayBitRate: Int {
quality.rawValue * 20_000 + 20_000
}

/**
* The scale related to the window size at which the replay will be created
*/
var sizeScale: Float {
quality == .low ? 0.8 : 1.0
}

/**
* Number of frames per second of the replay.
* The more the havier the process is.
Expand All @@ -108,30 +130,30 @@
if frameRate < 1 { frameRate = 1 }
}
}

/**
* The maximum duration of replays for error events.
*/
let errorReplayDuration = TimeInterval(30)

/**
* The maximum duration of the segment of a session replay.
*/
let sessionSegmentDuration = TimeInterval(5)

/**
* The maximum duration of a replay session.
*/
let maximumDuration = TimeInterval(3_600)

/**
* Inittialize session replay options disabled
*/
public override init() {
self.sessionSampleRate = 0
self.onErrorSampleRate = 0
}

/**
* Initialize session replay options
* - parameters:
Expand All @@ -145,7 +167,7 @@
self.maskAllText = maskAllText
self.maskAllImages = maskAllImages
}

convenience init(dictionary: [String: Any]) {
let sessionSampleRate = (dictionary["sessionSampleRate"] as? NSNumber)?.floatValue ?? 0
let onErrorSampleRate = (dictionary["errorSampleRate"] as? NSNumber)?.floatValue ?? 0
Expand All @@ -158,5 +180,8 @@
self.unmaskedViewClasses = ((dictionary["unmaskedViewClasses"] as? NSArray) ?? []).compactMap({ element in
NSClassFromString((element as? String) ?? "")
})
if let quality = SentryReplayQuality(rawValue: dictionary["quality"] as? Int ?? -1) {
self.quality = quality
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,45 @@ import Foundation
import XCTest

class SentryReplayOptionsTests: XCTestCase {


func testQualityConversion() {
XCTAssertEqual(SentryReplayOptions.SentryReplayQuality.fromName("low"), .low)
XCTAssertEqual(SentryReplayOptions.SentryReplayQuality.fromName("medium"), .medium)
XCTAssertEqual(SentryReplayOptions.SentryReplayQuality.fromName("high"), .high)
XCTAssertEqual(SentryReplayOptions.SentryReplayQuality.fromName("invalid_value"), .medium)
}

func testQualityLow() {
let options = SentryReplayOptions()
options.quality = .low

XCTAssertEqual(options.replayBitRate, 20_000)
XCTAssertEqual(options.sizeScale, 0.8)
}

func testQualityMedium() {
let options = SentryReplayOptions()
options.quality = .medium

XCTAssertEqual(options.replayBitRate, 40_000)
XCTAssertEqual(options.sizeScale, 1.0)
}

func testQualityHigh() {
let options = SentryReplayOptions()
options.quality = .high

XCTAssertEqual(options.replayBitRate, 60_000)
XCTAssertEqual(options.sizeScale, 1.0)
}

func testQualityFromName() {
XCTAssertEqual(SentryReplayOptions.SentryReplayQuality.fromName("low"), .low)
XCTAssertEqual(SentryReplayOptions.SentryReplayQuality.fromName("medium"), .medium)
XCTAssertEqual(SentryReplayOptions.SentryReplayQuality.fromName("high"), .high)
XCTAssertEqual(SentryReplayOptions.SentryReplayQuality.fromName("invalid_value"), .medium)
}

func testInitFromDictOnErrorSampleRateAsDouble() {
let options = SentryReplayOptions(dictionary: [
"errorSampleRate": 0.44
Expand Down Expand Up @@ -160,13 +174,43 @@ class SentryReplayOptionsTests: XCTestCase {
XCTAssertTrue(options.maskAllImages)
}

func testInitFromDictQualityWithString() {
let options = SentryReplayOptions(dictionary: [
"quality": 0 // low
])
XCTAssertEqual(options.quality, .low)

let options2 = SentryReplayOptions(dictionary: [
"quality": 1 // medium
])
XCTAssertEqual(options2.quality, .medium)

let options3 = SentryReplayOptions(dictionary: [
"quality": 2 // high
])
XCTAssertEqual(options3.quality, .high)
}

func testInitFromDictQualityWithInvalidValue() {
let options = SentryReplayOptions(dictionary: [
"quality": "invalid_value"
])
XCTAssertEqual(options.quality, .medium)

let options2 = SentryReplayOptions(dictionary: [
"quality": [1]
])
XCTAssertEqual(options2.quality, .medium)
}

func testInitFromDictWithMultipleOptions() {
let options = SentryReplayOptions(dictionary: [
"sessionSampleRate": 0.5,
"errorSampleRate": 0.8,
"maskAllText": false,
"maskedViewClasses": ["NSString", "not.a.class", 123] as [Any],
"unmaskedViewClasses": ["NSNumber", "invalid", true] as [Any]
"unmaskedViewClasses": ["NSNumber", "invalid", true] as [Any],
"quality": 0 // low
])

XCTAssertEqual(options.sessionSampleRate, 0.5)
Expand All @@ -177,5 +221,7 @@ class SentryReplayOptionsTests: XCTestCase {
XCTAssertEqual(ObjectIdentifier(options.maskedViewClasses.first!), ObjectIdentifier(NSString.self))
XCTAssertEqual(options.unmaskedViewClasses.count, 1)
XCTAssertEqual(ObjectIdentifier(options.unmaskedViewClasses.first!), ObjectIdentifier(NSNumber.self))
XCTAssertEqual(options.quality, .low)
}

}
Loading