watchdog-be: bellにアラートを出せるように
This commit is contained in:
parent
631a4c831f
commit
a54aadfe87
|
@ -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<String>) {
|
||||
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue