Skip to content

Commit

Permalink
Legg til mulighet for å sende inn eget registry
Browse files Browse the repository at this point in the history
  • Loading branch information
LudvigHz committed Oct 4, 2024
1 parent 3246628 commit 7b5a58c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -54,7 +55,7 @@ object Metrics {
route(config.contextpath) {
route("internal") {
get("metrics") {
call.respondText(Registry.scrape())
call.respondText(config.registry.scrape())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 7b5a58c

Please sign in to comment.