Micrometer

Installation (Gradle)

implementation group: "org.http4k", name: "http4k-metrics-micrometer", version: "3.280.0"

About

This module provides configurable Filters to provide metrics for http4k apps, plugging into the awesome Micrometer library.

Micrometer

Both Server and Client filters are available for recording request counts and latency, optionally overriding values for the metric names, descriptions and request identification.

package guide.modules.micrometer

import io.micrometer.core.instrument.simple.SimpleMeterRegistry
import org.http4k.client.ApacheClient
import org.http4k.core.Method.GET
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.then
import org.http4k.filter.ClientFilters
import org.http4k.filter.MicrometerMetrics
import org.http4k.filter.ServerFilters
import org.http4k.routing.bind
import org.http4k.routing.routes

fun main() {

    // this is a micrometer registry used mostly for testing - substitute the correct implementation.
    val registry = SimpleMeterRegistry()

    val server = routes("/metrics" bind GET to { Response(OK) })

    // apply filters to a server...
    val app = ServerFilters.MicrometerMetrics.RequestCounter(registry)
        .then(ServerFilters.MicrometerMetrics.RequestTimer(registry))
        .then(server)

    // ... or to a client
    val client =
        ClientFilters.MicrometerMetrics.RequestCounter(registry)
            .then(ClientFilters.MicrometerMetrics.RequestTimer(registry))
            .then(ApacheClient())

    // make some calls
    repeat((0..10).count()) {
        app(Request(GET, "/metrics"))
        client(Request(GET, "https://http4k.org"))
    }

    // see some results
    registry.forEachMeter { println("${it.id} ${it.measure().joinToString(",")}") }
}