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

chore: more network logging #173

Merged
merged 2 commits into from
Sep 12, 2024
Merged
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
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
Loading