Skip to content

Commit

Permalink
La til read-only health indicator
Browse files Browse the repository at this point in the history
  • Loading branch information
naviktthomas committed Dec 11, 2024
1 parent c9fcafa commit 4e3c767
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ ij_kotlin_imports_layout = *,java.**,javax.**,kotlin.**,^
ij_kotlin_name_count_to_use_star_import = 9999
ij_kotlin_name_count_to_use_star_import_for_members = 9999
ij_kotlin_packages_to_use_import_on_demand = java.util.*,kotlinx.android.synthetic.*,io.ktor.*

[{*.yml,*.yaml}]
indent_size = 2
tab_width = 2
ij_continuation_indent_size = 4
4 changes: 0 additions & 4 deletions apps/bekreftelse-api/nais/nais-prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,6 @@ spec:
accessPolicy:
inbound:
rules:
- application: tokenx-token-generator
namespace: aura
- application: azure-token-generator
namespace: aura
- application: arbeidssokerregistrering-for-veileder
namespace: paw
- application: arbeidssoekerregisteret-for-personbruker
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,47 @@ package no.nav.paw.health.model

import java.util.concurrent.atomic.AtomicReference

enum class HealthStatus(val value: String, val priority: Int) {
UNKNOWN("UNKNOWN", 10),
HEALTHY("HEALTHY", 5),
UNHEALTHY("UNHEALTHY", 1),
enum class HealthStatus(val value: String) {
UNKNOWN("UNKNOWN"),
HEALTHY("HEALTHY"),
UNHEALTHY("UNHEALTHY"),
}

open class HealthIndicator(initialStatus: HealthStatus) {
interface HealthIndicator {
fun getStatus(): HealthStatus
}

interface MutableHealthIndicator : HealthIndicator {
fun setUnknown();

fun setHealthy();

fun setUnhealthy();
}

open class DefaultHealthIndicator(initialStatus: HealthStatus) : MutableHealthIndicator {

private val status = AtomicReference(initialStatus)

fun setUnknown() {
override fun setUnknown() {
status.set(HealthStatus.UNKNOWN)
}

fun setHealthy() {
override fun setHealthy() {
status.set(HealthStatus.HEALTHY)
}

fun setUnhealthy() {
override fun setUnhealthy() {
status.set(HealthStatus.UNHEALTHY)
}

fun getStatus(): HealthStatus {
override fun getStatus(): HealthStatus {
return status.get()
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
other as HealthIndicator
if (other !is DefaultHealthIndicator) return false
return status == other.status
}

Expand All @@ -41,9 +53,20 @@ open class HealthIndicator(initialStatus: HealthStatus) {

typealias HealthIndicatorList = MutableList<HealthIndicator>

fun HealthIndicatorList.getAggregatedStatus(): HealthStatus =
map(HealthIndicator::getStatus)
.minByOrNull(HealthStatus::priority) ?: HealthStatus.UNKNOWN
fun HealthIndicatorList.getAggregatedStatus(): HealthStatus {
return if (this.isEmpty()) {
HealthStatus.HEALTHY // Default til healthy
} else if (this.all { it.getStatus() == HealthStatus.HEALTHY }) {
HealthStatus.HEALTHY
} else if (this.any { it.getStatus() == HealthStatus.UNHEALTHY }) {
HealthStatus.UNHEALTHY
} else {
HealthStatus.UNKNOWN
}
}

class ReadinessHealthIndicator(initialStatus: HealthStatus = HealthStatus.UNKNOWN) :
DefaultHealthIndicator(initialStatus)

class ReadinessHealthIndicator(initialStatus: HealthStatus = HealthStatus.UNKNOWN) : HealthIndicator(initialStatus)
class LivenessHealthIndicator(initialStatus: HealthStatus = HealthStatus.HEALTHY) : HealthIndicator(initialStatus)
class LivenessHealthIndicator(initialStatus: HealthStatus = HealthStatus.HEALTHY) :
DefaultHealthIndicator(initialStatus)
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package no.nav.paw.health.repository

import no.nav.paw.health.model.HealthIndicator
import no.nav.paw.health.model.HealthIndicatorList
import no.nav.paw.health.model.LivenessHealthIndicator
import no.nav.paw.health.model.ReadinessHealthIndicator

class HealthIndicatorRepository {

private val readinessIndicators: HealthIndicatorList = mutableListOf()
private val livenessIndicators: HealthIndicatorList = mutableListOf()

fun addReadinessIndicator(healthIndicator: ReadinessHealthIndicator): ReadinessHealthIndicator {
fun <T : HealthIndicator> addReadinessIndicator(healthIndicator: T): T {
readinessIndicators.add(healthIndicator)
return healthIndicator
}

fun addLivenessIndicator(healthIndicator: LivenessHealthIndicator): LivenessHealthIndicator {
fun <T : HealthIndicator> addLivenessIndicator(healthIndicator: T): T {
livenessIndicators.add(healthIndicator)
return healthIndicator
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ class HealthRoutesTest : FreeSpec({
val livenessResponse1 = client.get("/internal/isAlive")
val readinessResponse1 = client.get("/internal/isReady")

livenessResponse1.status shouldBe HttpStatusCode.ServiceUnavailable
livenessResponse1.body<String>() shouldBe HealthStatus.UNKNOWN.value
readinessResponse1.status shouldBe HttpStatusCode.ServiceUnavailable
readinessResponse1.body<String>() shouldBe HealthStatus.UNKNOWN.value
livenessResponse1.status shouldBe HttpStatusCode.OK
livenessResponse1.body<String>() shouldBe HealthStatus.HEALTHY.value
readinessResponse1.status shouldBe HttpStatusCode.OK
readinessResponse1.body<String>() shouldBe HealthStatus.HEALTHY.value

val liveness1 = healthIndicatorRepository.addLivenessIndicator(LivenessHealthIndicator())
val liveness2 = healthIndicatorRepository.addLivenessIndicator(LivenessHealthIndicator())
Expand Down

0 comments on commit 4e3c767

Please sign in to comment.