From a54aadfe871749c4a422cade8492d1c672ef5d71 Mon Sep 17 00:00:00 2001 From: usbharu <64310155+usbharu@users.noreply.github.com> Date: Tue, 19 Mar 2024 16:17:14 +0900 Subject: [PATCH] =?UTF-8?q?watchdog-be:=20bell=E3=81=AB=E3=82=A2=E3=83=A9?= =?UTF-8?q?=E3=83=BC=E3=83=88=E3=82=92=E5=87=BA=E3=81=9B=E3=82=8B=E3=82=88?= =?UTF-8?q?=E3=81=86=E3=81=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev/usbharu/unos/watchdog/be/Main.kt | 27 +++++++++++++---- .../unos/watchdog/be/PushMetricsService.kt | 29 +++++++++++++++++-- .../src/main/resources/application.conf | 7 ++++- 3 files changed, 55 insertions(+), 8 deletions(-) diff --git a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/Main.kt b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/Main.kt index 69a483e..7a646bf 100644 --- a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/Main.kt +++ b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/Main.kt @@ -3,9 +3,8 @@ package dev.usbharu.unos.watchdog.be import com.mongodb.ConnectionString import com.mongodb.MongoClientSettings import com.mongodb.kotlin.client.coroutine.MongoClient -import com.mongodb.kotlin.client.coroutine.MongoDatabase import dev.usbharu.unos.watchdog.be.domain.MongoMetricsRepository -import io.grpc.ServerBuilder +import io.grpc.ManagedChannelBuilder import io.ktor.http.* import io.ktor.serialization.kotlinx.json.* import io.ktor.server.application.* @@ -13,7 +12,10 @@ import io.ktor.server.plugins.callloging.* import io.ktor.server.plugins.contentnegotiation.* import io.ktor.server.plugins.cors.routing.* import io.ktor.server.routing.* +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.asExecutor import org.slf4j.event.Level +import watchdog.WatchdogBellServiceGrpcKt import java.net.URI fun main(args: Array) { @@ -25,7 +27,7 @@ fun Application.module() { install(ContentNegotiation) { json() } - install(CORS){ + install(CORS) { allowMethod(HttpMethod.Get) allowMethod(HttpMethod.Post) allowMethod(HttpMethod.Put) @@ -38,7 +40,7 @@ fun Application.module() { true } } - install(CallLogging){ + install(CallLogging) { level = Level.TRACE } @@ -56,7 +58,22 @@ fun Application.module() { val mongoMetricsRepository = MongoMetricsRepository(database) - grpc(grpcPort,PushMetricsService(mongoMetricsRepository)) + val useBell = environment.config.property("watchdog.bell.use").getString().toBoolean() + + val bell = if (useBell) { + val bellHost = environment.config.property("watchdog.bell.host").getString() + val bellPort = environment.config.property("watchdog.bell.port").getString().toInt() + + val channel = + ManagedChannelBuilder.forAddress(bellHost, bellPort).usePlaintext().executor(Dispatchers.IO.asExecutor()) + .build() + WatchdogBellServiceGrpcKt.WatchdogBellServiceCoroutineStub(channel) + } else { + null + } + + + grpc(grpcPort, PushMetricsService(mongoMetricsRepository, bell)) routing { api(ApiController(mongoMetricsRepository)) } diff --git a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/PushMetricsService.kt b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/PushMetricsService.kt index 26d0df5..8c93d4b 100644 --- a/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/PushMetricsService.kt +++ b/watch-dog-be/src/main/kotlin/dev/usbharu/unos/watchdog/be/PushMetricsService.kt @@ -6,18 +6,22 @@ import dev.usbharu.unos.watchdog.be.domain.Metric import dev.usbharu.unos.watchdog.be.domain.MetricsRepository import dev.usbharu.unos.watchdog.be.domain.Status import kotlinx.datetime.toKotlinInstant -import watchdog.PushMetrics -import watchdog.PushMetricsServiceGrpcKt +import watchdog.* import java.util.* import kotlin.coroutines.CoroutineContext import kotlin.coroutines.EmptyCoroutineContext class PushMetricsService( private val metricsRepository: MetricsRepository, + private val bell: WatchdogBellServiceGrpcKt.WatchdogBellServiceCoroutineStub?, coroutineContext: CoroutineContext = EmptyCoroutineContext ) : PushMetricsServiceGrpcKt.PushMetricsServiceCoroutineImplBase(coroutineContext) { override suspend fun push(request: PushMetrics.Metric): Empty { + + + bell(request) + metricsRepository.save( Metric( id = UUID.randomUUID().toString(), @@ -31,4 +35,25 @@ class PushMetricsService( ) return Empty.getDefaultInstance() } + + private suspend fun bell(request: PushMetrics.Metric) { + val level = when (request.status) { + watchdog.PushMetrics.Status.OK -> return + watchdog.PushMetrics.Status.DEAD -> PushBell.Bell.Level.FATAL + watchdog.PushMetrics.Status.UNKNOWN -> PushBell.Bell.Level.WARN + watchdog.PushMetrics.Status.NOT_GOOD -> PushBell.Bell.Level.WARN + watchdog.PushMetrics.Status.UNRECOGNIZED -> PushBell.Bell.Level.WARN + } + + + bell?.push( + bell { + this.level = level + this.title = "${request.name} is ${request.status}" + this.location = request.domain + this.timestamp = request.timestamp + this.message = request.message + } + ) + } } \ No newline at end of file diff --git a/watch-dog-be/src/main/resources/application.conf b/watch-dog-be/src/main/resources/application.conf index ee08044..b6e73bc 100644 --- a/watch-dog-be/src/main/resources/application.conf +++ b/watch-dog-be/src/main/resources/application.conf @@ -9,6 +9,7 @@ ktor { } watchdog { + front-url = "http://localhost:8081" db { url = "mongodb://localhost:27017" database = "watchdog" @@ -16,5 +17,9 @@ watchdog { grpc { port = 50051 } - + bell { + use = "true" + host = "localhost" + port = 50051 + } } \ No newline at end of file