Skip to content

Commit

Permalink
Expose enableWatchdogTerminationTracking in common options (#281)
Browse files Browse the repository at this point in the history
* update

* update

* format

* update

* Update CHANGELOG.md

* update
  • Loading branch information
buenaflor authored Oct 8, 2024
1 parent 887ef60 commit 1bf5cc1
Show file tree
Hide file tree
Showing 9 changed files with 132 additions and 128 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Sentry.init { options ->
}
```
- Add `Sentry.isEnabled()` API to common code ([#273](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/273))
- Add `enableWatchdogTerminationTracking` in common options ([#281](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/281))

### Dependencies

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
public final fun getEnableAppHangTracking ()Z
public final fun getEnableAutoSessionTracking ()Z
public final fun getEnableCaptureFailedRequests ()Z
public final fun getEnableWatchdogTerminationTracking ()Z
public final fun getEnvironment ()Ljava/lang/String;
public final fun getExperimental ()Lio/sentry/kotlin/multiplatform/SentryOptions$ExperimentalOptions;
public final fun getFailedRequestStatusCodes ()Ljava/util/List;
Expand Down Expand Up @@ -201,6 +202,7 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
public final fun setEnableAppHangTracking (Z)V
public final fun setEnableAutoSessionTracking (Z)V
public final fun setEnableCaptureFailedRequests (Z)V
public final fun setEnableWatchdogTerminationTracking (Z)V
public final fun setEnvironment (Ljava/lang/String;)V
public final fun setFailedRequestStatusCodes (Ljava/util/List;)V
public final fun setFailedRequestTargets (Ljava/util/List;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
public final fun getEnableAppHangTracking ()Z
public final fun getEnableAutoSessionTracking ()Z
public final fun getEnableCaptureFailedRequests ()Z
public final fun getEnableWatchdogTerminationTracking ()Z
public final fun getEnvironment ()Ljava/lang/String;
public final fun getExperimental ()Lio/sentry/kotlin/multiplatform/SentryOptions$ExperimentalOptions;
public final fun getFailedRequestStatusCodes ()Ljava/util/List;
Expand Down Expand Up @@ -198,6 +199,7 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
public final fun setEnableAppHangTracking (Z)V
public final fun setEnableAutoSessionTracking (Z)V
public final fun setEnableCaptureFailedRequests (Z)V
public final fun setEnableWatchdogTerminationTracking (Z)V
public final fun setEnvironment (Ljava/lang/String;)V
public final fun setFailedRequestStatusCodes (Ljava/util/List;)V
public final fun setFailedRequestTargets (Ljava/util/List;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,62 @@ internal fun SentryOptions.toCocoaOptionsConfiguration(): (CocoaSentryOptions?)
* Applies the given options to this CocoaSentryOptions.
* This avoids code duplication for init on iOS.
*/
internal fun CocoaSentryOptions.applyCocoaBaseOptions(options: SentryOptions) {
dsn = options.dsn
attachStacktrace = options.attachStackTrace
dist = options.dist
options.environment?.let {
environment = it
internal fun CocoaSentryOptions.applyCocoaBaseOptions(kmpOptions: SentryOptions) {
val cocoaOptions = this
cocoaOptions.dsn = kmpOptions.dsn
cocoaOptions.attachStacktrace = kmpOptions.attachStackTrace
cocoaOptions.dist = kmpOptions.dist
kmpOptions.environment?.let {
cocoaOptions.environment = it
}
releaseName = options.release
debug = options.debug
sessionTrackingIntervalMillis = options.sessionTrackingIntervalMillis.convert()
enableAutoSessionTracking = options.enableAutoSessionTracking
maxAttachmentSize = options.maxAttachmentSize.convert()
maxBreadcrumbs = options.maxBreadcrumbs.convert()
enableAppHangTracking = options.enableAppHangTracking
appHangTimeoutInterval = options.appHangTimeoutIntervalMillis.toDouble()
options.sampleRate?.let {
sampleRate = NSNumber(double = it)
cocoaOptions.releaseName = kmpOptions.release
cocoaOptions.debug = kmpOptions.debug
cocoaOptions.sessionTrackingIntervalMillis = kmpOptions.sessionTrackingIntervalMillis.convert()
cocoaOptions.enableAutoSessionTracking = kmpOptions.enableAutoSessionTracking
cocoaOptions.maxAttachmentSize = kmpOptions.maxAttachmentSize.convert()
cocoaOptions.maxBreadcrumbs = kmpOptions.maxBreadcrumbs.convert()
cocoaOptions.enableAppHangTracking = kmpOptions.enableAppHangTracking
cocoaOptions.enableWatchdogTerminationTracking = kmpOptions.enableWatchdogTerminationTracking
cocoaOptions.appHangTimeoutInterval = kmpOptions.appHangTimeoutIntervalMillis.toDouble()
kmpOptions.sampleRate?.let {
cocoaOptions.sampleRate = NSNumber(double = it)
}
options.tracesSampleRate?.let {
tracesSampleRate = NSNumber(double = it)
kmpOptions.tracesSampleRate?.let {
cocoaOptions.tracesSampleRate = NSNumber(double = it)
}
beforeSend = { event ->
cocoaOptions.beforeSend = { event ->
val sdk = event?.sdk?.toMutableMap()

val packages = options.sdk?.packages?.map {
val packages = kmpOptions.sdk?.packages?.map {
mapOf("name" to it.name, "version" to it.version)
}?.toMutableList() ?: mutableListOf()

sdk?.set("packages", packages)

event?.sdk = sdk

if (options.beforeSend == null) {
if (kmpOptions.beforeSend == null) {
event
} else {
event?.let { SentryEvent(it) }?.let { unwrappedEvent ->
val result = options.beforeSend?.invoke(unwrappedEvent)
val result = kmpOptions.beforeSend?.invoke(unwrappedEvent)
result?.let { event.applyKmpEvent(it) }
}
}
}

beforeBreadcrumb = { cocoaBreadcrumb ->
if (options.beforeBreadcrumb == null) {
cocoaOptions.beforeBreadcrumb = { cocoaBreadcrumb ->
if (kmpOptions.beforeBreadcrumb == null) {
cocoaBreadcrumb
} else {
cocoaBreadcrumb?.toKmpBreadcrumb()
?.let { options.beforeBreadcrumb?.invoke(it) }?.toCocoaBreadcrumb()
?.let { kmpOptions.beforeBreadcrumb?.invoke(it) }?.toCocoaBreadcrumb()
}
}

enableCaptureFailedRequests = options.enableCaptureFailedRequests
failedRequestTargets = options.failedRequestTargets
failedRequestStatusCodes = options.failedRequestStatusCodes.map {
cocoaOptions.enableCaptureFailedRequests = kmpOptions.enableCaptureFailedRequests
cocoaOptions.failedRequestTargets = kmpOptions.failedRequestTargets
cocoaOptions.failedRequestStatusCodes = kmpOptions.failedRequestStatusCodes.map {
SentryHttpStatusCodeRange(
min = it.min.convert(),
max = it.max.convert()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package io.sentry.kotlin.multiplatform

import io.sentry.kotlin.multiplatform.extensions.toCocoaOptionsConfiguration
import io.sentry.kotlin.multiplatform.utils.fakeDsn
import kotlinx.cinterop.convert
import kotlin.test.assertEquals

actual interface PlatformOptions : CommonPlatformOptions {
val enableWatchdogTerminationTracking: Boolean
}

open class SentryAppleOptionsWrapper(private val cocoaOptions: CocoaSentryOptions) :
PlatformOptions {
override val dsn: String?
get() = cocoaOptions.dsn

override val attachStackTrace: Boolean
get() = cocoaOptions.attachStacktrace

override val release: String?
get() = cocoaOptions.releaseName

override val debug: Boolean
get() = cocoaOptions.debug

override val environment: String
get() = cocoaOptions.environment

override val dist: String?
get() = cocoaOptions.dist

override val enableAutoSessionTracking: Boolean
get() = cocoaOptions.enableAutoSessionTracking

override val sessionTrackingIntervalMillis: Long
get() = cocoaOptions.sessionTrackingIntervalMillis.convert()

override val maxBreadcrumbs: Int
get() = cocoaOptions.maxBreadcrumbs.convert()

override val maxAttachmentSize: Long
get() = cocoaOptions.maxAttachmentSize.convert()

override val sampleRate: Double?
get() = cocoaOptions.sampleRate?.doubleValue

override val tracesSampleRate: Double?
get() = cocoaOptions.tracesSampleRate?.doubleValue

override val enableWatchdogTerminationTracking: Boolean
get() = cocoaOptions.enableWatchdogTerminationTracking

override fun applyFromOptions(options: SentryOptions) {
options.toCocoaOptionsConfiguration().invoke(cocoaOptions)
}
}

expect interface ApplePlatformOptions : PlatformOptions

actual fun createPlatformOptions(): PlatformOptions = createApplePlatformOptions()

expect fun createApplePlatformOptions(): PlatformOptions

expect fun ApplePlatformOptions.assertApplePlatformSpecificOptions(options: SentryOptions)

actual fun PlatformOptions.assertPlatformSpecificOptions(options: SentryOptions) {
(this as ApplePlatformOptions).assertApplePlatformSpecificOptions(options)

val appleOptions = this
assertEquals(appleOptions.enableWatchdogTerminationTracking, options.enableWatchdogTerminationTracking)
}

actual fun createSentryPlatformOptionsConfiguration(): PlatformOptionsConfiguration = {
val cocoaOptions = it as CocoaSentryOptions
cocoaOptions.dsn = fakeDsn
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ public open class SentryOptions {
*/
public var anrTimeoutIntervalMillis: Long = DEFAULT_ANR_TIMEOUT_INTERVAL_MILLIS

/**
* Whether to enable Watchdog Termination tracking or not.
*
* **Default**: Enabled.
*
* **Platform Availability**: Cocoa.
*
*/
public var enableWatchdogTerminationTracking: Boolean = true

/**
* Experimental options for new features, these options are going to be promoted to SentryOptions
* before GA.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,11 @@ class SentryOptionsTest : BaseSentryTest() {
assertTrue(options.experimental.sessionReplay.redactAllText)
assertTrue(options.experimental.sessionReplay.redactAllImages)
assertEquals(SentryReplayOptions.Quality.MEDIUM, options.experimental.sessionReplay.quality)
assertTrue(options.enableWatchdogTerminationTracking)
}

@Test
fun `GIVEN SentryOptions WHEN applyFromOptions THEN applies values to native options`() {
fun `GIVEN non-default SentryOptions WHEN options initialized THEN applies values to native options`() {
val options = SentryOptions().apply {
dsn = fakeDsn
attachStackTrace = false
Expand All @@ -154,6 +155,7 @@ class SentryOptionsTest : BaseSentryTest() {
appHangTimeoutIntervalMillis = 1000L
isAnrEnabled = false
anrTimeoutIntervalMillis = 1000L
enableWatchdogTerminationTracking = false
experimental.sessionReplay.onErrorSampleRate = 0.5
experimental.sessionReplay.sessionSampleRate = 0.5
experimental.sessionReplay.redactAllText = false
Expand Down
Original file line number Diff line number Diff line change
@@ -1,61 +1,12 @@
package io.sentry.kotlin.multiplatform

import io.sentry.kotlin.multiplatform.extensions.toCocoaOptionsConfiguration
import io.sentry.kotlin.multiplatform.utils.fakeDsn
import kotlinx.cinterop.convert
actual interface ApplePlatformOptions : PlatformOptions

actual interface PlatformOptions : CommonPlatformOptions
class SentryTvWatchMacOsOptionsWrapper(cocoaOptions: CocoaSentryOptions) : SentryAppleOptionsWrapper(cocoaOptions)

class SentryTvWatchMacOsOptionsWrapper(private val cocoaOptions: CocoaSentryOptions) :
PlatformOptions {
override val dsn: String?
get() = cocoaOptions.dsn

override val attachStackTrace: Boolean
get() = cocoaOptions.attachStacktrace

override val release: String?
get() = cocoaOptions.releaseName

override val debug: Boolean
get() = cocoaOptions.debug

override val environment: String
get() = cocoaOptions.environment

override val dist: String?
get() = cocoaOptions.dist

override val enableAutoSessionTracking: Boolean
get() = cocoaOptions.enableAutoSessionTracking

override val sessionTrackingIntervalMillis: Long
get() = cocoaOptions.sessionTrackingIntervalMillis.convert()

override val maxBreadcrumbs: Int
get() = cocoaOptions.maxBreadcrumbs.convert()

override val maxAttachmentSize: Long
get() = cocoaOptions.maxAttachmentSize.convert()

override val sampleRate: Double?
get() = cocoaOptions.sampleRate?.doubleValue

override val tracesSampleRate: Double?
get() = cocoaOptions.tracesSampleRate?.doubleValue

override fun applyFromOptions(options: SentryOptions) {
options.toCocoaOptionsConfiguration().invoke(cocoaOptions)
}
}

actual fun createPlatformOptions(): PlatformOptions =
actual fun createApplePlatformOptions(): PlatformOptions =
SentryTvWatchMacOsOptionsWrapper(CocoaSentryOptions())

actual fun PlatformOptions.assertPlatformSpecificOptions(options: SentryOptions) {
actual fun ApplePlatformOptions.assertApplePlatformSpecificOptions(options: SentryOptions) {
// no-op
}

actual fun createSentryPlatformOptionsConfiguration(): PlatformOptionsConfiguration = {
it.dsn = fakeDsn
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ 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
import kotlin.test.assertEquals

actual interface PlatformOptions : CommonPlatformOptions {
actual interface ApplePlatformOptions : PlatformOptions {
val attachScreenshot: Boolean
val attachViewHierarchy: Boolean
val enableAppHangTracking: Boolean
val appHangTimeoutIntervalMillis: Long
val sessionReplay: SentryReplayOptions
}

class SentryIosOptionsWrapper(private val cocoaOptions: CocoaSentryOptions) : PlatformOptions {
class SentryIosOptionsWrapper(private val cocoaOptions: CocoaSentryOptions) : SentryAppleOptionsWrapper(cocoaOptions), ApplePlatformOptions {
override val attachScreenshot: Boolean
get() = cocoaOptions.attachScreenshot

Expand All @@ -27,42 +25,6 @@ class SentryIosOptionsWrapper(private val cocoaOptions: CocoaSentryOptions) : Pl
override val appHangTimeoutIntervalMillis: Long
get() = cocoaOptions.appHangTimeoutInterval.toLong()

override val dsn: String?
get() = cocoaOptions.dsn

override val attachStackTrace: Boolean
get() = cocoaOptions.attachStacktrace

override val release: String?
get() = cocoaOptions.releaseName

override val debug: Boolean
get() = cocoaOptions.debug

override val environment: String
get() = cocoaOptions.environment

override val dist: String?
get() = cocoaOptions.dist

override val enableAutoSessionTracking: Boolean
get() = cocoaOptions.enableAutoSessionTracking

override val sessionTrackingIntervalMillis: Long
get() = cocoaOptions.sessionTrackingIntervalMillis.convert()

override val maxBreadcrumbs: Int
get() = cocoaOptions.maxBreadcrumbs.convert()

override val maxAttachmentSize: Long
get() = cocoaOptions.maxAttachmentSize.convert()

override val sampleRate: Double?
get() = cocoaOptions.sampleRate?.doubleValue

override val tracesSampleRate: Double?
get() = cocoaOptions.tracesSampleRate?.doubleValue

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

Expand All @@ -71,9 +33,9 @@ class SentryIosOptionsWrapper(private val cocoaOptions: CocoaSentryOptions) : Pl
}
}

actual fun createPlatformOptions(): PlatformOptions = SentryIosOptionsWrapper(CocoaSentryOptions())
actual fun createApplePlatformOptions(): PlatformOptions = SentryIosOptionsWrapper(CocoaSentryOptions())

actual fun PlatformOptions.assertPlatformSpecificOptions(options: SentryOptions) {
actual fun ApplePlatformOptions.assertApplePlatformSpecificOptions(options: SentryOptions) {
assertEquals(attachScreenshot, options.attachScreenshot)
assertEquals(attachViewHierarchy, options.attachViewHierarchy)
assertEquals(enableAppHangTracking, options.enableAppHangTracking)
Expand All @@ -84,7 +46,3 @@ actual fun PlatformOptions.assertPlatformSpecificOptions(options: SentryOptions)
assertEquals(sessionReplay.sessionSampleRate().toDouble(), options.experimental.sessionReplay.sessionSampleRate)
assertEquals(sessionReplay.quality(), options.experimental.sessionReplay.quality.ordinal.toLong())
}

actual fun createSentryPlatformOptionsConfiguration(): PlatformOptionsConfiguration = {
it.dsn = fakeDsn
}

0 comments on commit 1bf5cc1

Please sign in to comment.