Compare commits
1 Commits
master
...
watchdog-b
Author | SHA1 | Date |
---|---|---|
usbharu | a54aadfe87 |
|
@ -3,9 +3,8 @@ package dev.usbharu.unos.watchdog.be
|
||||||
import com.mongodb.ConnectionString
|
import com.mongodb.ConnectionString
|
||||||
import com.mongodb.MongoClientSettings
|
import com.mongodb.MongoClientSettings
|
||||||
import com.mongodb.kotlin.client.coroutine.MongoClient
|
import com.mongodb.kotlin.client.coroutine.MongoClient
|
||||||
import com.mongodb.kotlin.client.coroutine.MongoDatabase
|
|
||||||
import dev.usbharu.unos.watchdog.be.domain.MongoMetricsRepository
|
import dev.usbharu.unos.watchdog.be.domain.MongoMetricsRepository
|
||||||
import io.grpc.ServerBuilder
|
import io.grpc.ManagedChannelBuilder
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
import io.ktor.serialization.kotlinx.json.*
|
import io.ktor.serialization.kotlinx.json.*
|
||||||
import io.ktor.server.application.*
|
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.contentnegotiation.*
|
||||||
import io.ktor.server.plugins.cors.routing.*
|
import io.ktor.server.plugins.cors.routing.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.asExecutor
|
||||||
import org.slf4j.event.Level
|
import org.slf4j.event.Level
|
||||||
|
import watchdog.WatchdogBellServiceGrpcKt
|
||||||
import java.net.URI
|
import java.net.URI
|
||||||
|
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
|
@ -25,7 +27,7 @@ fun Application.module() {
|
||||||
install(ContentNegotiation) {
|
install(ContentNegotiation) {
|
||||||
json()
|
json()
|
||||||
}
|
}
|
||||||
install(CORS){
|
install(CORS) {
|
||||||
allowMethod(HttpMethod.Get)
|
allowMethod(HttpMethod.Get)
|
||||||
allowMethod(HttpMethod.Post)
|
allowMethod(HttpMethod.Post)
|
||||||
allowMethod(HttpMethod.Put)
|
allowMethod(HttpMethod.Put)
|
||||||
|
@ -38,7 +40,7 @@ fun Application.module() {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
install(CallLogging){
|
install(CallLogging) {
|
||||||
level = Level.TRACE
|
level = Level.TRACE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +58,22 @@ fun Application.module() {
|
||||||
|
|
||||||
val mongoMetricsRepository = MongoMetricsRepository(database)
|
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 {
|
routing {
|
||||||
api(ApiController(mongoMetricsRepository))
|
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.MetricsRepository
|
||||||
import dev.usbharu.unos.watchdog.be.domain.Status
|
import dev.usbharu.unos.watchdog.be.domain.Status
|
||||||
import kotlinx.datetime.toKotlinInstant
|
import kotlinx.datetime.toKotlinInstant
|
||||||
import watchdog.PushMetrics
|
import watchdog.*
|
||||||
import watchdog.PushMetricsServiceGrpcKt
|
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.coroutines.CoroutineContext
|
import kotlin.coroutines.CoroutineContext
|
||||||
import kotlin.coroutines.EmptyCoroutineContext
|
import kotlin.coroutines.EmptyCoroutineContext
|
||||||
|
|
||||||
class PushMetricsService(
|
class PushMetricsService(
|
||||||
private val metricsRepository: MetricsRepository,
|
private val metricsRepository: MetricsRepository,
|
||||||
|
private val bell: WatchdogBellServiceGrpcKt.WatchdogBellServiceCoroutineStub?,
|
||||||
coroutineContext: CoroutineContext = EmptyCoroutineContext
|
coroutineContext: CoroutineContext = EmptyCoroutineContext
|
||||||
) :
|
) :
|
||||||
PushMetricsServiceGrpcKt.PushMetricsServiceCoroutineImplBase(coroutineContext) {
|
PushMetricsServiceGrpcKt.PushMetricsServiceCoroutineImplBase(coroutineContext) {
|
||||||
override suspend fun push(request: PushMetrics.Metric): Empty {
|
override suspend fun push(request: PushMetrics.Metric): Empty {
|
||||||
|
|
||||||
|
|
||||||
|
bell(request)
|
||||||
|
|
||||||
metricsRepository.save(
|
metricsRepository.save(
|
||||||
Metric(
|
Metric(
|
||||||
id = UUID.randomUUID().toString(),
|
id = UUID.randomUUID().toString(),
|
||||||
|
@ -31,4 +35,25 @@ class PushMetricsService(
|
||||||
)
|
)
|
||||||
return Empty.getDefaultInstance()
|
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 {
|
watchdog {
|
||||||
|
front-url = "http://localhost:8081"
|
||||||
db {
|
db {
|
||||||
url = "mongodb://localhost:27017"
|
url = "mongodb://localhost:27017"
|
||||||
database = "watchdog"
|
database = "watchdog"
|
||||||
|
@ -16,5 +17,9 @@ watchdog {
|
||||||
grpc {
|
grpc {
|
||||||
port = 50051
|
port = 50051
|
||||||
}
|
}
|
||||||
|
bell {
|
||||||
|
use = "true"
|
||||||
|
host = "localhost"
|
||||||
|
port = 50051
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue