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

feat: add hooks for session replay #4974

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 23 additions & 0 deletions Samples/iOS-Swift/iOS-Swift/SentrySDKWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,29 @@ struct SentrySDKWrapper {
if #available(iOS 16.0, *), enableSessionReplay {
options.sessionReplay = SentryReplayOptions(sessionSampleRate: 0, onErrorSampleRate: 1, maskAllText: true, maskAllImages: true)
options.sessionReplay.quality = .high
options.sessionReplay.onRenderedScreenshot = { image in
guard let data = image.pngData() else {
return image
}
do {
try data.write(to: URL(fileURLWithPath: "/tmp/session-replay-0-rendered.png"))
} catch {
print("Failed to write in onRenderedScreenshot, reason: \(error)")
}
return image
}
options.sessionReplay.onMaskedScreenshot = { image in
guard let data = image.pngData() else {
return image
}
do {
try data.write(to: URL(fileURLWithPath: "/tmp/session-replay-1-masked.png"))
} catch {
print("Failed to write screenshot in onMaskingScreenshot, reason: \(error)")
}
return image
}

}

if #available(iOS 15.0, *), enableMetricKit {
Expand Down
2 changes: 2 additions & 0 deletions Sources/Sentry/SentrySessionReplayIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ - (void)setupWith:(SentryReplayOptions *)replayOptions
[[SentryViewPhotographer alloc] initWithRenderer:viewRenderer
redactOptions:replayOptions
enableExperimentalMaskRenderer:enableExperimentalRenderer];
[_viewPhotographer setOnRenderScreenshot:replayOptions.onRenderedScreenshot];
[_viewPhotographer setOnMaskScreenshot:replayOptions.onMaskedScreenshot];

if (touchTracker) {
_touchTracker = [[SentryTouchTracker alloc]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
@_implementationOnly import _SentryPrivate
import Foundation

#if canImport(UIKit)
import UIKit
#endif

@objcMembers
public class SentryReplayOptions: NSObject, SentryRedactOptions {

Expand Down Expand Up @@ -106,6 +110,22 @@ public class SentryReplayOptions: NSObject, SentryRedactOptions {
*/
public var unmaskedViewClasses = [AnyClass]()

#if canImport(UIKit)
/**
* Callback to modify the screenshot after rendered from the view hierarchy.
*
* This can be used to apply custom modifications to the screenshot before it is masked.
*/
public var onRenderedScreenshot: ((_ screenshot: UIImage) -> UIImage)?

/**
* Callback to modify the screenshot after masking.
*
* This can be used to apply custom modifications to the screenshot after it is masked.
*/
public var onMaskedScreenshot: ((_ screenshot: UIImage) -> UIImage)?
#endif

/**
* Enables the up to 5x faster experimental view renderer used by the Session Replay integration.
*
Expand Down
11 changes: 8 additions & 3 deletions Sources/Swift/Tools/ViewCapture/SentryViewPhotographer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class SentryViewPhotographer: NSObject, SentryViewScreenshotProvider {

var renderer: SentryViewRenderer

var onRenderScreenshot: ((UIImage) -> UIImage)?
var onMaskScreenshot: ((UIImage) -> UIImage)?

/// Creates a view photographer used to convert a view hierarchy to an image.
///
/// - Parameters:
Expand All @@ -39,13 +42,15 @@ class SentryViewPhotographer: NSObject, SentryViewScreenshotProvider {
// The render method is synchronous and must be called on the main thread.
// This is because the render method accesses the view hierarchy which is managed from the main thread.
let renderedScreenshot = renderer.render(view: view)
let processedRenderedScreenshot = onRenderScreenshot?(renderedScreenshot) ?? renderedScreenshot

dispatchQueue.dispatchAsync { [maskRenderer] in
dispatchQueue.dispatchAsync { [maskRenderer, onMaskScreenshot] in
// The mask renderer does not need to be on the main thread.
// Moving it to a background thread to avoid blocking the main thread, therefore reducing the performance
// impact/lag of the user interface.
let maskedScreenshot = maskRenderer.maskScreenshot(screenshot: renderedScreenshot, size: viewSize, masking: redact)
onComplete(maskedScreenshot)
let maskedScreenshot = maskRenderer.maskScreenshot(screenshot: processedRenderedScreenshot, size: viewSize, masking: redact)
let processedMaskedScreenshot = onMaskScreenshot?(maskedScreenshot) ?? maskedScreenshot
onComplete(processedMaskedScreenshot)
}
}

Expand Down
Loading