From 7b5a58c819aef11c774eb30b4f7e3dd624767e74 Mon Sep 17 00:00:00 2001 From: Ludvig Hozman Date: Fri, 4 Oct 2024 11:28:14 +0200 Subject: [PATCH] =?UTF-8?q?Legg=20til=20mulighet=20for=20=C3=A5=20sende=20?= =?UTF-8?q?inn=20eget=20registry?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../no/nav/personoversikt/common/ktor/utils/Metrics.kt | 7 ++++--- .../personoversikt/common/ktor/utils/MetricsTest.kt | 10 ++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/ktor-utils/src/main/kotlin/no/nav/personoversikt/common/ktor/utils/Metrics.kt b/ktor-utils/src/main/kotlin/no/nav/personoversikt/common/ktor/utils/Metrics.kt index 32d409c..9eff3cf 100644 --- a/ktor-utils/src/main/kotlin/no/nav/personoversikt/common/ktor/utils/Metrics.kt +++ b/ktor-utils/src/main/kotlin/no/nav/personoversikt/common/ktor/utils/Metrics.kt @@ -19,6 +19,7 @@ import io.micrometer.prometheusmetrics.PrometheusMeterRegistry object Metrics { class Config { var contextpath: String = "" + var registry: PrometheusMeterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT) // Expose configuration of MicrometerMetrics, defaults copied var metricName: String = "ktor.http.server.requests" @@ -38,12 +39,12 @@ object Metrics { timerBuilder = block } } - val Registry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT) + val Plugin = createApplicationPlugin("Metrics", Metrics::Config) { val config = pluginConfig with(application) { install(MicrometerMetrics) { - registry = Registry + registry = config.registry metricName = config.metricName meterBinders = config.meterBinders distributionStatisticConfig = config.distributionStatisticConfig @@ -54,7 +55,7 @@ object Metrics { route(config.contextpath) { route("internal") { get("metrics") { - call.respondText(Registry.scrape()) + call.respondText(config.registry.scrape()) } } } diff --git a/ktor-utils/src/test/kotlin/no/nav/personoversikt/common/ktor/utils/MetricsTest.kt b/ktor-utils/src/test/kotlin/no/nav/personoversikt/common/ktor/utils/MetricsTest.kt index 9d13beb..ac820a5 100644 --- a/ktor-utils/src/test/kotlin/no/nav/personoversikt/common/ktor/utils/MetricsTest.kt +++ b/ktor-utils/src/test/kotlin/no/nav/personoversikt/common/ktor/utils/MetricsTest.kt @@ -5,6 +5,8 @@ import io.ktor.client.statement.* import io.ktor.http.* import io.ktor.server.application.* import io.ktor.server.testing.* +import io.micrometer.prometheusmetrics.PrometheusConfig +import io.micrometer.prometheusmetrics.PrometheusMeterRegistry import org.junit.jupiter.api.Assertions.* import org.junit.jupiter.api.Test @@ -36,11 +38,15 @@ internal class MetricsTest { // Must alias plugin to ensure Metrics.kt is only loaded once by the classloader during test val plugin = Metrics.Plugin + val meterRegistry = PrometheusMeterRegistry(PrometheusConfig.DEFAULT) + application { - install(plugin) + install(plugin) { + registry = meterRegistry + } } - val counter = Metrics.Registry.counter("testcounter") + val counter = meterRegistry.counter("testcounter") counter.increment(13.0) val response = client.get("/internal/metrics")