Skip to content

Commit 9e7bbfd

Browse files
authored
feat: add sample rate configuration (#144)
* Add sample rate configuration * fix formatting * run api dump * Set to NSNumber * Modify NSNumber usage * Add another test * Add changelog * Ignore file for detekt
1 parent 1f7cab4 commit 9e7bbfd

File tree

9 files changed

+66
-3
lines changed

9 files changed

+66
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- Add sample & trace rate configuration ([#144](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/144))
78
- Remove need for context in Sentry.init for Android ([#117](https://github.com/getsentry/sentry-kotlin-multiplatform/pull/117))
89

910
### Dependencies

config/detekt/detekt.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@ complexity:
2626
"**/JvmScopeProvider.kt",
2727
"**/Breadcrumb.kt",
2828
]
29+
LongMethod:
30+
excludes:
31+
- "**/SentryOptionsExtensions.*"
2932

sentry-kotlin-multiplatform/api/android/sentry-kotlin-multiplatform.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,10 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
173173
public final fun getMaxAttachmentSize ()J
174174
public final fun getMaxBreadcrumbs ()I
175175
public final fun getRelease ()Ljava/lang/String;
176+
public final fun getSampleRate ()Ljava/lang/Double;
176177
public final fun getSdk ()Lio/sentry/kotlin/multiplatform/protocol/SdkVersion;
177178
public final fun getSessionTrackingIntervalMillis ()J
179+
public final fun getTracesSampleRate ()Ljava/lang/Double;
178180
public final fun setAttachScreenshot (Z)V
179181
public final fun setAttachStackTrace (Z)V
180182
public final fun setAttachThreads (Z)V
@@ -192,8 +194,10 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
192194
public final fun setMaxAttachmentSize (J)V
193195
public final fun setMaxBreadcrumbs (I)V
194196
public final fun setRelease (Ljava/lang/String;)V
197+
public final fun setSampleRate (Ljava/lang/Double;)V
195198
public final fun setSdk (Lio/sentry/kotlin/multiplatform/protocol/SdkVersion;)V
196199
public final fun setSessionTrackingIntervalMillis (J)V
200+
public final fun setTracesSampleRate (Ljava/lang/Double;)V
197201
}
198202

199203
public final class io/sentry/kotlin/multiplatform/protocol/Breadcrumb {

sentry-kotlin-multiplatform/api/jvm/sentry-kotlin-multiplatform.api

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,10 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
170170
public final fun getMaxAttachmentSize ()J
171171
public final fun getMaxBreadcrumbs ()I
172172
public final fun getRelease ()Ljava/lang/String;
173+
public final fun getSampleRate ()Ljava/lang/Double;
173174
public final fun getSdk ()Lio/sentry/kotlin/multiplatform/protocol/SdkVersion;
174175
public final fun getSessionTrackingIntervalMillis ()J
176+
public final fun getTracesSampleRate ()Ljava/lang/Double;
175177
public final fun setAttachScreenshot (Z)V
176178
public final fun setAttachStackTrace (Z)V
177179
public final fun setAttachThreads (Z)V
@@ -189,8 +191,10 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
189191
public final fun setMaxAttachmentSize (J)V
190192
public final fun setMaxBreadcrumbs (I)V
191193
public final fun setRelease (Ljava/lang/String;)V
194+
public final fun setSampleRate (Ljava/lang/Double;)V
192195
public final fun setSdk (Lio/sentry/kotlin/multiplatform/protocol/SdkVersion;)V
193196
public final fun setSessionTrackingIntervalMillis (J)V
197+
public final fun setTracesSampleRate (Ljava/lang/Double;)V
194198
}
195199

196200
public final class io/sentry/kotlin/multiplatform/protocol/Breadcrumb {

sentry-kotlin-multiplatform/src/commonAppleMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryOptionsExtensions.apple.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.sentry.kotlin.multiplatform.SentryEvent
99
import io.sentry.kotlin.multiplatform.SentryOptions
1010
import io.sentry.kotlin.multiplatform.nsexception.dropKotlinCrashEvent
1111
import kotlinx.cinterop.convert
12+
import platform.Foundation.NSNumber
1213
import NSException.Sentry.SentryEvent as NSExceptionSentryEvent
1314

1415
internal fun SentryOptions.toCocoaOptionsConfiguration(): (CocoaSentryOptions?) -> Unit = {
@@ -32,6 +33,12 @@ internal fun CocoaSentryOptions.applyCocoaBaseOptions(options: SentryOptions) {
3233
enableAutoSessionTracking = options.enableAutoSessionTracking
3334
maxAttachmentSize = options.maxAttachmentSize.convert()
3435
maxBreadcrumbs = options.maxBreadcrumbs.convert()
36+
options.sampleRate?.let {
37+
sampleRate = NSNumber(double = it)
38+
}
39+
options.tracesSampleRate?.let {
40+
tracesSampleRate = NSNumber(double = it)
41+
}
3542
beforeSend = { event ->
3643
val cocoaName = BuildKonfig.SENTRY_COCOA_PACKAGE_NAME
3744
val cocoaVersion = BuildKonfig.SENTRY_COCOA_VERSION

sentry-kotlin-multiplatform/src/commonAppleTest/kotlin/io/sentry/kotlin/multiplatform/FoundationTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import kotlin.test.assertContentEquals
1111
import kotlin.test.assertEquals
1212

1313
class FoundationTest {
14-
1514
@Test
1615
fun `convert string to NSData and ByteArray is correct`() {
1716
val text = "$!()I!(DKDASKDKSD(#(ldkiadjk91jd"

sentry-kotlin-multiplatform/src/commonJvmMain/kotlin/io/sentry/kotlin/multiplatform/extensions/SentryOptionsExtensions.jvm.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ internal fun JvmSentryOptions.applyJvmBaseOptions(options: SentryOptions) {
4040
isEnableAutoSessionTracking = options.enableAutoSessionTracking
4141
maxAttachmentSize = options.maxAttachmentSize
4242
maxBreadcrumbs = options.maxBreadcrumbs
43+
sampleRate = options.sampleRate
44+
tracesSampleRate = options.tracesSampleRate
4345
setBeforeBreadcrumb { jvmBreadcrumb, _ ->
4446
if (options.beforeBreadcrumb == null) {
4547
jvmBreadcrumb

sentry-kotlin-multiplatform/src/commonMain/kotlin/io/sentry/kotlin/multiplatform/SentryOptions.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ private const val DEFAULT_SESSION_INTERVAL_MILLIS = 30000L
99

1010
/** Sentry options that can be used to configure the SDK. */
1111
public open class SentryOptions {
12-
1312
/**
1413
* The DSN tells the SDK where to send the events to. If this value is not provided, the SDK will
1514
* just not send any events.
@@ -107,4 +106,18 @@ public open class SentryOptions {
107106
* Available on Apple.
108107
*/
109108
public var failedRequestTargets: List<String> = listOf(".*")
109+
110+
/**
111+
* Configures the sample rate as a percentage of events to be sent in the range of 0.0 to 1.0. if
112+
* 1.0 is set it means that 100% of events are sent. If set to 0.1 only 10% of events will be
113+
* sent. Events are picked randomly. Default is null (disabled)
114+
*/
115+
public var sampleRate: Double? = null
116+
117+
/**
118+
* Configures the sample rate as a percentage of transactions to be sent in the range of 0.0 to
119+
* 1.0. if 1.0 is set it means that 100% of transactions are sent. If set to 0.1 only 10% of
120+
* transactions will be sent. Transactions are picked randomly. Default is null (disabled)
121+
*/
122+
public var tracesSampleRate: Double? = null
110123
}

sentry-kotlin-multiplatform/src/commonTest/kotlin/io/sentry/kotlin/multiplatform/SentryOptionsTest.kt

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,40 @@
11
package io.sentry.kotlin.multiplatform
22

33
import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
4+
import io.sentry.kotlin.multiplatform.utils.fakeDsn
45
import kotlin.test.Test
56
import kotlin.test.assertEquals
67

7-
class SentryOptionsTest {
8+
class SentryOptionsTest : BaseSentryTest() {
9+
@Test
10+
fun `GIVEN sample rate WHEN set in Sentry init THEN does not crash`() {
11+
// GIVEN
12+
val sampleRate = 0.5
13+
14+
// WHEN
15+
sentryInit {
16+
it.dsn = fakeDsn
17+
it.sampleRate = sampleRate
18+
}
19+
20+
// THEN
21+
// does not crash
22+
}
23+
24+
@Test
25+
fun `GIVEN traces sample rate WHEN set in Sentry init THEN does not crash`() {
26+
// GIVEN
27+
val traceSampleRate = 0.5
28+
29+
// WHEN
30+
sentryInit {
31+
it.dsn = fakeDsn
32+
it.tracesSampleRate = traceSampleRate
33+
}
34+
35+
// THEN
36+
// does not crash
37+
}
838

939
@Test
1040
fun `Breadcrumb can be modified via callback in init and should return the modified Breadcrumb`() {

0 commit comments

Comments
 (0)