Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sample rate configuration #144

Merged
merged 8 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

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

### Dependencies
Expand Down
3 changes: 3 additions & 0 deletions config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@ complexity:
"**/JvmScopeProvider.kt",
"**/Breadcrumb.kt",
]
LongMethod:
excludes:
- "**/SentryOptionsExtensions.*"

Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,10 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
public final fun getMaxAttachmentSize ()J
public final fun getMaxBreadcrumbs ()I
public final fun getRelease ()Ljava/lang/String;
public final fun getSampleRate ()Ljava/lang/Double;
public final fun getSdk ()Lio/sentry/kotlin/multiplatform/protocol/SdkVersion;
public final fun getSessionTrackingIntervalMillis ()J
public final fun getTracesSampleRate ()Ljava/lang/Double;
public final fun setAttachScreenshot (Z)V
public final fun setAttachStackTrace (Z)V
public final fun setAttachThreads (Z)V
Expand All @@ -192,8 +194,10 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
public final fun setMaxAttachmentSize (J)V
public final fun setMaxBreadcrumbs (I)V
public final fun setRelease (Ljava/lang/String;)V
public final fun setSampleRate (Ljava/lang/Double;)V
public final fun setSdk (Lio/sentry/kotlin/multiplatform/protocol/SdkVersion;)V
public final fun setSessionTrackingIntervalMillis (J)V
public final fun setTracesSampleRate (Ljava/lang/Double;)V
}

public final class io/sentry/kotlin/multiplatform/protocol/Breadcrumb {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,10 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
public final fun getMaxAttachmentSize ()J
public final fun getMaxBreadcrumbs ()I
public final fun getRelease ()Ljava/lang/String;
public final fun getSampleRate ()Ljava/lang/Double;
public final fun getSdk ()Lio/sentry/kotlin/multiplatform/protocol/SdkVersion;
public final fun getSessionTrackingIntervalMillis ()J
public final fun getTracesSampleRate ()Ljava/lang/Double;
public final fun setAttachScreenshot (Z)V
public final fun setAttachStackTrace (Z)V
public final fun setAttachThreads (Z)V
Expand All @@ -189,8 +191,10 @@ public class io/sentry/kotlin/multiplatform/SentryOptions {
public final fun setMaxAttachmentSize (J)V
public final fun setMaxBreadcrumbs (I)V
public final fun setRelease (Ljava/lang/String;)V
public final fun setSampleRate (Ljava/lang/Double;)V
public final fun setSdk (Lio/sentry/kotlin/multiplatform/protocol/SdkVersion;)V
public final fun setSessionTrackingIntervalMillis (J)V
public final fun setTracesSampleRate (Ljava/lang/Double;)V
}

public final class io/sentry/kotlin/multiplatform/protocol/Breadcrumb {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import io.sentry.kotlin.multiplatform.SentryEvent
import io.sentry.kotlin.multiplatform.SentryOptions
import io.sentry.kotlin.multiplatform.nsexception.dropKotlinCrashEvent
import kotlinx.cinterop.convert
import platform.Foundation.NSNumber
import NSException.Sentry.SentryEvent as NSExceptionSentryEvent

internal fun SentryOptions.toCocoaOptionsConfiguration(): (CocoaSentryOptions?) -> Unit = {
Expand All @@ -32,6 +33,12 @@ internal fun CocoaSentryOptions.applyCocoaBaseOptions(options: SentryOptions) {
enableAutoSessionTracking = options.enableAutoSessionTracking
maxAttachmentSize = options.maxAttachmentSize.convert()
maxBreadcrumbs = options.maxBreadcrumbs.convert()
options.sampleRate?.let {
sampleRate = NSNumber(double = it)
}
options.tracesSampleRate?.let {
tracesSampleRate = NSNumber(double = it)
}
beforeSend = { event ->
val cocoaName = BuildKonfig.SENTRY_COCOA_PACKAGE_NAME
val cocoaVersion = BuildKonfig.SENTRY_COCOA_VERSION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlin.test.assertContentEquals
import kotlin.test.assertEquals

class FoundationTest {

@Test
fun `convert string to NSData and ByteArray is correct`() {
val text = "$!()I!(DKDASKDKSD(#(ldkiadjk91jd"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ internal fun JvmSentryOptions.applyJvmBaseOptions(options: SentryOptions) {
isEnableAutoSessionTracking = options.enableAutoSessionTracking
maxAttachmentSize = options.maxAttachmentSize
maxBreadcrumbs = options.maxBreadcrumbs
sampleRate = options.sampleRate
tracesSampleRate = options.tracesSampleRate
setBeforeBreadcrumb { jvmBreadcrumb, _ ->
if (options.beforeBreadcrumb == null) {
jvmBreadcrumb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ private const val DEFAULT_SESSION_INTERVAL_MILLIS = 30000L

/** Sentry options that can be used to configure the SDK. */
public open class SentryOptions {

/**
* The DSN tells the SDK where to send the events to. If this value is not provided, the SDK will
* just not send any events.
Expand Down Expand Up @@ -107,4 +106,18 @@ public open class SentryOptions {
* Available on Apple.
*/
public var failedRequestTargets: List<String> = listOf(".*")

/**
* Configures the sample rate as a percentage of events to be sent in the range of 0.0 to 1.0. if
* 1.0 is set it means that 100% of events are sent. If set to 0.1 only 10% of events will be
* sent. Events are picked randomly. Default is null (disabled)
*/
public var sampleRate: Double? = null

/**
* Configures the sample rate as a percentage of transactions to be sent in the range of 0.0 to
* 1.0. if 1.0 is set it means that 100% of transactions are sent. If set to 0.1 only 10% of
* transactions will be sent. Transactions are picked randomly. Default is null (disabled)
*/
public var tracesSampleRate: Double? = null
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,40 @@
package io.sentry.kotlin.multiplatform

import io.sentry.kotlin.multiplatform.protocol.Breadcrumb
import io.sentry.kotlin.multiplatform.utils.fakeDsn
import kotlin.test.Test
import kotlin.test.assertEquals

class SentryOptionsTest {
class SentryOptionsTest : BaseSentryTest() {
@Test
fun `GIVEN sample rate WHEN set in Sentry init THEN does not crash`() {
// GIVEN
val sampleRate = 0.5

// WHEN
sentryInit {
it.dsn = fakeDsn
it.sampleRate = sampleRate
}

// THEN
// does not crash
}

@Test
fun `GIVEN traces sample rate WHEN set in Sentry init THEN does not crash`() {
// GIVEN
val traceSampleRate = 0.5

// WHEN
sentryInit {
it.dsn = fakeDsn
it.tracesSampleRate = traceSampleRate
}

// THEN
// does not crash
}

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