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

Add debug tracing to scheduling service #1735

Merged
merged 1 commit into from
Nov 29, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ internal class DeliveryModuleImpl(
workerThreadModule.backgroundWorker(Worker.Background.IoRegWorker),
workerThreadModule.backgroundWorker(Worker.Background.HttpRequestWorker),
initModule.clock,
initModule.logger
initModule.logger,
deliveryTracer
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,16 @@
* The payload store returned a list of payloads by priority
*/
internal class GetPayloadsByPriority(val payloads: List<StoredTelemetryMetadata>) : DeliveryTraceState() {
override fun toString(): String = "GetPayloadsByPriority, count=${payloads.size},\n${payloads.reportListString()}"
override fun toString(): String =
"GetPayloadsByPriority, count=${payloads.size},\n${payloads.reportListString()}"

Check warning on line 54 in embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt#L54

Added line #L54 was not covered by tests
}

/**
* The payload store returned a list of undelivered payloads
*/
internal class GetUndeliveredPayloads(val payloads: List<StoredTelemetryMetadata>) : DeliveryTraceState() {
override fun toString(): String = "GetUndeliveredPayloads, count=${payloads.size},\n${payloads.reportListString()}"
override fun toString(): String =
"GetUndeliveredPayloads, count=${payloads.size},\n${payloads.reportListString()}"

Check warning on line 62 in embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt#L62

Added line #L62 was not covered by tests
}

/**
Expand Down Expand Up @@ -92,6 +94,43 @@
override fun toString(): String = "SessionCacheAttempt"
}

/**
* The delivery loop was started
*/
internal class StartDeliveryLoop(private val loopAlreadyActive: Boolean) : DeliveryTraceState() {
override fun toString(): String = "StartDeliveryLoop loopAlreadyActive=$loopAlreadyActive"

Check warning on line 101 in embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt#L101

Added line #L101 was not covered by tests
}

/**
* A payload was enqueued for delivery
*/
class PayloadEnqueued(private val payload: StoredTelemetryMetadata) : DeliveryTraceState() {
override fun toString(): String = "PayloadEnqueued, ${payload.toReportString()}"

Check warning on line 108 in embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt#L108

Added line #L108 was not covered by tests
}

/**
* A payload result was obtained
*/
class PayloadResult(
private val payload: StoredTelemetryMetadata,
private val result: ExecutionResult,
) : DeliveryTraceState() {
override fun toString(): String = "PayloadResult, ${payload.toReportString()}, result=${result.javaClass.simpleName}"

Check warning on line 118 in embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt#L118

Added line #L118 was not covered by tests
}

/**
* A payload queue was created
*/
class PayloadQueueCreated(
private val payloadsByPriority: List<StoredTelemetryMetadata>,
private val payloadsToSend: List<StoredTelemetryMetadata>,
) : DeliveryTraceState() {
override fun toString(): String {
return "PayloadQueueCreated, payloadsByPriority=${payloadsByPriority.map { it.uuid }}, " +
"payloadsToSend=${payloadsToSend.map { it.uuid }}"

Check warning on line 130 in embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt

View check run for this annotation

Codecov / codecov/patch

embrace-android-delivery/src/main/kotlin/io/embrace/android/embracesdk/internal/delivery/debug/DeliveryTraceState.kt#L129-L130

Added lines #L129 - L130 were not covered by tests
}
}

internal fun StoredTelemetryMetadata.toReportString(): String {
return "$timestamp, $uuid, $payloadType, complete=$complete"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,23 @@ class DeliveryTracer {
"#$k, $state"
}.joinToString("\n")
}

fun onStartDeliveryLoop(sendLoopActive: Boolean) {
events.add(DeliveryTraceState.StartDeliveryLoop(sendLoopActive))
}

fun onPayloadQueueCreated(
payloadsByPriority: List<StoredTelemetryMetadata>,
payloadsToSend: List<StoredTelemetryMetadata>,
) {
events.add(DeliveryTraceState.PayloadQueueCreated(payloadsByPriority, payloadsToSend))
}

fun onPayloadEnqueued(payload: StoredTelemetryMetadata) {
events.add(DeliveryTraceState.PayloadEnqueued(payload))
}

fun onPayloadResult(payload: StoredTelemetryMetadata, result: ExecutionResult) {
events.add(DeliveryTraceState.PayloadResult(payload, result))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ class IntakeServiceImpl(
val lastReference = cacheReferences[metadata.envelopeType]

if (metadata.complete) {
schedulingService.onPayloadIntake()
deliveryTracer?.onPayloadIntake(metadata)
schedulingService.onPayloadIntake()
} else {
cacheReferences[metadata.envelopeType] = metadata
if (!cacheableEnvelopeTypes.contains(metadata.envelopeType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.embrace.android.embracesdk.internal.clock.Clock
import io.embrace.android.embracesdk.internal.comms.api.Endpoint
import io.embrace.android.embracesdk.internal.comms.delivery.NetworkStatus
import io.embrace.android.embracesdk.internal.delivery.StoredTelemetryMetadata
import io.embrace.android.embracesdk.internal.delivery.debug.DeliveryTracer
import io.embrace.android.embracesdk.internal.delivery.execution.ExecutionResult
import io.embrace.android.embracesdk.internal.delivery.execution.RequestExecutionService
import io.embrace.android.embracesdk.internal.delivery.storage.PayloadStorageService
Expand All @@ -25,7 +26,8 @@ class SchedulingServiceImpl(
private val schedulingWorker: BackgroundWorker,
private val deliveryWorker: BackgroundWorker,
private val clock: Clock,
private val logger: EmbLogger
private val logger: EmbLogger,
private val deliveryTracer: DeliveryTracer? = null,
) : SchedulingService {

private val blockedEndpoints: MutableMap<Endpoint, Long> = ConcurrentHashMap()
Expand Down Expand Up @@ -65,9 +67,12 @@ class SchedulingServiceImpl(
// When a payload arrives, check to see if there's already an active job try to deliver payloads
// If not, schedule job. If so, do nothing.
if (sendLoopActive.compareAndSet(false, true)) {
deliveryTracer?.onStartDeliveryLoop(true)
schedulingWorker.submit {
deliveryLoop()
}
} else {
deliveryTracer?.onStartDeliveryLoop(false)
}
}

Expand Down Expand Up @@ -120,13 +125,20 @@ class SchedulingServiceImpl(
}
}

private fun createPayloadQueue(exclude: Set<StoredTelemetryMetadata> = emptySet()) = LinkedList(
storageService.getPayloadsByPriority()
private fun createPayloadQueue(exclude: Set<StoredTelemetryMetadata> = emptySet()): LinkedList<StoredTelemetryMetadata> {
val payloadsByPriority = storageService.getPayloadsByPriority()
val payloadsToSend = payloadsByPriority
.filter { it.shouldSendPayload() && !exclude.contains(it) }
.sortedWith(storedTelemetryComparator)
)
deliveryTracer?.onPayloadQueueCreated(
payloadsByPriority,
payloadsToSend,
)
return LinkedList(payloadsToSend)
}

private fun queueDelivery(payload: StoredTelemetryMetadata): Future<ExecutionResult> {
deliveryTracer?.onPayloadEnqueued(payload)
activeSends.add(payload)
return deliveryWorker.submit<ExecutionResult> {
val result: ExecutionResult =
Expand All @@ -149,6 +161,8 @@ class SchedulingServiceImpl(
ExecutionResult.Incomplete(exception = t, retry = false)
}

deliveryTracer?.onPayloadResult(payload, result)

with(result) {
if (!shouldRetry) {
// If the response is such that we should not ever retry the delivery of this payload,
Expand Down