Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Persist buffer replay type when switching to session
  • Loading branch information
romtsn committed Aug 5, 2024
commit 7027bd7ff015b873e3295c8f66287fc284ec1261
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ internal abstract class BaseCaptureStrategy(
override var currentSegment: Int by persistableAtomic(initialValue = -1, propertyName = SEGMENT_KEY_ID)
override val replayCacheDir: File? get() = cache?.replayCacheDir

private var replayType by persistableAtomic<ReplayType>(propertyName = SEGMENT_KEY_REPLAY_TYPE)
override var replayType by persistableAtomic<ReplayType>(propertyName = SEGMENT_KEY_REPLAY_TYPE)
protected val currentEvents: LinkedList<RRWebEvent> = PersistableLinkedList(
propertyName = SEGMENT_KEY_REPLAY_RECORDING,
options,
Expand All @@ -105,15 +105,15 @@ internal abstract class BaseCaptureStrategy(
override fun start(
recorderConfig: ScreenshotRecorderConfig,
segmentId: Int,
replayId: SentryId
replayId: SentryId,
replayType: ReplayType?
) {
cache = replayCacheProvider?.invoke(replayId, recorderConfig) ?: ReplayCache(options, replayId, recorderConfig)

// TODO: this should be persisted even after conversion
replayType = if (this is SessionCaptureStrategy) SESSION else BUFFER
this.replayType = replayType ?: (if (this is SessionCaptureStrategy) SESSION else BUFFER)
this.recorderConfig = recorderConfig
currentSegment = segmentId
currentReplayId = replayId
this.currentSegment = segmentId
this.currentReplayId = replayId

segmentTimestamp = DateUtils.getCurrentDateTime()
replayStartTimestamp.set(dateProvider.currentTimeMillis)
Expand All @@ -140,7 +140,7 @@ internal abstract class BaseCaptureStrategy(
segmentId: Int,
height: Int,
width: Int,
replayType: ReplayType = SESSION,
replayType: ReplayType = this.replayType,
cache: ReplayCache? = this.cache,
frameRate: Int = recorderConfig.frameRate,
screenAtStart: String? = this.screenAtStart,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import io.sentry.SentryLevel.DEBUG
import io.sentry.SentryLevel.ERROR
import io.sentry.SentryLevel.INFO
import io.sentry.SentryOptions
import io.sentry.SentryReplayEvent.ReplayType
import io.sentry.SentryReplayEvent.ReplayType.BUFFER
import io.sentry.android.replay.ReplayCache
import io.sentry.android.replay.ScreenshotRecorderConfig
Expand Down Expand Up @@ -46,9 +47,10 @@ internal class BufferCaptureStrategy(
override fun start(
recorderConfig: ScreenshotRecorderConfig,
segmentId: Int,
replayId: SentryId
replayId: SentryId,
replayType: ReplayType?
) {
super.start(recorderConfig, segmentId, replayId)
super.start(recorderConfig, segmentId, replayId, replayType)

hub?.configureScope {
val screen = it.screen?.substringAfterLast('.')
Expand Down Expand Up @@ -159,7 +161,7 @@ internal class BufferCaptureStrategy(
}
// we hand over replayExecutor to the new strategy to preserve order of execution
val captureStrategy = SessionCaptureStrategy(options, hub, dateProvider, replayExecutor)
captureStrategy.start(recorderConfig, segmentId = currentSegment, replayId = currentReplayId)
captureStrategy.start(recorderConfig, segmentId = currentSegment, replayId = currentReplayId, replayType = BUFFER)
return captureStrategy
}

Expand Down Expand Up @@ -250,7 +252,7 @@ internal class BufferCaptureStrategy(

replayExecutor.submitSafely(options, "$TAG.$taskName") {
val segment =
createSegmentInternal(duration, currentSegmentTimestamp, replayId, segmentId, height, width, BUFFER)
createSegmentInternal(duration, currentSegmentTimestamp, replayId, segmentId, height, width)
onSegmentCreated(segment)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ internal interface CaptureStrategy {
var currentSegment: Int
var currentReplayId: SentryId
val replayCacheDir: File?
var replayType: ReplayType

fun start(
recorderConfig: ScreenshotRecorderConfig,
segmentId: Int = 0,
replayId: SentryId = SentryId()
replayId: SentryId = SentryId(),
replayType: ReplayType? = null
)

fun stop()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.sentry.IHub
import io.sentry.SentryLevel.DEBUG
import io.sentry.SentryLevel.INFO
import io.sentry.SentryOptions
import io.sentry.SentryReplayEvent.ReplayType
import io.sentry.android.replay.ReplayCache
import io.sentry.android.replay.ScreenshotRecorderConfig
import io.sentry.android.replay.capture.CaptureStrategy.ReplaySegment
Expand All @@ -31,9 +32,10 @@ internal class SessionCaptureStrategy(
override fun start(
recorderConfig: ScreenshotRecorderConfig,
segmentId: Int,
replayId: SentryId
replayId: SentryId,
replayType: ReplayType?
) {
super.start(recorderConfig, segmentId, replayId)
super.start(recorderConfig, segmentId, replayId, replayType)
// only set replayId on the scope if it's a full session, otherwise all events will be
// tagged with the replay that might never be sent when we're recording in buffer mode
hub?.configureScope {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyLong
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.argThat
import org.mockito.kotlin.check
import org.mockito.kotlin.doAnswer
Expand Down Expand Up @@ -160,7 +161,7 @@ class ReplayIntegrationTest {

replay.start()

verify(captureStrategy, never()).start(any(), any(), any())
verify(captureStrategy, never()).start(any(), any(), any(), anyOrNull())
}

@Test
Expand All @@ -186,7 +187,8 @@ class ReplayIntegrationTest {
verify(captureStrategy, times(1)).start(
any(),
eq(0),
argThat { this != SentryId.EMPTY_ID }
argThat { this != SentryId.EMPTY_ID },
anyOrNull()
)
}

Expand All @@ -201,7 +203,8 @@ class ReplayIntegrationTest {
verify(captureStrategy, never()).start(
any(),
eq(0),
argThat { this != SentryId.EMPTY_ID }
argThat { this != SentryId.EMPTY_ID },
anyOrNull()
)
}

Expand All @@ -216,7 +219,8 @@ class ReplayIntegrationTest {
verify(captureStrategy, times(1)).start(
any(),
eq(0),
argThat { this != SentryId.EMPTY_ID }
argThat { this != SentryId.EMPTY_ID },
anyOrNull()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,18 @@ class BufferCaptureStrategyTest {
assertEquals(strategy.currentReplayId, fixture.scope.replayId)
}

@Test
fun `convert persists buffer replayType when converting to session strategy`() {
val strategy = fixture.getSut()
strategy.start(fixture.recorderConfig)

val converted = strategy.convert()
assertEquals(
ReplayType.BUFFER,
converted.replayType
)
}

@Test
fun `captureReplay does not replayId to scope when not sampled`() {
val strategy = fixture.getSut(errorSampleRate = 0.0)
Expand Down