Skip to content

Commit

Permalink
chore: more network logging (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
marandaneto authored Sep 12, 2024
1 parent b392f76 commit 725172f
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions posthog/src/main/java/com/posthog/internal/PostHogQueue.kt
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ internal class PostHogQueue(
os.use { theOutputStream ->
config.serializer.serialize(event, theOutputStream.writer().buffered())
}
config.logger.log("Queued event ${file.name}.")
config.logger.log("Queued Event ${event.event}: ${file.name}.")

flushIfOverThreshold()
} catch (e: Throwable) {
config.logger.log("Event ${event.event} failed to parse: $e.")
config.logger.log("Event ${event.event}: ${file.name} failed to parse: $e.")
}
}
}
Expand All @@ -103,7 +103,11 @@ internal class PostHogQueue(
}

private fun isAboveThreshold(flushAt: Int): Boolean {
return deque.size >= flushAt
if (deque.size >= flushAt) {
return true
}
config.logger.log("Cannot flush the Queue yet, below the threshold: $flushAt")
return false
}

private fun canFlushBatch(): Boolean {
Expand Down Expand Up @@ -185,10 +189,14 @@ internal class PostHogQueue(
var deleteFiles = true
try {
if (events.isNotEmpty()) {
config.logger.log("Flushing ${events.size} events.")

when (endpoint) {
PostHogApiEndpoint.BATCH -> api.batch(events)
PostHogApiEndpoint.SNAPSHOT -> api.snapshot(events)
}

config.logger.log("Flushed ${events.size} events successfully.")
}
} catch (e: PostHogApiError) {
deleteFiles = deleteFilesIfAPIError(e, config)
Expand All @@ -198,6 +206,9 @@ internal class PostHogQueue(
// no connection should try again
if (e.isNetworkingError()) {
deleteFiles = false
config.logger.log("Flushing failed because of a network error, let's try again soon.")
} else {
config.logger.log("Flushing failed: $e")
}
throw e
} finally {
Expand Down Expand Up @@ -326,6 +337,8 @@ internal fun deleteFilesIfAPIError(
config: PostHogConfig,
): Boolean {
if (e.statusCode < 400) {
config.logger.log("Flushing failed with ${e.statusCode}, let's try again soon.")

return false
}
// workaround due to png images exceed our max. limit in kafka
Expand All @@ -335,6 +348,8 @@ internal fun deleteFilesIfAPIError(
config.maxBatchSize = calcFloor(config.maxBatchSize)
config.flushAt = calcFloor(config.flushAt)

config.logger.log("Flushing failed with ${e.statusCode}, let's try again with a smaller batch.")

return false
}
return true
Expand Down

0 comments on commit 725172f

Please sign in to comment.