Skip to content

Commit b4f8dba

Browse files
feat(replay): add captureReplay without Event param for Hybrid SDKs (#3878)
1 parent 07b120c commit b4f8dba

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

Sources/Sentry/SentrySessionReplay.m

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,29 @@ - (void)captureReplayForEvent:(SentryEvent *)event;
129129
return;
130130
}
131131

132-
if ([_sentryRandom nextNumber] > _replayOptions.errorSampleRate) {
132+
BOOL didCaptureReplay = [self captureReplay];
133+
if (!didCaptureReplay) {
133134
return;
134135
}
135136

136-
[self startFullReplay];
137137
[self setEventContext:event];
138+
}
139+
140+
- (BOOL)captureReplay
141+
{
142+
if (!_isRunning) {
143+
return NO;
144+
}
145+
146+
if (_isFullSession) {
147+
return YES;
148+
}
149+
150+
if ([_sentryRandom nextNumber] > _replayOptions.errorSampleRate) {
151+
return NO;
152+
}
153+
154+
[self startFullReplay];
138155

139156
NSURL *finalPath = [_urlToCache URLByAppendingPathComponent:@"replay.mp4"];
140157
NSDate *replayStart =
@@ -143,6 +160,8 @@ - (void)captureReplayForEvent:(SentryEvent *)event;
143160
[self createAndCapture:finalPath
144161
duration:_replayOptions.errorReplayDuration
145162
startedAt:replayStart];
163+
164+
return YES;
146165
}
147166

148167
- (void)setEventContext:(SentryEvent *)event

Sources/Sentry/include/SentrySessionReplay.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ API_AVAILABLE(ios(16.0), tvos(16.0))
6262
*/
6363
- (void)captureReplayForEvent:(SentryEvent *)event;
6464

65+
/**
66+
* Captures a replay. This method is used by the Hybrid SDKs.
67+
*/
68+
- (BOOL)captureReplay;
69+
6570
@end
6671

6772
NS_ASSUME_NONNULL_END

Tests/SentryTests/Integrations/SessionReplay/SentrySessionReplayTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,18 @@ class SentrySessionReplayTests: XCTestCase {
191191
assertFullSession(sut, expected: false)
192192
}
193193

194+
@available(iOS 16.0, tvOS 16, *)
195+
func testChangeReplayMode_forHybridSDKEvent() {
196+
let fixture = startFixture()
197+
let sut = fixture.getSut(options: SentryReplayOptions(sessionSampleRate: 1, errorSampleRate: 1))
198+
sut.start(fixture.rootView, fullSession: false)
199+
200+
sut.capture()
201+
202+
expect(fixture.hub.scope.replayId) == sut.sessionReplayId.sentryIdString
203+
assertFullSession(sut, expected: true)
204+
}
205+
194206
@available(iOS 16.0, tvOS 16, *)
195207
func testSessionReplayMaximumDuration() {
196208
let fixture = startFixture()

0 commit comments

Comments
 (0)