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) {
- addHistogram(templateContext, "sone.parsing.duration", "soneParsingDuration")
- addHistogram(templateContext, "sone.insert.duration", "soneInsertDuration")
+ metricsRegistry.histograms
+ .mapKeys { it.key to it.key.toI18n() }
+ .onEach { addHistogram(templateContext, it.key.first, it.key.second) }
+ .keys
+ .map(Pair<*, String>::second)
+ .let { templateContext["histogramKeys"] = it }
}
private fun addHistogram(templateContext: TemplateContext, metricName: String, variablePrefix: String) {
metricsRegistry.histogram(metricName).also { histogram ->
+ templateContext["${variablePrefix}I18n"] = variablePrefix.capitalizeFirst()
templateContext["${variablePrefix}Count"] = histogram.count
histogram.snapshot.also { snapshot ->
templateContext["${variablePrefix}Min"] = snapshot.min
}
}
+
+private fun String.toI18n() = split(".")
+ .mapIndexed { index, s -> if (index > 0) s.capitalizeFirst() else s }
+ .joinToString("")
+
+private fun String.capitalizeFirst() =
+ get(0).toUpperCase() + if (length > 0) {
+ substring(1)
+ } else {
+ ""
+ }
</tr>
</thead>
<tbody>
- <tr>
- <td><%= Page.Metrics.SoneInsertDuration.Title|l10n|html></td>
- <td class="numeric"><% soneInsertDurationCount|html></td>
- <td class="numeric"><% soneInsertDurationMin|duration scale=="μs"|html></td>
- <td class="numeric"><% soneInsertDurationMax|duration scale=="μs"|html></td>
- <td class="numeric"><% soneInsertDurationMean|duration scale=="μs"|html></td>
- <td class="numeric"><% soneInsertDurationMedian|duration scale=="μs"|html></td>
- <td class="numeric"><% soneInsertDurationPercentile75|duration scale=="μs"|html></td>
- <td class="numeric"><% soneInsertDurationPercentile95|duration scale=="μs"|html></td>
- <td class="numeric"><% soneInsertDurationPercentile98|duration scale=="μs"|html></td>
- <td class="numeric"><% soneInsertDurationPercentile99|duration scale=="μs"|html></td>
- <td class="numeric"><% soneInsertDurationPercentile999|duration scale=="μs"|html></td>
- </tr>
- <tr>
- <td><%= Page.Metrics.SoneParsingDuration.Title|l10n|html></td>
- <td class="numeric"><% soneParsingDurationCount|html></td>
- <td class="numeric"><% soneParsingDurationMin|duration scale=="μs"|html></td>
- <td class="numeric"><% soneParsingDurationMax|duration scale=="μs"|html></td>
- <td class="numeric"><% soneParsingDurationMean|duration scale=="μs"|html></td>
- <td class="numeric"><% soneParsingDurationMedian|duration scale=="μs"|html></td>
- <td class="numeric"><% soneParsingDurationPercentile75|duration scale=="μs"|html></td>
- <td class="numeric"><% soneParsingDurationPercentile95|duration scale=="μs"|html></td>
- <td class="numeric"><% soneParsingDurationPercentile98|duration scale=="μs"|html></td>
- <td class="numeric"><% soneParsingDurationPercentile99|duration scale=="μs"|html></td>
- <td class="numeric"><% soneParsingDurationPercentile999|duration scale=="μs"|html></td>
- </tr>
+ <%foreach histogramKeys histogramKey>
+ <tr>
+ <td><%= "Page.Metrics.<%= '<%histogramKey>I18n'|parse>.Title"|parse|l10n|html></td>
+ <td class="numeric"><% "<%histogramKey>Count"|parse|html></td>
+ <td class="numeric"><% "<%histogramKey>Min"|parse|duration scale=="μs"|html></td>
+ <td class="numeric"><% "<%histogramKey>Max"|parse|duration scale=="μs"|html></td>
+ <td class="numeric"><% "<%histogramKey>Mean"|parse|duration scale=="μs"|html></td>
+ <td class="numeric"><% "<%histogramKey>Median"|parse|duration scale=="μs"|html></td>
+ <td class="numeric"><% "<%histogramKey>Percentile75"|parse|duration scale=="μs"|html></td>
+ <td class="numeric"><% "<%histogramKey>Percentile95"|parse|duration scale=="μs"|html></td>
+ <td class="numeric"><% "<%histogramKey>Percentile98"|parse|duration scale=="μs"|html></td>
+ <td class="numeric"><% "<%histogramKey>Percentile99"|parse|duration scale=="μs"|html></td>
+ <td class="numeric"><% "<%histogramKey>Percentile999"|parse|duration scale=="μs"|html></td>
+ </tr>
+ <%/foreach>
</tbody>
</table>
}
@Test
- fun `metrics page lists stats about sone parsing durations`() {
- createHistogram("sone.parsing.duration")
+ fun `metrics page auto-converts histogram name`() {
+ createHistogram("sone.random.duration")
page.handleRequest(soneRequest, templateContext)
- verifyHistogram("soneParsingDuration")
+ verifyHistogram("soneRandomDuration")
}
@Test
- fun `metrics page lists stats about sone insert durations`() {
- createHistogram("sone.insert.duration")
+ @Suppress("UNCHECKED_CAST")
+ fun `metrics page stores histogram keys in template`() {
+ createHistogram("sone.random.duration2")
+ createHistogram("sone.random.duration1")
page.handleRequest(soneRequest, templateContext)
- verifyHistogram("soneInsertDuration")
+ assertThat(templateContext["histogramKeys"] as Iterable<String>, contains("soneRandomDuration1", "soneRandomDuration2"))
+ }
+
+ @Test
+ fun `metrics page stores i18n names for histogram keys`() {
+ createHistogram("sone.random.duration1")
+ page.handleRequest(soneRequest, templateContext)
+ assertThat(templateContext["soneRandomDuration1I18n"] as String, equalTo("SoneRandomDuration1"))
}
@Test