Skip to content

Commit

Permalink
Merge branch 'trunk' into performance-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
wzieba authored May 7, 2024
2 parents 5c57d40 + 39a82f2 commit 5e56d75
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 9 deletions.
1 change: 0 additions & 1 deletion AutomatticTracks/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ android {
defaultConfig {
minSdkVersion rootProject.minSdkVersion
targetSdkVersion rootProject.targetSdkVersion
buildConfigField "String", "VERSION_NAME", "\"2.1.0\""
}

buildFeatures {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
public class TracksClient {
public static final String LOGTAG = "NosaraClient";

public static final String LIB_VERSION = BuildConfig.VERSION_NAME;
protected static final String DEFAULT_USER_AGENT = "Nosara Client for Android" + "/" + LIB_VERSION;
protected static final String DEFAULT_USER_AGENT = "Nosara Client for Android";
protected static final String NOSARA_REST_API_ENDPOINT_URL_V1_1 = "https://public-api.wordpress.com/rest/v1.1/";
protected static final int DEFAULT_EVENTS_QUEUE_THRESHOLD = 9;
protected static final int DEFAULT_EVENTS_QUEUE_MAX_SIZE = 10000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,17 @@ interface CrashLoggingDataProvider {
val user: Flow<CrashLoggingUser?>

/**
* Provides the {@link CrashLogging} with information about the current application state.
* Provides the [CrashLogging] with information about the current application state.
*/
val applicationContextProvider: Flow<Map<String, String>>

/**
* Provides [CrashLogging] with information about the sampling for **error tracking**.
* By default, sampling is disabled meaning all errors are reported.
*/
val errorSampling: ErrorSampling
get() = ErrorSampling.Disabled

/**
* Provides [CrashLogging] with information about exceptions that should be dropped if is the
* last one on stack trace
Expand Down Expand Up @@ -117,3 +124,19 @@ sealed class PerformanceMonitoringConfig {
}
}
}

sealed class ErrorSampling {
object Disabled : ErrorSampling()

data class Enabled(
/**
* Provides sample rate for error tracking. Indicates how often do we report errors.
* Has to be between 0 and 1.
*/
val sampleRate: Double
) : ErrorSampling() {
init {
assert(sampleRate in 0.0..1.0)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.automattic.android.tracks.crashlogging

data class CrashLoggingUser(
val userID: String,
val email: String,
val username: String
val userID: String? = null,
val email: String? = null,
val username: String? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.Application
import com.automattic.android.tracks.crashlogging.CrashLogging
import com.automattic.android.tracks.crashlogging.CrashLoggingDataProvider
import com.automattic.android.tracks.crashlogging.CrashLoggingUser
import com.automattic.android.tracks.crashlogging.ErrorSampling
import com.automattic.android.tracks.crashlogging.ExtraKnownKey
import com.automattic.android.tracks.crashlogging.JsException
import com.automattic.android.tracks.crashlogging.JsExceptionCallback
Expand Down Expand Up @@ -75,6 +76,10 @@ internal class SentryCrashLogging constructor(
appendExtra(event)
event
}
sampleRate = when (val errorsSampleRate = dataProvider.errorSampling) {
ErrorSampling.Disabled -> null
is ErrorSampling.Enabled -> errorsSampleRate.sampleRate
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class SentryCrashLoggingTest {
shouldDropException: (String, String, String) -> Boolean = dataProvider.shouldDropException,
extraKeys: List<String> = dataProvider.extraKeys,
provideExtrasForEvent: (Map<ExtraKnownKey, String>) -> Map<ExtraKnownKey, String> = dataProvider.provideExtrasForEvent,
applicationContext: Map<String, String> = dataProvider.fakeApplicationContextEmitter.value
applicationContext: Map<String, String> = dataProvider.fakeApplicationContextEmitter.value,
errorSampling: ErrorSampling = dataProvider.errorSampling
) {
dataProvider = FakeDataProvider(
locale = locale,
Expand All @@ -65,7 +66,8 @@ class SentryCrashLoggingTest {
shouldDropException = shouldDropException,
extraKeys = extraKeys,
provideExtrasForEvent = provideExtrasForEvent,
initialApplicationContext = applicationContext
initialApplicationContext = applicationContext,
errorSampling = errorSampling
)

crashLogging = SentryCrashLogging(
Expand Down Expand Up @@ -402,6 +404,23 @@ class SentryCrashLoggingTest {
}.assertAll()
}

@Test
fun `should provide null sampling rate if error sampling is disabled`() {
dataProvider.errorSampling = ErrorSampling.Disabled
prepareSut()

assertThat(capturedOptions.sampleRate).isNull()
}

@Test
fun `should provide expected sampling rate if error sampling is enabled`() {
val errorSamplingRate = 0.3
dataProvider.errorSampling = ErrorSampling.Enabled(errorSamplingRate)
prepareSut()

assertThat(capturedOptions.sampleRate).isEqualTo(errorSamplingRate)
}

private val crashLoggingMethods = listOf(
{ crashLogging.sendReport() } to "sendReport",
{ crashLogging.recordEvent("message") } to "recordEvent",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.automattic.android.tracks.fakes
import com.automattic.android.tracks.crashlogging.BuildConfig
import com.automattic.android.tracks.crashlogging.CrashLoggingDataProvider
import com.automattic.android.tracks.crashlogging.CrashLoggingUser
import com.automattic.android.tracks.crashlogging.ErrorSampling
import com.automattic.android.tracks.crashlogging.EventLevel
import com.automattic.android.tracks.crashlogging.ExtraKnownKey
import com.automattic.android.tracks.crashlogging.PerformanceMonitoringConfig
Expand All @@ -21,6 +22,7 @@ class FakeDataProvider(
var shouldDropException: (String, String, String) -> Boolean = { _: String, _: String, _: String -> false },
var extraKeys: List<String> = emptyList(),
var provideExtrasForEvent: (Map<ExtraKnownKey, String>) -> Map<ExtraKnownKey, String> = { currentExtras -> currentExtras },
override var errorSampling: ErrorSampling = ErrorSampling.Disabled,
initialUser: CrashLoggingUser? = testUser1,
initialApplicationContext: Map<String, String> = emptyMap()
) : CrashLoggingDataProvider {
Expand Down

0 comments on commit 5e56d75

Please sign in to comment.