com.lightningkite.lightningserver.metrics.MetricSettings.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of server-core Show documentation
Show all versions of server-core Show documentation
A set of tools to fill in/replace what Ktor is lacking in.
The newest version!
@file:UseContextualSerialization(Duration::class)
package com.lightningkite.lightningserver.metrics
import com.lightningkite.lightningdb.Database
import com.lightningkite.lightningserver.db.DatabaseSettings
import com.lightningkite.lightningserver.serverhealth.HealthStatus
import com.lightningkite.lightningserver.settings.Pluggable
import com.lightningkite.lightningserver.settings.Settings
import kotlinx.serialization.Serializable
import kotlinx.serialization.Transient
import kotlinx.serialization.UseContextualSerialization
import org.slf4j.LoggerFactory
import java.time.Duration
@Serializable
data class MetricSettings(
val url: String = "none",
val trackingByEntryPoint: Set = setOf(Metrics.executionTime.name),
val trackingTotalsOnly: Set = setOf()
) : () -> Metrics {
@Transient
val tracked = trackingTotalsOnly + trackingByEntryPoint
companion object : Pluggable() {
init {
register("none") {
object : Metrics {
override val settings: MetricSettings = it
override suspend fun report(events: List) {}
override suspend fun healthCheck(): HealthStatus =
HealthStatus(HealthStatus.Level.OK, additionalMessage = "No metrics reporting")
}
}
register("log") {
object : Metrics {
override val settings: MetricSettings = it
val logger = LoggerFactory.getLogger("Metrics")
override suspend fun report(events: List) {
events.forEach {
logger.debug("Logging metric event $it")
}
}
override suspend fun healthCheck(): HealthStatus =
HealthStatus(HealthStatus.Level.OK, additionalMessage = "Metrics only recorded in log")
}
}
register("db") {
DatabaseMetrics(it) {
(Settings.requirements[it.url.substringAfter("://")]?.invoke() as? Database)
?: DatabaseSettings(it.url.substringAfter("://")).invoke()
}
}
}
}
override fun invoke(): Metrics = parse(url.substringBefore("://"), this)
}