Skip to content

Commit

Permalink
Metrics/Koin: Prevent race condition
Browse files Browse the repository at this point in the history
Signed-off-by: Till Kottmann <me@deletescape.ch>
  • Loading branch information
nyancrimew committed Nov 28, 2019
1 parent 5ab8597 commit 0965d21
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions app/src/dog/del/app/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,30 @@ fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
fun Application.module(testing: Boolean = false) {
val appConfig = AppConfig(environment.config)

install(PrometheusFeature) {
disableMetricsEndpoint()
}
DefaultExports.initialize()
DogbinCollectors.register()

val metrics = DogbinMetrics()

val metricsPhase = PipelinePhase("metrics")
insertPhaseBefore(ApplicationCallPipeline.Monitoring, metricsPhase)
intercept(metricsPhase) {
metrics.activeRequests.inc()
val timer = metrics.requestDuration.startTimer()
try {
proceed()
} catch (e: Exception) {
metrics.exceptions.labels((e::class.qualifiedName ?: e::class.jvmName)).inc()
throw e
} finally {
timer.setDuration()
metrics.activeRequests.dec()
}
}

install(Koin) {
// TODO: split into multiple modules
val appModule = org.koin.dsl.module {
Expand All @@ -95,7 +119,7 @@ fun Application.module(testing: Boolean = false) {
single { MarkdownRenderer() }
single { Iframely() }
single { PasswordEstimator.init() }
single { DogbinMetrics() }
single { metrics }
}
modules(
appModule
Expand Down Expand Up @@ -166,29 +190,6 @@ fun Application.module(testing: Boolean = false) {
header("X-Api-Key")
}

install(PrometheusFeature) {
disableMetricsEndpoint()
}
DefaultExports.initialize()
DogbinCollectors.register()

val metricsPhase = PipelinePhase("metrics")
insertPhaseBefore(ApplicationCallPipeline.Monitoring, metricsPhase)
intercept(metricsPhase) {
val metrics = get<DogbinMetrics>()
metrics.activeRequests.inc()
val timer = metrics.requestDuration.startTimer()
try {
proceed()
} catch (e: Exception) {
metrics.exceptions.labels((e::class.qualifiedName ?: e::class.jvmName)).inc()
throw e
} finally {
timer.setDuration()
metrics.activeRequests.dec()
}
}

install(Sessions) {
cookie<WebSession>("doggie_session", XdSessionStorage()) {
transform(SessionTransportTransformerMessageAuthentication(appConfig.keys.session))
Expand Down

0 comments on commit 0965d21

Please sign in to comment.