Skip to content

Commit

Permalink
fix: add additional logging to detect network issues (#193)
Browse files Browse the repository at this point in the history
* fix: add additional logging to detect network issues

* fix: log on error level
  • Loading branch information
nicklasl authored Feb 18, 2025
1 parent aee8b09 commit 780feb1
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ object ConfidenceFactory {
.callTimeout(timeoutMillis, TimeUnit.MILLISECONDS)
.build(),
dispatcher = dispatcher,
sdkMetadata = SdkMetadata(SDK_ID, BuildConfig.SDK_VERSION)
sdkMetadata = SdkMetadata(SDK_ID, BuildConfig.SDK_VERSION),
debugLogger = debugLogger
)
val visitorId = ConfidenceValue.String(VisitorUtil.getId(context))
val initContext = initialContext.toMutableMap()
Expand Down
10 changes: 9 additions & 1 deletion Confidence/src/main/java/com/spotify/confidence/DebugLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal interface DebugLogger {
fun logFlag(action: String, details: String? = null)
fun logContext(action: String, context: Map<String, ConfidenceValue>)
fun logResolve(flag: String, context: JsonElement)
fun logError(message: String, throwable: Throwable? = null)
companion object {
const val TAG = "Confidence"
}
Expand Down Expand Up @@ -51,12 +52,19 @@ internal class DebugLoggerImpl(private val filterLevel: LoggingLevel, private va
)
}

override fun logError(message: String, throwable: Throwable?) {
error(message, throwable)
}

private fun verbose(message: String) = log(LoggingLevel.VERBOSE, message)
private fun debug(message: String) = log(LoggingLevel.DEBUG, message)
private fun warn(message: String, throwable: Throwable?) =
log(LoggingLevel.WARN, throwable?.let { "$message: ${throwable.message}" } ?: message)

private fun error(message: String, throwable: Throwable) = log(LoggingLevel.ERROR, "$message: ${throwable.message}")
private fun error(message: String, throwable: Throwable?) = log(
LoggingLevel.ERROR,
throwable?.let { "$message: ${throwable.message}" } ?: message
)

private fun log(messageLevel: LoggingLevel, message: String) {
if (messageLevel >= filterLevel) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ internal class EventSenderEngineImpl(
EventSenderEngineImpl(
EventStorageImpl(context),
clientSecret,
uploader = EventSenderUploaderImpl(OkHttpClient(), dispatcher),
uploader = EventSenderUploaderImpl(OkHttpClient(), dispatcher, debugLogger),
flushPolicies = flushPolicies.toMutableList(),
dispatcher = dispatcher,
sdkMetadata = sdkMetadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ internal data class EngineEvent(

internal class EventSenderUploaderImpl(
private val httpClient: OkHttpClient,
private val dispatcher: CoroutineDispatcher
private val dispatcher: CoroutineDispatcher,
private val debugLogger: DebugLogger? = null
) : EventSenderUploader {
private val headers by lazy {
Headers.headersOf(
Expand All @@ -65,6 +66,9 @@ internal class EventSenderUploaderImpl(
.build()

val response = httpClient.newCall(httpRequest).await()
if (!response.isSuccessful) {
debugLogger?.logError(message = "Failed to upload events. http code ${response.code}")
}
when (response.code) {
// clean up in case of success
200 -> true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ internal class RemoteFlagResolver(
private val httpClient: OkHttpClient,
private val sdkMetadata: SdkMetadata,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO,
private val baseUrl: HttpUrl? = null
private val baseUrl: HttpUrl? = null,
private val debugLogger: DebugLogger? = null
) : FlagResolver {
private val headers = Headers.headersOf(
"Content-Type",
Expand Down Expand Up @@ -68,6 +69,27 @@ internal class RemoteFlagResolver(
ConfidenceRegion.EUROPE -> "https://resolver.eu.confidence.dev"
ConfidenceRegion.USA -> "https://resolver.us.confidence.dev"
}

private fun Response.toResolveFlags(): ResolveResponse {
if (!isSuccessful) {
debugLogger?.logError("Failed to resolve flags. Http code: $code")
}
body?.let { body ->
val bodyString = body.string()

// building the json class responsible for serializing the object
val networkJson = Json {
serializersModule = SerializersModule {
ignoreUnknownKeys = true
}
}
try {
return ResolveResponse.Resolved(networkJson.decodeFromString(bodyString))
} finally {
body.close()
}
} ?: throw ConfidenceError.ParseError("Response body is null", listOf())
}
}

@Serializable
Expand All @@ -77,22 +99,4 @@ private data class ResolveFlagsRequest(
val clientSecret: String,
val apply: Boolean,
val sdk: Sdk
)

private fun Response.toResolveFlags(): ResolveResponse {
body?.let { body ->
val bodyString = body.string()

// building the json class responsible for serializing the object
val networkJson = Json {
serializersModule = SerializersModule {
ignoreUnknownKeys = true
}
}
try {
return ResolveResponse.Resolved(networkJson.decodeFromString(bodyString))
} finally {
body.close()
}
} ?: throw ConfidenceError.ParseError("Response body is null", listOf())
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ internal open class DebugLoggerFake : DebugLogger {
// not important enough to test right now
}

override fun logError(message: String, throwable: Throwable?) {
messagesLogged.add(Msg(message, true, throwable))
}

data class Msg(val message: String, val isWarning: Boolean, val throwable: Throwable?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ class DebugLoggerImplTest {

errorLogger.logMessage("Error msg", throwable = Error("my error"))
verify { Log.e("Confidence", "Error msg: my error") }

errorLogger.logError("Error msg")
verify { Log.e("Confidence", "Error msg") }
}

@Test
Expand Down

0 comments on commit 780feb1

Please sign in to comment.