Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/commit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on: [ push, workflow_dispatch ]
jobs:
semantic_library_workflow:
name: push
uses: krogerco/Shared-CI-Workflow-Android/.github/workflows/semantic_library_workflow.yml@v1.5.0
uses: krogerco/Shared-CI-Workflow-Android/.github/workflows/semantic_library_workflow.yml@v1
with:
java_version: '24'
ktlint_version: '-1'
Expand Down
3 changes: 1 addition & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

ktlint "**/src/**/*.kt" "!**/generated/**" --android --reporter=plain?group_by_file

./gradlew lintKotlin
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ kgpDagger = "2.56.1"
kgpAndroidDesugarJdkLibs = "2.1.5"
kgpAndroidxComposeBom = "2025.05.00"
kgpDokka = "2.0.0"
kgpJdk = "24"
kgpJdk = "17"
kgpJvmTarget = "11"
kgpJunit4 = "4.13.2"
kgpJunitBom = "5.12.2"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"husky": "^7.0.1"
},
"scripts": {
"prepare": "husky install"
"prepare": "husky"
}
}
55 changes: 49 additions & 6 deletions telemetry/src/main/java/com/kroger/telemetry/StandardTelemeter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ package com.kroger.telemetry
import com.kroger.telemetry.facet.Facet
import com.kroger.telemetry.facet.FacetResolver
import com.kroger.telemetry.facet.Failure
import com.kroger.telemetry.facet.RelayFailure
import com.kroger.telemetry.facet.ThreadData
import com.kroger.telemetry.facet.UnresolvedFacet
import kotlinx.coroutines.CoroutineScope
Expand Down Expand Up @@ -55,25 +56,67 @@ internal class StandardTelemeter(
relays.forEach { relay ->
events
.onEach { event ->
// Skip processing if this Relay is the cause of any RelayFailure event being
// processed. This prevents infinite loops when Relays fail while processing
// their own or each other's failures.
val shouldSkip =
event.facets
.filterIsInstance<RelayFailure>()
.any { relayFailure ->
relayIsSourceOfFailure(relayFailure, relay)
}

if (shouldSkip) {
return@onEach
}

Result
.runCatching {
relay.process(event)
}.onFailure {
val message = "An error was caught during Relay processing. It was $it"
val facet = Failure(message = message, throwable = it)
// Avoid loops
if (event.facets.contains(facet)) return@onEach
}.onFailure { throwable ->
val message =
"An error was caught during Relay processing. It was $throwable"

// If we were already processing a RelayFailure, note it as the cause.
val causeRelayFailure =
event.facets.filterIsInstance<RelayFailure>().firstOrNull()

val relayFailureFacet =
RelayFailure(
sourceRelay = relay,
message = message,
throwable = throwable,
cause = causeRelayFailure,
)

val failureFacet = Failure(message = message, throwable = throwable)

val failureEvent =
object : Event {
override val description: String = message
override val facets: List<Facet> = listOf(facet)
override val facets: List<Facet> =
listOf(relayFailureFacet, failureFacet)
}
record(failureEvent)
}
}.launchIn(coroutineScope)
}
}

/**
* Checks if a relay appears anywhere in the cause chain of a [RelayFailure]
*/
private fun relayIsSourceOfFailure(
relayFailure: RelayFailure,
relay: Relay,
): Boolean =
(relayFailure.sourceRelay === relay) ||
(
relayFailure.cause?.let {
relayIsSourceOfFailure(it, relay)
} ?: false
)

override fun record(
event: Event,
withFacets: List<Facet>?,
Expand Down
54 changes: 54 additions & 0 deletions telemetry/src/main/java/com/kroger/telemetry/facet/RelayFailure.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* MIT License
*
* Copyright (c) 2021 The Kroger Co. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.kroger.telemetry.facet

import com.kroger.telemetry.Relay

/**
* A facet representing a failure during a Relay's processing of events.
*
* A RelayFailure contains a reference to the relay that failed, allowing the Telemeter to prevent
* infinite loops by preventing that specific relay from processing its own failure events. A
* RelayFailure may include another RelayFailure as a [cause]
*
* The [cause] property enables progressive relay disqualification: when a relay fails
* while processing a RelayFailure, the new RelayFailure references the original as its cause.
* This creates a cause chain that prevents infinite loops when multiple relays fail
* while processing each other's failures.
*
* This facet is created automatically by the Telemeter. Consumers should not add this facet to their
* own events.
*
* @param sourceRelay the Relay where the failure occurred
* @param message a message associated with the failure
* @param throwable a [Throwable] associated with the failure
* @param cause an optional RelayFailure that was the cause of this failure
*/
public data class RelayFailure(
val sourceRelay: Relay,
val message: String? = null,
val throwable: Throwable? = null,
val cause: RelayFailure? = null,
) : Facet
Loading
Loading