From 9be9fa37b1397946e3d8641248744abb87766049 Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 12 Sep 2024 08:53:29 +0200 Subject: [PATCH 1/2] chore: more network logging --- .../java/com/posthog/internal/PostHogQueue.kt | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt b/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt index 0a02b94e..147343c6 100644 --- a/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt +++ b/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt @@ -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.") } } } @@ -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 { @@ -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) @@ -198,6 +206,9 @@ internal class PostHogQueue( // no connection should try again if (e.isNetworkingError()) { deleteFiles = false + config.logger.log("Flush failed because of a network error, let's try again soon.") + } else { + config.logger.log("Flush failed: $e") } throw e } finally { @@ -326,6 +337,8 @@ internal fun deleteFilesIfAPIError( config: PostHogConfig, ): Boolean { if (e.statusCode < 400) { + config.logger.log("Flush failed with ${e.statusCode}, let's try again soon.") + return false } // workaround due to png images exceed our max. limit in kafka @@ -335,6 +348,8 @@ internal fun deleteFilesIfAPIError( config.maxBatchSize = calcFloor(config.maxBatchSize) config.flushAt = calcFloor(config.flushAt) + config.logger.log("Flush failed with ${e.statusCode}, let's try again with a smaller batch.") + return false } return true From af06b75f27db9a198c4b81efa91ed6fbfed4d09c Mon Sep 17 00:00:00 2001 From: Manoel Aranda Neto Date: Thu, 12 Sep 2024 08:56:18 +0200 Subject: [PATCH 2/2] fix --- .../src/main/java/com/posthog/internal/PostHogQueue.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt b/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt index 147343c6..71f8ccce 100644 --- a/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt +++ b/posthog/src/main/java/com/posthog/internal/PostHogQueue.kt @@ -206,9 +206,9 @@ internal class PostHogQueue( // no connection should try again if (e.isNetworkingError()) { deleteFiles = false - config.logger.log("Flush failed because of a network error, let's try again soon.") + config.logger.log("Flushing failed because of a network error, let's try again soon.") } else { - config.logger.log("Flush failed: $e") + config.logger.log("Flushing failed: $e") } throw e } finally { @@ -337,7 +337,7 @@ internal fun deleteFilesIfAPIError( config: PostHogConfig, ): Boolean { if (e.statusCode < 400) { - config.logger.log("Flush failed with ${e.statusCode}, let's try again soon.") + config.logger.log("Flushing failed with ${e.statusCode}, let's try again soon.") return false } @@ -348,7 +348,7 @@ internal fun deleteFilesIfAPIError( config.maxBatchSize = calcFloor(config.maxBatchSize) config.flushAt = calcFloor(config.flushAt) - config.logger.log("Flush failed with ${e.statusCode}, let's try again with a smaller batch.") + config.logger.log("Flushing failed with ${e.statusCode}, let's try again with a smaller batch.") return false }