From 27c2e034ee606d8d838e3cebf8dfd1383333836a Mon Sep 17 00:00:00 2001 From: Giancarlo Buenaflor Date: Tue, 8 Oct 2024 01:33:32 +0200 Subject: [PATCH] Add `Sentry.isEnabled()` to common code (#273) * add function * fix imports * update changelog * Format code * update * update api --------- Co-authored-by: Sentry Github Bot --- CHANGELOG.md | 6 ++++++ .../api/android/sentry-kotlin-multiplatform.api | 1 + .../api/jvm/sentry-kotlin-multiplatform.api | 1 + .../kotlin/multiplatform/SentryBridge.apple.kt | 4 ++++ .../multiplatform/SentryBridge.commonJvm.kt | 4 ++++ .../sentry/kotlin/multiplatform/SentryBridge.kt | 2 ++ .../io/sentry/kotlin/multiplatform/SentryKMP.kt | 7 +++++++ .../kotlin/multiplatform/SentryIntegrationTest.kt | 15 +++++++++++++++ 8 files changed, 40 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c1029f4..65618461 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +### Features + +- Add `Sentry.isEnabled()` API to common code ([#273](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/273)) + ## 0.9.0 ### Improvements diff --git a/sentry-kotlin-multiplatform/api/android/sentry-kotlin-multiplatform.api b/sentry-kotlin-multiplatform/api/android/sentry-kotlin-multiplatform.api index e52c5bac..abd48718 100644 --- a/sentry-kotlin-multiplatform/api/android/sentry-kotlin-multiplatform.api +++ b/sentry-kotlin-multiplatform/api/android/sentry-kotlin-multiplatform.api @@ -86,6 +86,7 @@ public final class io/sentry/kotlin/multiplatform/Sentry { public final fun init (Lkotlin/jvm/functions/Function1;)V public final fun initWithPlatformOptions (Lkotlin/jvm/functions/Function1;)V public final fun isCrashedLastRun ()Z + public final fun isEnabled ()Z public final fun setUser (Lio/sentry/kotlin/multiplatform/protocol/User;)V } diff --git a/sentry-kotlin-multiplatform/api/jvm/sentry-kotlin-multiplatform.api b/sentry-kotlin-multiplatform/api/jvm/sentry-kotlin-multiplatform.api index f44183bc..c1d27726 100644 --- a/sentry-kotlin-multiplatform/api/jvm/sentry-kotlin-multiplatform.api +++ b/sentry-kotlin-multiplatform/api/jvm/sentry-kotlin-multiplatform.api @@ -83,6 +83,7 @@ public final class io/sentry/kotlin/multiplatform/Sentry { public final fun init (Lkotlin/jvm/functions/Function1;)V public final fun initWithPlatformOptions (Lkotlin/jvm/functions/Function1;)V public final fun isCrashedLastRun ()Z + public final fun isEnabled ()Z public final fun setUser (Lio/sentry/kotlin/multiplatform/protocol/User;)V } diff --git a/sentry-kotlin-multiplatform/src/appleMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.apple.kt b/sentry-kotlin-multiplatform/src/appleMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.apple.kt index cad8180d..a954e90b 100644 --- a/sentry-kotlin-multiplatform/src/appleMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.apple.kt +++ b/sentry-kotlin-multiplatform/src/appleMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.apple.kt @@ -108,6 +108,10 @@ internal actual class SentryBridge actual constructor(private val sentryInstance return SentrySDK.crashedLastRun() } + actual fun isEnabled(): Boolean { + return SentrySDK.isEnabled() + } + actual fun close() { SentrySDK.close() } diff --git a/sentry-kotlin-multiplatform/src/commonJvmMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.commonJvm.kt b/sentry-kotlin-multiplatform/src/commonJvmMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.commonJvm.kt index 66443f27..4ddb8d4b 100644 --- a/sentry-kotlin-multiplatform/src/commonJvmMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.commonJvm.kt +++ b/sentry-kotlin-multiplatform/src/commonJvmMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.commonJvm.kt @@ -71,6 +71,10 @@ internal actual class SentryBridge actual constructor(private val sentryInstance return Sentry.isCrashedLastRun() ?: false } + actual fun isEnabled(): Boolean { + return Sentry.isEnabled() + } + actual fun close() { Sentry.close() } diff --git a/sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.kt b/sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.kt index 8b517677..167c51ad 100644 --- a/sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.kt +++ b/sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryBridge.kt @@ -30,5 +30,7 @@ internal expect class SentryBridge(sentryInstance: SentryInstance = SentryPlatfo fun isCrashedLastRun(): Boolean + fun isEnabled(): Boolean + fun close() } diff --git a/sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryKMP.kt b/sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryKMP.kt index 6d9b2f3e..9535a38c 100644 --- a/sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryKMP.kt +++ b/sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryKMP.kt @@ -154,6 +154,13 @@ public object Sentry { throw RuntimeException("Uncaught Exception from Kotlin Multiplatform.") } + /** + * Checks if the SDK is enabled. + */ + public fun isEnabled(): Boolean { + return bridge.isEnabled() + } + /** * Closes the SDK. */ diff --git a/sentry-kotlin-multiplatform/src/commonTest/kotlin/io/sentry/kotlin/multiplatform/SentryIntegrationTest.kt b/sentry-kotlin-multiplatform/src/commonTest/kotlin/io/sentry/kotlin/multiplatform/SentryIntegrationTest.kt index 9cb7a8b0..222e9b7f 100644 --- a/sentry-kotlin-multiplatform/src/commonTest/kotlin/io/sentry/kotlin/multiplatform/SentryIntegrationTest.kt +++ b/sentry-kotlin-multiplatform/src/commonTest/kotlin/io/sentry/kotlin/multiplatform/SentryIntegrationTest.kt @@ -7,6 +7,7 @@ import kotlinx.coroutines.test.runTest import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertNotEquals import kotlin.test.assertNotNull import kotlin.test.assertTrue @@ -225,6 +226,20 @@ class SentryIntegrationTest : BaseSentryTest() { assertEquals(expectedUsername, actualUsername) } + @Test + fun `isEnabled returns true when SDK is enabled`() { + sentryInit { + it.dsn = fakeDsn + } + + assertTrue(Sentry.isEnabled()) + } + + @Test + fun `isEnabled returns false when SDK is disabled`() { + assertFalse(Sentry.isEnabled()) + } + @Test fun `global scope sets context correctly with different data types`() = runTest { val stringKey = "stringKey"