Skip to content

Instantly share code, notes, and snippets.

@liyuntao
Created April 15, 2018 02:58
Show Gist options
  • Save liyuntao/09a839ffcbde0ea377b6f17ed348cc4f to your computer and use it in GitHub Desktop.
Save liyuntao/09a839ffcbde0ea377b6f17ed348cc4f to your computer and use it in GitHub Desktop.
first tmp version for Promviz
const val UPSTREAM_SERVICE_NAME_HEADER = "x-client-name"
class VisualisationMetricHandler : Handler<RoutingContext> {
val httpGauge = Gauge.build()
.name("status:http_requests_total:rate5m")
.help("Requests per 5m")
.labelNames("service", "client", "status")
.register()
var lastTickInstant = Instant.now()
override fun handle(ctx: RoutingContext) {
ctx.addBodyEndHandler {
val fiveMinutesAgo = Instant.now().minus(Duration.ofMinutes(5))
val clientName = ctx.request().getHeader(UPSTREAM_SERVICE_NAME_HEADER) ?: "unknown"
if (lastTickInstant.isBefore(fiveMinutesAgo)) {
httpGauge.labels(SERVICE_NAME, clientName, "OK").set(0.0)
httpGauge.labels(SERVICE_NAME, clientName, "WARN").set(0.0)
httpGauge.labels(SERVICE_NAME, clientName, "ERROR").set(0.0)
lastTickInstant = Instant.now()
}
val status = when (ctx.response().statusCode) {
in 200 until 400 -> "OK"
in 400 until 500 -> "WARN"
else -> "ERROR"
}
httpGauge.labels(SERVICE_NAME, clientName, status).inc()
}
ctx.next()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment