dev.evo.prometheus:prometheus-kt-push-metadata

Prometheus Kotlin Client


License
Apache-2.0

Documentation

Build Status codecov Download

Prometheus-kt

Prometheus client for Kotlin

Motivation

  1. Kotlin multiplatform support
  2. Coroutines friendly (does not use synchronized at all)
  3. Typed labels
  4. Nice DSL
  5. Ktor support out of the box

At the moment official prometheus java client has a bit more performance.

Versions

Version Kotlin version
0.1.1 1.4.30
0.1.2 1.5.21
0.1.3 1.5.31
0.2.0 1.7.20
0.2.1 1.7.22
0.2.2 1.7.22
0.2.3 1.7.22

See Kotlin release details for kotlinx library versions compatibility.

How to use?

Add it into your build script:

build.gradle.kts:

repositories {
    mavenCentral()
}

dependencies {
    implementation("dev.evo.prometheus", "prometheus-kt-ktor", "0.2.3")
}

build.gradle:

repositories {
    mavenCentral()
}

dependencies {
    implementation "dev.evo.prometheus:prometheus-kt-ktor:0.2.3"
}

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 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(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