From 71d80e04efdab3fdffc5aeb6521f7de94347dee6 Mon Sep 17 00:00:00 2001 From: jonesho Date: Sat, 30 Nov 2024 01:31:56 +0800 Subject: [PATCH] feat: update test cases in MicrometerMetricsFacadeTest --- .../micrometer/MicrometerMetricsFacade.kt | 12 +- .../micrometer/MicrometerMetricsFacadeTest.kt | 106 +++++++++++++++++- 2 files changed, 111 insertions(+), 7 deletions(-) diff --git a/jvm-libs/linea/metrics/micrometer/src/main/kotlin/net/consensys/linea/metrics/micrometer/MicrometerMetricsFacade.kt b/jvm-libs/linea/metrics/micrometer/src/main/kotlin/net/consensys/linea/metrics/micrometer/MicrometerMetricsFacade.kt index 518e6050c..5090183d8 100644 --- a/jvm-libs/linea/metrics/micrometer/src/main/kotlin/net/consensys/linea/metrics/micrometer/MicrometerMetricsFacade.kt +++ b/jvm-libs/linea/metrics/micrometer/src/main/kotlin/net/consensys/linea/metrics/micrometer/MicrometerMetricsFacade.kt @@ -39,6 +39,12 @@ class MicrometerMetricsFacade( if (metricsPrefix != null) requireValidMicrometerName(metricsPrefix) } + private fun metricHandle(category: LineaMetricsCategory?, metricName: String): String { + val prefixName = if (metricsPrefix == null) "" else "$metricsPrefix." + val categoryName = if (category == null) "" else "$category." + return "$prefixName$categoryName$metricName" + } + override fun createGauge( category: LineaMetricsCategory?, name: String, @@ -103,12 +109,6 @@ class MicrometerMetricsFacade( return MicrometerHistogramAdapter(distributionSummaryBuilder.register(registry)) } - private fun metricHandle(category: LineaMetricsCategory?, metricName: String): String { - val prefixName = if (metricsPrefix == null) "" else "$metricsPrefix." - val categoryName = if (category == null) "" else "$category." - return "$prefixName$categoryName$metricName" - } - override fun createSimpleTimer( category: LineaMetricsCategory?, name: String, diff --git a/jvm-libs/linea/metrics/micrometer/src/test/kotlin/net/consensys/linea/metrics/micrometer/MicrometerMetricsFacadeTest.kt b/jvm-libs/linea/metrics/micrometer/src/test/kotlin/net/consensys/linea/metrics/micrometer/MicrometerMetricsFacadeTest.kt index 03ec36772..39df4fcb3 100644 --- a/jvm-libs/linea/metrics/micrometer/src/test/kotlin/net/consensys/linea/metrics/micrometer/MicrometerMetricsFacadeTest.kt +++ b/jvm-libs/linea/metrics/micrometer/src/test/kotlin/net/consensys/linea/metrics/micrometer/MicrometerMetricsFacadeTest.kt @@ -82,6 +82,9 @@ class MicrometerMetricsFacadeTest { val createdHistogram = meterRegistry.find("linea.test.batch.some.metric").summary() assertThat(createdHistogram).isNotNull assertThat(createdHistogram!!.id.description).isEqualTo("This is a test metric") + assertThat(createdHistogram.id.tags).isEqualTo( + listOf(ImmutableTag("key1", "value1"), ImmutableTag("key2", "value2")) + ) assertThat(createdHistogram.id.baseUnit).isEqualTo("seconds") assertThat(createdHistogram.count()).isEqualTo(0L) @@ -103,7 +106,7 @@ class MicrometerMetricsFacadeTest { } @Test - fun `createSimpleTimer creates Timer with specified parameters`() { + fun `createSimpleTimer creates timer with specified parameters`() { fun mockTimer() { Thread.sleep(200L) } @@ -119,10 +122,111 @@ class MicrometerMetricsFacadeTest { val createdTimer = meterRegistry.find("linea.test.some.timer.metric").timer() assertThat(createdTimer).isNotNull assertThat(createdTimer!!.id.description).isEqualTo("This is a test metric") + assertThat(createdTimer.id.tags).isEqualTo(listOf(ImmutableTag("key1", "value1"), ImmutableTag("key2", "value2"))) assertThat(createdTimer.max(TimeUnit.SECONDS)).isGreaterThan(0.2) timer.captureTime(::mockTimer) assertThat(createdTimer.totalTime(TimeUnit.SECONDS)).isGreaterThan(0.4) assertThat(createdTimer.mean(TimeUnit.SECONDS)).isGreaterThan(0.2) } + + @Test + fun `createDynamicTagTimer creates timer with specified parameters`() { + fun mockTimer() { + Thread.sleep(200L) + } + + val timer = metricsFacade.createDynamicTagTimer( + name = "some.dynamictag.timer.metric", + description = "This is a test metric", + tagKey = "key", + tagValueExtractorOnError = { "unfound_key" } + ) { + "value" + } + + timer.captureTime(::mockTimer) + val createdTimer = meterRegistry.find("linea.test.some.dynamictag.timer.metric").timer() + assertThat(createdTimer).isNotNull + assertThat(createdTimer!!.id.description).isEqualTo("This is a test metric") + assertThat(createdTimer.id.tags).isEqualTo(listOf(ImmutableTag("key", "value"))) + assertThat(createdTimer.max(TimeUnit.SECONDS)).isGreaterThan(0.2) + + timer.captureTime(::mockTimer) + assertThat(createdTimer.totalTime(TimeUnit.SECONDS)).isGreaterThan(0.4) + assertThat(createdTimer.mean(TimeUnit.SECONDS)).isGreaterThan(0.2) + } + + @Test + fun `createGauge creates gauge with correct name when metrics prefix and category are absent`() { + val metricMeasureValue = 0L + val meterRegistry = SimpleMeterRegistry() + val metricsFacade = MicrometerMetricsFacade(meterRegistry) + metricsFacade.createGauge( + name = "some.gauge.metric", + description = "This is a test metric", + measurementSupplier = { metricMeasureValue }, + tags = listOf(Tag("key1", "value1"), Tag("key2", "value2")) + ) + val createdGauge = meterRegistry.find("some.gauge.metric").gauge() + assertThat(createdGauge).isNotNull + } + + @Test + fun `createCounter creates counter with correct name when metrics prefix and category are absent`() { + val meterRegistry = SimpleMeterRegistry() + val metricsFacade = MicrometerMetricsFacade(meterRegistry) + metricsFacade.createCounter( + name = "some.counter.metric", + description = "This is a test metric", + tags = listOf(Tag("key1", "value1"), Tag("key2", "value2")) + ) + val createdCounter = meterRegistry.find("some.counter.metric").counter() + assertThat(createdCounter).isNotNull + } + + @Test + fun `createHistogram creates histogram with correct name when metrics prefix and category are absent`() { + val meterRegistry = SimpleMeterRegistry() + val metricsFacade = MicrometerMetricsFacade(meterRegistry) + metricsFacade.createHistogram( + name = "some.histogram.metric", + description = "This is a test metric", + tags = listOf(Tag("key1", "value1"), Tag("key2", "value2")), + baseUnit = "seconds" + ) + val createdHistogram = meterRegistry.find("some.histogram.metric").summary() + assertThat(createdHistogram).isNotNull + } + + @Test + fun `createSimpleTimer creates timer with correct name when metrics prefix and category are absent`() { + val meterRegistry = SimpleMeterRegistry() + val metricsFacade = MicrometerMetricsFacade(meterRegistry) + val timer = metricsFacade.createSimpleTimer( + name = "some.timer.metric", + description = "This is a test metric", + tags = listOf(Tag("key1", "value1"), Tag("key2", "value2")) + ) + timer.captureTime {} + val createdTimer = meterRegistry.find("some.timer.metric").timer() + assertThat(createdTimer).isNotNull + } + + @Test + fun `createDynamicTagTimer creates timer with correct name when metrics prefix and category are absent`() { + val meterRegistry = SimpleMeterRegistry() + val metricsFacade = MicrometerMetricsFacade(meterRegistry) + val timer = metricsFacade.createDynamicTagTimer( + name = "some.dynamictag.timer.metric", + description = "This is a test metric", + tagKey = "key", + tagValueExtractorOnError = { "unfound_key" } + ) { + "value" + } + timer.captureTime {} + val createdTimer = meterRegistry.find("some.dynamictag.timer.metric").timer() + assertThat(createdTimer).isNotNull + } }