Created
April 15, 2018 02:58
-
-
Save liyuntao/09a839ffcbde0ea377b6f17ed348cc4f to your computer and use it in GitHub Desktop.
first tmp version for Promviz
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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