Prometheus client for Kotlin
- Kotlin multiplatform support
- Coroutines friendly (does not use
synchronized
at all) - Typed labels
- Nice DSL
- Ktor support out of the box
At the moment official prometheus java client has a bit more performance.
Add it into your build script:
build.gradle.kts
:
repositories {
maven {
url = uri("https://dl.bintray.com/evo/maven")
}
}
dependencies {
implementation("dev.evo", "prometheus-kt-ktor", "0.1.0-rc-2")
}
build.gradle
:
repositories {
maven {
url 'https://dl.bintray.com/evo/maven'
}
}
dependencies {
implementation "dev.evo:prometheus-kt-ktor:0.1.0-rc-2"
}
Create your own metrics:
import dev.evo.prometheus.LabelSet
import dev.evo.prometheus.PrometheusMetrics
import dev.evo.prometheus.jvm.DefaultJvmMetrics
class ProcessingLabels : LabelSet() {
var source by label()
}
object AppMetrics : PrometheusMetrics() {
val processedProducts by histogram("processed_products", logScale(0, 2)) {
ProcessingLabels()
}
val jvm by submetrics(DefaultJvmMetrics())
}
Use them in your application:
import kotlinx.coroutines.delay
suspend fun startProcessing() {
while (true) {
AppMetrics.processedProducts.measureTime({
source = "manual"
}) {
// Processing
delay(100)
}
}
}
Then expose them:
import dev.evo.prometheus.ktor.metricsModule
import dev.evo.prometheus.ktor.MetricsFeature
import io.ktor.server.engine.embeddedServer
import io.ktor.server.netty.Netty
suspend fun main(args: Array<String>) {
val metricsApp = embeddedServer(
Netty,
port = 9090,
module = {
metricsModule(MetricsFeature(AppMetrics))
}
)
.start(wait = false)
// Start processing
startProcessing()
metricsApp.stop(1000, 2000)
}
And finally watch them:
curl -X GET 'localhost:9090/metrics'
More samples at: https://github.com/anti-social/prometheus-kt/tree/master/samples
Just run them:
./gradlew --project-dir samples/processing-app run
./gradlew --project-dir samples/http-app run