diff --git a/ktor-server/ktor-server-plugins/ktor-server-metrics-micrometer/jvm/src/io/ktor/server/metrics/micrometer/MicrometerMetrics.kt b/ktor-server/ktor-server-plugins/ktor-server-metrics-micrometer/jvm/src/io/ktor/server/metrics/micrometer/MicrometerMetrics.kt index 014396afbc0..c9f41590b86 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-metrics-micrometer/jvm/src/io/ktor/server/metrics/micrometer/MicrometerMetrics.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-metrics-micrometer/jvm/src/io/ktor/server/metrics/micrometer/MicrometerMetrics.kt @@ -49,6 +49,10 @@ public class MicrometerMetricsConfig { * @see [MicrometerMetrics] */ public var registry: MeterRegistry = LoggingMeterRegistry() + set(value) { + field.close() + field = value + } /** * Specifies if requests for non-existent routes should diff --git a/ktor-server/ktor-server-plugins/ktor-server-metrics-micrometer/jvm/test/io/ktor/server/metrics/micrometer/MicrometerMetricsTests.kt b/ktor-server/ktor-server-plugins/ktor-server-metrics-micrometer/jvm/test/io/ktor/server/metrics/micrometer/MicrometerMetricsTests.kt index 67bddf2a68c..cdf4e4e1383 100644 --- a/ktor-server/ktor-server-plugins/ktor-server-metrics-micrometer/jvm/test/io/ktor/server/metrics/micrometer/MicrometerMetricsTests.kt +++ b/ktor-server/ktor-server-plugins/ktor-server-metrics-micrometer/jvm/test/io/ktor/server/metrics/micrometer/MicrometerMetricsTests.kt @@ -18,6 +18,7 @@ import io.micrometer.core.instrument.binder.* import io.micrometer.core.instrument.binder.jvm.* import io.micrometer.core.instrument.binder.system.* import io.micrometer.core.instrument.distribution.* +import io.micrometer.core.instrument.logging.* import io.micrometer.core.instrument.simple.* import kotlin.reflect.* import kotlin.test.* @@ -399,6 +400,27 @@ class MicrometerMetricsTests { assertEquals("OK", response.bodyAsText()) } + @Test + fun `test closes previous registry`(): Unit = testApplication { + var closed = false + val metrics = object : LoggingMeterRegistry() { + override fun close() { + super.close() + closed = true + } + } + + application { + install(MicrometerMetrics) { + registry = metrics + registry = LoggingMeterRegistry() + } + } + + startApplication() + assertTrue(closed) + } + private fun TestApplicationEngine.metersAreRegistered( meterBinder: KClass, vararg meterNames: String