All Downloads are FREE. Search and download functionalities are using the official Maven repository.

orbit.server.service.HealthService.kt Maven / Gradle / Ivy

/*
 Copyright (C) 2015 - 2019 Electronic Arts Inc.  All rights reserved.
 This file is part of the Orbit Project .
 See license in LICENSE.
 */

package orbit.server.service

import grpc.health.v1.HealthImplBase
import grpc.health.v1.HealthOuterClass
import io.micrometer.core.instrument.Metrics
import orbit.util.instrumentation.recordSuspended
import java.util.concurrent.atomic.AtomicInteger

class HealthService(private val checks: HealthCheckList) : HealthImplBase() {
    private val healthyChecks = AtomicInteger()

    init {
        Metrics.gauge(Meters.Names.PassingHealthChecks, healthyChecks)
    }

    override suspend fun check(request: HealthOuterClass.HealthCheckRequest): HealthOuterClass.HealthCheckResponse {
        return HealthOuterClass.HealthCheckResponse.newBuilder()
            .setStatus(
                if (this.isHealthy()) {
                    HealthOuterClass.HealthCheckResponse.ServingStatus.SERVING
                } else {
                    HealthOuterClass.HealthCheckResponse.ServingStatus.NOT_SERVING
                }
            ).build()
    }

    suspend fun isHealthy(): Boolean {
        val checks = checks.getChecks()
        Metrics.timer(Meters.Names.HealthCheck).recordSuspended {
            healthyChecks.set(checks.count { check -> check.isHealthy() })
        }
        return healthyChecks.get() == checks.count()
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy