dave
02/18/2018, 9:24 PMobject ReportHttpTransaction {
operator fun invoke(clock: Clock = Clock.systemUTC(), transactionLabeller: HttpTransactionLabeller = { it }, recordFn: (HttpTransaction) -> Unit): Filter = Filter { next ->
{
clock.instant().let { start ->
next(it).apply {
recordFn(transactionLabeller(HttpTransaction(it, this, between(start, clock.instant()))))
}
}
}
}
}
.
As for the actual problem that you're experiencing, here's an example which would do what you want...import org.http4k.core.Filter
import org.http4k.core.Method.GET
import <http://org.http4k.core.Method.POST|org.http4k.core.Method.POST>
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.ResponseFilters.ReportHttpTransaction
import org.http4k.routing.bind
import org.http4k.routing.routes
fun main(args: Array<String>) {
fun named(name: String): Filter = ReportHttpTransaction(
transactionLabeller = { it.label("routename", name) }) {
println("metrics get done here for ... " + it.labels)
}
val app = routes(
"/" bind GET to named("get route").then { _: Request -> Response(OK).body("get") },
"/" bind POST to named("post route").then { _: Request -> Response(OK).body("get") }
)
println(app(Request(GET, "/")))
println(app(Request(POST, "/")))
}