Skip to content

Commit

Permalink
feat: update test cases in MicrometerMetricsFacadeTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesho committed Nov 29, 2024
1 parent 69ad44d commit 71d80e0
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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 <T> createSimpleTimer(
category: LineaMetricsCategory?,
name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
}
Expand All @@ -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<Unit>(
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<Unit>(
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<Unit>(
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
}
}

0 comments on commit 71d80e0

Please sign in to comment.