🐛 Fix metrics page showing wrong event count
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 30 Jul 2019 07:59:17 +0000 (09:59 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 30 Jul 2019 07:59:17 +0000 (09:59 +0200)
src/main/kotlin/net/pterodactylus/sone/web/pages/MetricsPage.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/MetricsPageTest.kt

index 041e755..e097daf 100644 (file)
@@ -13,17 +13,19 @@ import javax.inject.*
 class MetricsPage @Inject constructor(webInterface: WebInterface, loaders: Loaders, templateRenderer: TemplateRenderer, private val metricsRegistry: MetricRegistry) : SoneTemplatePage(webInterface, loaders, templateRenderer, "Page.Metrics.Title") {
 
        override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) {
-               metricsRegistry.histogram("sone.parsing.duration").snapshot.also { snapshot ->
-                       templateContext["soneParsingDurationCount"] = snapshot.size()
-                       templateContext["soneParsingDurationMin"] = snapshot.min
-                       templateContext["soneParsingDurationMax"] = snapshot.max
-                       templateContext["soneParsingDurationMedian"] = snapshot.median
-                       templateContext["soneParsingDurationMean"] = snapshot.mean
-                       templateContext["soneParsingDurationPercentile75"] = snapshot.get75thPercentile()
-                       templateContext["soneParsingDurationPercentile95"] = snapshot.get95thPercentile()
-                       templateContext["soneParsingDurationPercentile98"] = snapshot.get98thPercentile()
-                       templateContext["soneParsingDurationPercentile99"] = snapshot.get99thPercentile()
-                       templateContext["soneParsingDurationPercentile999"] = snapshot.get999thPercentile()
+               metricsRegistry.histogram("sone.parsing.duration").also { histogram ->
+                       templateContext["soneParsingDurationCount"] = histogram.count
+                       histogram.snapshot.also { snapshot ->
+                               templateContext["soneParsingDurationMin"] = snapshot.min
+                               templateContext["soneParsingDurationMax"] = snapshot.max
+                               templateContext["soneParsingDurationMedian"] = snapshot.median
+                               templateContext["soneParsingDurationMean"] = snapshot.mean
+                               templateContext["soneParsingDurationPercentile75"] = snapshot.get75thPercentile()
+                               templateContext["soneParsingDurationPercentile95"] = snapshot.get95thPercentile()
+                               templateContext["soneParsingDurationPercentile98"] = snapshot.get98thPercentile()
+                               templateContext["soneParsingDurationPercentile99"] = snapshot.get99thPercentile()
+                               templateContext["soneParsingDurationPercentile999"] = snapshot.get999thPercentile()
+                       }
                }
        }
 
index 0565612..b727909 100644 (file)
@@ -68,8 +68,16 @@ class MetricsPageTest : WebPageTest() {
                verifyHistogram("soneParsingDuration")
        }
 
+       @Test
+       fun `metrics page delivers correct histogram size`() {
+               val histogram = metricRegistry.histogram("sone.parsing.duration")
+               (0..4000).forEach(histogram::update)
+               page.handleRequest(soneRequest, templateContext)
+               assertThat(templateContext["soneParsingDurationCount"] as Long, equalTo(4001L))
+       }
+
        private fun verifyHistogram(name: String) {
-               assertThat(templateContext["${name}Count"] as Int, equalTo(5))
+               assertThat(templateContext["${name}Count"] as Long, equalTo(5L))
                assertThat(templateContext["${name}Min"] as Long, equalTo(1L))
                assertThat(templateContext["${name}Max"] as Long, equalTo(10L))
                assertThat(templateContext["${name}Median"] as Double, equalTo(8.0))