package io.vertx.ext.dropwizard.impl;
import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.net.SocketAddress;
import io.vertx.ext.dropwizard.ThroughputMeter;
import io.vertx.ext.dropwizard.ThroughputTimer;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
abstract class HttpMetricsImpl extends TCPMetricsImpl {
private ThroughputTimer requests;
private ThroughputMeter[] responses;
private final Counter openWebSockets;
private Map<HttpMethod, ThroughputTimer> methodRequests;
public HttpMetricsImpl(MetricRegistry registry, String baseName, SocketAddress localAddress) {
super(registry, baseName);
openWebSockets = counter("open-websockets");
requests = throughputTimer("requests");
responses = new ThroughputMeter[]{
throughputMeter("responses-1xx"),
throughputMeter("responses-2xx"),
throughputMeter("responses-3xx"),
throughputMeter("responses-4xx"),
throughputMeter("responses-5xx")
};
methodRequests = new HashMap<>();
for (HttpMethod method : HttpMethod.values()) {
methodRequests.put(method, throughputTimer(method.toString().toLowerCase() + "-requests"));
}
}
protected WebSocketMetric createWebSocketMetric() {
openWebSockets.inc();
return null;
}
protected long end(HttpRequestMetric metric, int statusCode, Matcher matcher) {
if (closed) {
return 0;
}
String match = null;
if (metric.uri != null) {
match = matcher.matches(metric.uri);
}
long duration = System.nanoTime() - metric.requestBegin;
int responseStatus = statusCode / 100;
if (responseStatus >= 1 && responseStatus <= 5) {
responses[responseStatus - 1].mark();
}
requests.update(duration, TimeUnit.NANOSECONDS);
if (metric.method != null) {
methodRequests.get(metric.method).update(duration, TimeUnit.NANOSECONDS);
if (match != null) {
throughputTimer(metric.method.toString().toLowerCase() + "-requests", match).update(duration, TimeUnit.NANOSECONDS);
throughputMeter("responses" + "-" + responseStatus + "xx", match).mark();
}
} else if (match != null) {
throughputTimer("requests", match).update(duration, TimeUnit.NANOSECONDS);
throughputMeter("responses" + "-" + responseStatus + "xx", match).mark();
}
return duration;
}
protected void disconnect(WebSocketMetric metric) {
if (closed) {
return;
}
openWebSockets.dec();
}
}