Skip to content
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions .changeset/quiet-queues-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"posthog": patch
"posthog-android": patch
---

Only attach `$sdk_debug_pending_queue_size` once the event queue reaches `flushAt`, so healthy queues no longer add it to every event
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
Expand Down Expand Up @@ -269,11 +270,7 @@ internal class PostHogAndroidEventSnapshotsTest {
event["uuid"] = "<uuid>"
val properties = event.map("properties")
normalizeSdkVersion(properties)
// Depends on how many earlier captures the async queueExecutor has drained by the time
// this event's properties are built on the calling thread -- not deterministic run to run.
if (properties.containsKey("\$sdk_debug_pending_queue_size")) {
properties["\$sdk_debug_pending_queue_size"] = "<pending-queue-size>"
}
assertFalse(properties.containsKey("\$sdk_debug_pending_queue_size"))
}

val featureFlagEvent = batch.single { (it as Map<*, *>)["event"] == "\$feature_flag_called" } as Map<*, *>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
"$sdk_debug_replay_event_trigger_status": "trigger_disabled",
"$sdk_debug_replay_linked_flag_trigger_status": "trigger_disabled",
"$sdk_debug_replay_internal_buffer_length": 0,
"$sdk_debug_pending_queue_size": "<pending-queue-size>",
"$set": {
"plan": "pro"
},
Expand Down Expand Up @@ -99,7 +98,6 @@
"$sdk_debug_replay_capture_mode": "screenshot",
"$sdk_debug_replay_internal_buffer_length": 0,
"$sdk_debug_session_start": 1700000000123,
"$sdk_debug_pending_queue_size": "<pending-queue-size>",
"$set": {
"email": "person@example.com"
},
Expand Down Expand Up @@ -144,7 +142,6 @@
"$sdk_debug_replay_event_trigger_status": "trigger_disabled",
"$sdk_debug_replay_linked_flag_trigger_status": "trigger_disabled",
"$sdk_debug_replay_internal_buffer_length": 0,
"$sdk_debug_pending_queue_size": "<pending-queue-size>",
"$group_type": "company",
"$group_key": "posthog",
"$group_set": {
Expand Down Expand Up @@ -189,7 +186,6 @@
"$sdk_debug_replay_event_trigger_status": "trigger_disabled",
"$sdk_debug_replay_linked_flag_trigger_status": "trigger_disabled",
"$sdk_debug_replay_internal_buffer_length": 0,
"$sdk_debug_pending_queue_size": "<pending-queue-size>",
"$groups": {
"company": "posthog"
},
Expand Down Expand Up @@ -237,7 +233,6 @@
"$sdk_debug_replay_event_trigger_status": "trigger_disabled",
"$sdk_debug_replay_linked_flag_trigger_status": "trigger_disabled",
"$sdk_debug_replay_internal_buffer_length": 0,
"$sdk_debug_pending_queue_size": "<pending-queue-size>",
"$groups": {
"company": "posthog"
},
Expand Down
2 changes: 1 addition & 1 deletion posthog/src/main/java/com/posthog/PostHog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ public class PostHog private constructor(
if (claimed) {
sessionStart(sessionId, managerStart)?.let { props["\$sdk_debug_session_start"] = it }
}
queue?.size?.let { props["\$sdk_debug_pending_queue_size"] = it }
queue?.backedUpSize?.let { props["\$sdk_debug_pending_queue_size"] = it }
} catch (e: Throwable) {
props["\$sdk_debug_error_capturing_properties"] = e.toString().take(MAX_DEBUG_ERROR_LENGTH)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ public class PostHogQueue<Record>(
override val size: Int
get() = synchronized(dequeLock) { deque.size }

override val backedUpSize: Int?
get() = size.takeIf { it > 0 && it >= batchLimits.flushAt }
Comment thread
marandaneto marked this conversation as resolved.
Resolved

public val queueDirectory: File?
get() = spec.storagePrefix?.let { File(it, config.projectToken) }

Expand Down Expand Up @@ -771,7 +774,8 @@ public class PostHogQueue<Record>(

internal class BatchLimits(
var cap: Int,
var flushAt: Int,
// Halved on the queue executor and read on capture threads by backedUpSize.
@Volatile var flushAt: Int,
) {
fun halve(actualBatchSize: Int) {
cap =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import com.posthog.PostHogInternal
public interface PostHogQueueInterface<Record> {
public val size: Int

/**
* The size once the queue has reached its flush threshold, else null: below it the queue is
* ordinary batching, not a backlog worth reporting.
*/
public val backedUpSize: Int?
get() = null

public fun add(record: Record)

public fun flush()
Expand Down
48 changes: 42 additions & 6 deletions posthog/src/test/java/com/posthog/PostHogTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1062,7 +1062,7 @@ internal class PostHogTest {
assertTrue(theEvent.properties!!.containsKey("\$active_feature_flags"))
assertEquals("disabled", theEvent.properties!!["\$recording_status"])
assertFalse(theEvent.properties!!.containsKey("\$sdk_debug_session_start"))
assertNotNull(theEvent.properties!!["\$sdk_debug_pending_queue_size"])
assertFalse(theEvent.properties!!.containsKey("\$sdk_debug_pending_queue_size"))

sut.close()
}
Expand Down Expand Up @@ -4122,7 +4122,7 @@ internal class PostHogTest {
assertEquals("trigger_pending", props["\$sdk_debug_replay_event_trigger_status"])
assertEquals("trigger_disabled", props["\$sdk_debug_replay_linked_flag_trigger_status"])
assertEquals(1, (props["\$sdk_debug_replay_internal_buffer_length"] as Number).toInt())
assertNotNull(props["\$sdk_debug_pending_queue_size"])
assertFalse(props.containsKey("\$sdk_debug_pending_queue_size"))
assertFalse(props.containsKey("\$sdk_debug_replay_capture_mode"))
assertFalse(props.containsKey("\$sdk_debug_replay_flush_hold_reason"))
assertFalse(props.containsKey("\$sdk_debug_replay_pending_trigger_conditions"))
Expand Down Expand Up @@ -4158,7 +4158,7 @@ internal class PostHogTest {
assertEquals("disabled", theEvent.properties!!["\$recording_status"])
assertEquals("wireframe", theEvent.properties!!["\$sdk_debug_replay_capture_mode"])
assertNotNull(theEvent.properties!!["\$sdk_debug_session_start"])
assertNotNull(theEvent.properties!!["\$sdk_debug_pending_queue_size"])
assertFalse(theEvent.properties!!.containsKey("\$sdk_debug_pending_queue_size"))
assertFalse(theEvent.properties!!.containsKey("\$sdk_debug_replay_throttle_delay_ms"))
assertFalse(theEvent.properties!!.containsKey("\$sdk_debug_current_session_duration"))

Expand Down Expand Up @@ -4297,7 +4297,7 @@ internal class PostHogTest {
assertFalse(theEvent.properties!!.containsKey("\$recording_status"))
// The session and queue keys do not come from the replay handler, so they survive its throw.
assertNotNull(theEvent.properties!!["\$sdk_debug_session_start"])
assertNotNull(theEvent.properties!!["\$sdk_debug_pending_queue_size"])
assertFalse(theEvent.properties!!.containsKey("\$sdk_debug_pending_queue_size"))

sut.close()
}
Expand Down Expand Up @@ -4338,6 +4338,42 @@ internal class PostHogTest {
beforeSend = beforeSend,
)

@Test
fun `pending queue size is attached only once the queue reaches flushAt`() {
val http = mockHttp()
val sut = getSut(http.url("/").toString(), flushAt = 3, preloadFeatureFlags = false, reloadFeatureFlags = false)

repeat(3) { sut.capture(EVENT) }

val events = capturedEvents(http)
assertEquals(listOf(false, false, false), events.map { it.properties!!.containsKey("\$sdk_debug_pending_queue_size") })

sut.close()
}

@Test
fun `a backed-up queue reports its size on the next event`() {
val http = mockHttp()
val sut = getSut(http.url("/").toString(), flushAt = 3, preloadFeatureFlags = false, reloadFeatureFlags = false)
var connected = false
config.networkStatus =
object : PostHogNetworkStatus {
override fun isConnected(): Boolean = connected
}

repeat(3) { sut.capture(EVENT) }
queueExecutor.awaitExecution()
assertEquals(0, http.requestCount)
sut.capture(EVENT)

connected = true
sut.flush()
val events = capturedEvents(http)
assertEquals(listOf(null, null, null, 3), events.map { it.properties!!["\$sdk_debug_pending_queue_size"] })

sut.close()
}

@Test
fun `replay debug bundle is attached at most once every 30 seconds while required keys stay on every event`() {
val http = mockHttp()
Expand All @@ -4358,7 +4394,7 @@ internal class PostHogTest {
assertEquals("trigger_pending", it.properties!!["\$sdk_debug_replay_event_trigger_status"])
assertEquals("trigger_disabled", it.properties!!["\$sdk_debug_replay_linked_flag_trigger_status"])
assertNotNull(it.properties!!["\$sdk_debug_replay_internal_buffer_length"])
assertNotNull(it.properties!!["\$sdk_debug_pending_queue_size"])
assertFalse(it.properties!!.containsKey("\$sdk_debug_pending_queue_size"))
}

sut.close()
Expand Down Expand Up @@ -4416,7 +4452,7 @@ internal class PostHogTest {
assertEquals(listOf(false, false, true), events.map { it.carriesReplayDebugBundle })
events.forEach {
assertEquals("buffering", it.properties!!["\$recording_status"])
assertNotNull(it.properties!!["\$sdk_debug_pending_queue_size"])
assertFalse(it.properties!!.containsKey("\$sdk_debug_pending_queue_size"))
}

sut.close()
Expand Down
25 changes: 25 additions & 0 deletions posthog/src/test/java/com/posthog/internal/PostHogQueueTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import kotlin.test.AfterTest
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertNull
import kotlin.test.assertTrue

internal class PostHogQueueTest {
Expand Down Expand Up @@ -236,6 +237,30 @@ internal class PostHogQueueTest {
assertEquals(0, http.requestCount)
}

@Test
fun `reports a backed-up size only once the queue reaches flushAt`() {
val http = mockHttp()
val sut =
getSut(
host = http.url("/").toString(),
flushAt = 2,
networkStatus =
object : PostHogNetworkStatus {
override fun isConnected() = false
},
)

assertNull(sut.backedUpSize)

sut.add(generateEvent())
awaitQueue()
assertNull(sut.backedUpSize)

sut.add(generateEvent())
awaitQueue()
assertEquals(2, sut.backedUpSize)
}

@Test
fun `does not flush if not connected but try to flush again`() {
val http = mockHttp()
Expand Down
Loading