[+] Metrics (#95)

* Add actuator and micrometer

* update

* [-] Remove unused import

* [O] Make code less verbose

* format

* refactor

---------

Co-authored-by: Azalea <22280294+hykilpikonna@users.noreply.github.com>
This commit is contained in:
Menci
2024-12-13 05:47:31 +08:00
committed by GitHub
parent 8434842c65
commit c5dad11e5e
15 changed files with 225 additions and 54 deletions

View File

@@ -0,0 +1,45 @@
package icu.samnyan.aqua.spring
import io.micrometer.core.instrument.Counter
import io.micrometer.core.instrument.Timer
import java.util.concurrent.ConcurrentHashMap
import io.micrometer.core.instrument.Metrics as MMetrics
object Metrics {
fun counter(metricName: String, vararg pairs: Pair<String, Any>): Counter {
val expandedLabels = expandLabels(*pairs)
return cache.computeIfAbsent(MetricCacheKey(Counter::class.java, metricName, expandedLabels)) {
Counter
.builder(metricName)
.tags(*expandedLabels)
.register(MMetrics.globalRegistry)
} as Counter
}
fun timer(metricName: String, vararg pairs: Pair<String, Any>): Timer {
val expandedLabels = expandLabels(*pairs)
return cache.computeIfAbsent(MetricCacheKey(Timer::class.java, metricName, expandedLabels)) {
Timer
.builder(metricName)
.publishPercentiles(0.5, 0.75, 0.90, 0.95, 0.99)
.tags(*expandedLabels)
.register(MMetrics.globalRegistry)
} as Timer
}
private data class MetricCacheKey(
val type: Class<*>,
val metricName: String,
val expandedLabels: Array<String>,
)
private val cache = ConcurrentHashMap<MetricCacheKey, Any>()
private fun expandLabels(vararg pairs: Pair<String, Any>): Array<String> {
return pairs
.flatMap {
listOf(it.first, it.second.toString())
}
.toTypedArray()
}
}