Skip to content

Commit

Permalink
Merge pull request #23 from kartverket/prometheus-koin-di
Browse files Browse the repository at this point in the history
Ta i bruk Koin for dependency injection av PrometheusMeterRegistry
  • Loading branch information
henriwi authored Jul 30, 2024
2 parents 70458cd + c00b1ef commit ea17814
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 16 deletions.
27 changes: 15 additions & 12 deletions src/main/kotlin/no/kartverket/matrikkel/bygning/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import org.koin.dsl.module
import org.koin.ktor.ext.inject
import org.koin.ktor.plugin.KoinIsolated

val appMicrometerRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT)

fun main(args: Array<String>) {
embeddedServer(Netty, port = 8081, host = "0.0.0.0", module = Application::internalModule).start(wait = false)

Expand Down Expand Up @@ -64,6 +62,9 @@ val appModule = module {
single { EgenregistreringsService(get()) }
}

val metricsModule = module {
single { PrometheusMeterRegistry(PrometheusConfig.DEFAULT) }
}

@OptIn(ExperimentalSerializationApi::class)
fun Application.module() {
Expand All @@ -87,10 +88,6 @@ fun Application.module() {
}
}

install(MicrometerMetrics) {
registry = appMicrometerRegistry
}

install(NotarizedApplication()) {
spec = OpenApiSpec(
jsonSchemaDialect = "https://spec.openapis.org/oas/3.1/dialect/base", info = Info(
Expand All @@ -102,7 +99,12 @@ fun Application.module() {
}

install(KoinIsolated) {
modules(appModule, databaseModule)
modules(appModule, databaseModule, metricsModule)
}

val meterRegistry by inject<PrometheusMeterRegistry>()
install(MicrometerMetrics) {
registry = meterRegistry
}

val dbFactory: DatabaseFactory by inject()
Expand All @@ -117,13 +119,14 @@ val internalModule = module {
}

fun Application.internalModule() {
install(MicrometerMetrics) {
registry = appMicrometerRegistry
install(KoinIsolated) {
modules(internalModule, databaseModule, metricsModule)
}

install(KoinIsolated) {
modules(internalModule, databaseModule)
val meterRegistry by inject<PrometheusMeterRegistry>()
install(MicrometerMetrics) {
registry = meterRegistry
}

installInternalRouting()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import no.kartverket.matrikkel.bygning.appMicrometerRegistry
import io.micrometer.prometheusmetrics.PrometheusMeterRegistry
import no.kartverket.matrikkel.bygning.services.HealthService
import org.koin.ktor.ext.inject

fun Application.installInternalRouting() {
routing {
route("/metrics") {
val meterRegistry by inject<PrometheusMeterRegistry>()
get {
call.respondText(appMicrometerRegistry.scrape())
call.respondText(meterRegistry.scrape())
}
}


route("/healthz") {
val healthService: HealthService by inject()

Expand All @@ -31,4 +31,4 @@ fun Application.installInternalRouting() {
}
}
}
}
}

0 comments on commit ea17814

Please sign in to comment.