Skip to content

Commit

Permalink
add failed event count (#241)
Browse files Browse the repository at this point in the history
  • Loading branch information
xinlili-statsig authored Mar 13, 2024
1 parent be0f376 commit 6c0f222
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/main/kotlin/com/statsig/sdk/ErrorBoundary.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ internal class ErrorBoundary(private val apiKey: String, private val options: St
}
}

internal fun logException(tag: String, ex: Throwable, configName: String? = null, extraInfo: String? = null) {
internal fun logException(tag: String, ex: Throwable, configName: String? = null, extraInfo: String? = null, bypassDedupe: Boolean = false) {
try {
if (options.localMode || options.disableAllLogging || seen.contains(ex.javaClass.name)) {
if (options.localMode || options.disableAllLogging) {
return
}
if (!bypassDedupe && seen.contains(ex.javaClass.name)) {
return
}

seen.add(ex.javaClass.name)

val info = ex.stackTraceToString()
Expand All @@ -80,7 +82,7 @@ internal class ErrorBoundary(private val apiKey: String, private val options: St
"statsigMetadata": ${statsigMetadata.asJson()},
"configName": "$configName",
"setupOptions": $optionsCopy,
"extraInfo": $extraInfo
"extra": $extraInfo
}
""".trimIndent()
val req =
Expand Down
20 changes: 20 additions & 0 deletions src/main/kotlin/com/statsig/sdk/StatsigNetwork.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ private const val MS_IN_S: Long = 1000
const val STATSIG_API_URL_BASE: String = "https://statsigapi.net/v1"
private const val STATSIG_CDN_URL_BASE: String = "https://api.statsigcdn.com/v1"
const val LOG_EVENT_RETRY_COUNT = 5
const val LOG_EVENT_FAILURE_TAG = "statsig::log_event_failed"

internal class StatsigNetwork(
private val sdkKey: String,
private val options: StatsigOptions,
Expand Down Expand Up @@ -257,6 +259,7 @@ internal class StatsigNetwork(
return@coroutineScope
} else if (!retryCodes.contains(response.code) || currRetry == 0) {
options.customLogger.warning("[Statsig]: Network request failed with status code: ${response.code}")
logPostLogFailure(eventsCount)
return@coroutineScope
} else if (retryCodes.contains(response.code) && currRetry > 0) {
options.customLogger.info("[Statsig]: Retrying network request. Retry count: $currRetry. Response code: ${response.code}")
Expand All @@ -267,6 +270,10 @@ internal class StatsigNetwork(
if (e is JsonParseException) {
errorBoundary.logException("retryPostLogs", e)
}
if (currRetry == 0) {
logPostLogFailure(eventsCount)
return@coroutineScope
}
}

val count = retries - --currRetry
Expand All @@ -278,4 +285,17 @@ internal class StatsigNetwork(
fun shutdown() {
statsigHttpClient.dispatcher.executorService.shutdown()
}

private fun logPostLogFailure(eventsCount: String) {
errorBoundary.logException(
LOG_EVENT_FAILURE_TAG,
Exception("Drop log event"),
null,
extraInfo = """{
"eventCount": $eventsCount
}
""".trimIndent(),
bypassDedupe = true,
)
}
}
4 changes: 4 additions & 0 deletions src/main/kotlin/com/statsig/sdk/StatsigServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,11 @@ sealed class StatsigServer {
): CompletableFuture<Layer>

@JvmSynthetic abstract fun overrideLayerAsync(layerName: String, value: Map<String, Any>): CompletableFuture<Unit>

@JvmSynthetic abstract fun removeLayerOverrideAsync(layerName: String): CompletableFuture<Unit>

@JvmSynthetic abstract fun removeConfigOverrideAsync(configName: String): CompletableFuture<Unit>

@JvmSynthetic abstract fun removeGateOverrideAsync(gateName: String): CompletableFuture<Unit>

abstract fun manuallyLogLayerParameterExposureAsync(user: StatsigUser, layerName: String, paramName: String): CompletableFuture<Void>
Expand All @@ -216,6 +219,7 @@ sealed class StatsigServer {

@JvmSynthetic
internal abstract suspend fun flush()

@JvmSynthetic internal abstract fun getCustomLogger(): LoggerInterface

companion object {
Expand Down

0 comments on commit 6c0f222

Please sign in to comment.