Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
buenaflor committed Sep 30, 2024
1 parent 168def6 commit 01c2470
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
package io.sentry.kotlin.multiplatform.extensions

import io.sentry.android.core.SentryAndroidOptions
import io.sentry.kotlin.multiplatform.JvmSentryReplayQuality
import io.sentry.kotlin.multiplatform.SentryOptions
import io.sentry.kotlin.multiplatform.SentryReplayOptions
import kotlin.collections.forEach as kForEach

internal fun SentryOptions.toAndroidSentryOptionsCallback(): (SentryAndroidOptions) -> Unit = {
internal fun SentryOptions.toAndroidSentryOptionsCallback(): (SentryAndroidOptions) -> Unit = { androidOptions ->
val kmpOptions = this

// Apply base options available to all JVM targets
it.applyJvmBaseOptions(this)
androidOptions.applyJvmBaseOptions(kmpOptions)

// Apply Android specific options
it.isAttachScreenshot = this.attachScreenshot
it.isAttachViewHierarchy = this.attachViewHierarchy
it.isAnrEnabled = this.isAnrEnabled
it.anrTimeoutIntervalMillis = this.anrTimeoutIntervalMillis
androidOptions.isAttachScreenshot = kmpOptions.attachScreenshot
androidOptions.isAttachViewHierarchy = kmpOptions.attachViewHierarchy
androidOptions.isAnrEnabled = kmpOptions.isAnrEnabled
androidOptions.anrTimeoutIntervalMillis = kmpOptions.anrTimeoutIntervalMillis

// Replay options
it.experimental.sessionReplay.redactAllText = this.experimental.sessionReplay.redactAllText
it.experimental.sessionReplay.redactAllImages = this.experimental.sessionReplay.redactAllImages
it.experimental.sessionReplay.sessionSampleRate = this.experimental.sessionReplay.sessionSampleRate?.toDouble()
it.experimental.sessionReplay.errorSampleRate = this.experimental.sessionReplay.onErrorSampleRate?.toDouble()
androidOptions.experimental.sessionReplay.redactAllText = kmpOptions.experimental.sessionReplay.redactAllText
androidOptions.experimental.sessionReplay.redactAllImages = kmpOptions.experimental.sessionReplay.redactAllImages
androidOptions.experimental.sessionReplay.sessionSampleRate = kmpOptions.experimental.sessionReplay.sessionSampleRate
androidOptions.experimental.sessionReplay.errorSampleRate = kmpOptions.experimental.sessionReplay.onErrorSampleRate
androidOptions.experimental.sessionReplay.quality = kmpOptions.experimental.sessionReplay.quality.toAndroidSentryQuality()

// kForEach solves an issue with linter where it thinks forEach is the Java version
// see here: https://stackoverflow.com/questions/44751469/kotlin-extension-functions-suddenly-require-api-level-24/68897591#68897591
this.sdk?.packages?.kForEach { sdkPackage ->
it.sdkVersion?.addPackage(sdkPackage.name, sdkPackage.version)
androidOptions.sdkVersion?.addPackage(sdkPackage.name, sdkPackage.version)
}
}

internal fun SentryReplayOptions.Quality.toAndroidSentryQuality(): JvmSentryReplayQuality {
val kmpQuality = this
return when (kmpQuality) {
SentryReplayOptions.Quality.LOW -> JvmSentryReplayQuality.LOW
SentryReplayOptions.Quality.MEDIUM -> JvmSentryReplayQuality.MEDIUM
SentryReplayOptions.Quality.HIGH -> JvmSentryReplayQuality.HIGH

Check warning on line 40 in sentry-kotlin-multiplatform/src/androidMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryOptionsExtensions.android.kt

View check run for this annotation

Codecov / codecov/patch

sentry-kotlin-multiplatform/src/androidMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryOptionsExtensions.android.kt#L40

Added line #L40 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import io.sentry.android.core.SentryAndroidOptions
import io.sentry.kotlin.multiplatform.extensions.toAndroidSentryOptionsCallback
import io.sentry.kotlin.multiplatform.utils.fakeDsn
import kotlin.test.assertEquals
import io.sentry.SentryReplayOptions as NativeSentryReplayOptions

actual interface PlatformOptions : CommonPlatformOptions {
val isAnrEnabled: Boolean
val anrTimeoutIntervalMillis: Long
val attachScreenshot: Boolean
val attachViewHierarchy: Boolean
val sessionReplay: NativeSentryReplayOptions
}

class SentryAndroidOptionsWrapper(private val androidOptions: SentryAndroidOptions) :
Expand Down Expand Up @@ -62,6 +64,9 @@ class SentryAndroidOptionsWrapper(private val androidOptions: SentryAndroidOptio
override val attachViewHierarchy: Boolean
get() = androidOptions.isAttachViewHierarchy

override val sessionReplay: NativeSentryReplayOptions
get() = androidOptions.experimental.sessionReplay

override fun applyFromOptions(options: SentryOptions) {
options.toAndroidSentryOptionsCallback().invoke(androidOptions)
}
Expand All @@ -75,6 +80,11 @@ actual fun PlatformOptions.assertPlatformSpecificOptions(options: SentryOptions)
assertEquals(attachViewHierarchy, options.attachViewHierarchy)
assertEquals(isAnrEnabled, options.isAnrEnabled)
assertEquals(anrTimeoutIntervalMillis, options.anrTimeoutIntervalMillis)
assertEquals(sessionReplay.redactAllText, options.experimental.sessionReplay.redactAllText)
assertEquals(sessionReplay.redactAllImages, options.experimental.sessionReplay.redactAllImages)
assertEquals(sessionReplay.errorSampleRate, options.experimental.sessionReplay.onErrorSampleRate)
assertEquals(sessionReplay.sessionSampleRate, options.experimental.sessionReplay.sessionSampleRate)
assertEquals(sessionReplay.quality.name, options.experimental.sessionReplay.quality.name)
}

actual fun createSentryPlatformOptionsConfiguration(): PlatformOptionsConfiguration = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import io.sentry.Scope
import io.sentry.SentryEvent
import io.sentry.SentryLevel
import io.sentry.SentryOptions
import io.sentry.SentryReplayOptions
import io.sentry.UserFeedback
import io.sentry.protocol.Contexts
import io.sentry.protocol.Message
Expand All @@ -27,3 +28,4 @@ internal typealias JvmSentryEvent = SentryEvent
internal typealias JvmMessage = Message
internal typealias JvmSentryException = SentryException
internal typealias JvmContexts = Contexts
internal typealias JvmSentryReplayQuality = SentryReplayOptions.SentryReplayQuality
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,29 @@ public data class SentryReplayOptions(
* The default is true.
*/
public var redactAllImages: Boolean = true,
)

/**
* Defines the quality of the session replay. The higher the quality, the more accurate the replay
* will be, but also more data to transfer and more CPU load, defaults to MEDIUM.
*/
public var quality: Quality = Quality.MEDIUM
) {
public enum class Quality(
/** The scale related to the window size (in dp) at which the replay will be created. */
public val sizeScale: Float,
/**
* Defines the quality of the session replay. Higher bit rates have better replay quality, but
* also affect the final payload size to transfer, defaults to 40kbps.
*/
public val bitRate: Int
) {
/** Video Scale: 80% Bit Rate: 50.000 */
LOW(0.8f, 50000),

/** Video Scale: 100% Bit Rate: 75.000 */
MEDIUM(1.0f, 75000),

/** Video Scale: 100% Bit Rate: 100.000 */
HIGH(1.0f, 100000)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ class SentryOptionsTest : BaseSentryTest() {
assertEquals(2000L, options.appHangTimeoutIntervalMillis)
assertTrue(options.isAnrEnabled)
assertEquals(5000L, options.anrTimeoutIntervalMillis)
assertNull(options.experimental.sessionReplay.onErrorSampleRate)
assertNull(options.experimental.sessionReplay.sessionSampleRate)
assertTrue(options.experimental.sessionReplay.redactAllText)
assertTrue(options.experimental.sessionReplay.redactAllImages)
assertEquals(SentryReplayOptions.Quality.MEDIUM, options.experimental.sessionReplay.quality)
}

@Test
Expand All @@ -149,6 +154,11 @@ class SentryOptionsTest : BaseSentryTest() {
appHangTimeoutIntervalMillis = 1000L
isAnrEnabled = false
anrTimeoutIntervalMillis = 1000L
experimental.sessionReplay.onErrorSampleRate = 0.5
experimental.sessionReplay.sessionSampleRate = 0.5
experimental.sessionReplay.redactAllText = false
experimental.sessionReplay.redactAllImages = false
experimental.sessionReplay.quality = SentryReplayOptions.Quality.LOW
}

val platformOptions = createPlatformOptions()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ internal fun SentryOptions.toIosOptionsConfiguration(): (CocoaSentryOptions?) ->
setRedactAllImages(kmpOptions.experimental.sessionReplay.redactAllImages)
kmpOptions.experimental.sessionReplay.sessionSampleRate?.let { setSessionSampleRate(it.toFloat()) }
kmpOptions.experimental.sessionReplay.onErrorSampleRate?.let { setOnErrorSampleRate(it.toFloat()) }
setQuality(kmpOptions.experimental.sessionReplay.quality.ordinal.toLong())
}
} ?: run {
// Log a warning if options is null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.sentry.kotlin.multiplatform

import cocoapods.Sentry.SentryReplayOptions
import io.sentry.kotlin.multiplatform.extensions.toIosOptionsConfiguration
import io.sentry.kotlin.multiplatform.utils.fakeDsn
import kotlinx.cinterop.convert
Expand All @@ -10,6 +11,7 @@ actual interface PlatformOptions : CommonPlatformOptions {
val attachViewHierarchy: Boolean
val enableAppHangTracking: Boolean
val appHangTimeoutIntervalMillis: Long
val sessionReplay: SentryReplayOptions
}

class SentryIosOptionsWrapper(private val cocoaOptions: CocoaSentryOptions) : PlatformOptions {
Expand Down Expand Up @@ -61,6 +63,9 @@ class SentryIosOptionsWrapper(private val cocoaOptions: CocoaSentryOptions) : Pl
override val tracesSampleRate: Double?
get() = cocoaOptions.tracesSampleRate?.doubleValue

override val sessionReplay: SentryReplayOptions
get() = cocoaOptions.experimental.sessionReplay()

override fun applyFromOptions(options: SentryOptions) {
options.toIosOptionsConfiguration().invoke(cocoaOptions)
}
Expand All @@ -73,6 +78,11 @@ actual fun PlatformOptions.assertPlatformSpecificOptions(options: SentryOptions)
assertEquals(attachViewHierarchy, options.attachViewHierarchy)
assertEquals(enableAppHangTracking, options.enableAppHangTracking)
assertEquals(appHangTimeoutIntervalMillis, options.appHangTimeoutIntervalMillis)
assertEquals(sessionReplay.redactAllText(), options.experimental.sessionReplay.redactAllText)
assertEquals(sessionReplay.redactAllImages(), options.experimental.sessionReplay.redactAllImages)
assertEquals(sessionReplay.onErrorSampleRate().toDouble(), options.experimental.sessionReplay.onErrorSampleRate)
assertEquals(sessionReplay.sessionSampleRate().toDouble(), options.experimental.sessionReplay.sessionSampleRate)
assertEquals(sessionReplay.quality(), options.experimental.sessionReplay.quality.ordinal.toLong())
}

actual fun createSentryPlatformOptionsConfiguration(): PlatformOptionsConfiguration = {
Expand Down

0 comments on commit 01c2470

Please sign in to comment.