Giter Site home page Giter Site logo

prometheus-kt's Introduction

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.

How to use?

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

prometheus-kt's People

Contributors

anti-social avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.