package io.vertx.ext.dropwizard.impl;
import com.codahale.metrics.Counter;
import com.codahale.metrics.Histogram;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.TCPMetrics;
import java.util.concurrent.TimeUnit;
class TCPMetricsImpl extends AbstractMetrics implements TCPMetrics<Long> {
private Counter openConnections;
private Timer connections;
private Counter bytesRead;
private Counter bytesWritten;
private Counter exceptions;
protected volatile boolean closed;
TCPMetricsImpl(MetricRegistry registry, String baseName) {
super(registry, baseName);
this.openConnections = counter("open-netsockets");
this.connections = timer("connections");
this.exceptions = counter("exceptions");
this.bytesRead = counter("bytes-read");
this.bytesWritten = counter("bytes-written");
}
@Override
public void close() {
this.closed = true;
removeAll();
}
@Override
public Long connected(SocketAddress remoteAddress, String remoteName) {
openConnections.inc();
if (remoteAddress != null) {
counter("open-connections", remoteAddress.host()).inc();
}
if (closed) {
removeAll();
}
return System.nanoTime();
}
@Override
public void disconnected(Long ctx, SocketAddress remoteAddress) {
openConnections.dec();
connections.update(System.nanoTime() - ctx, TimeUnit.NANOSECONDS);
if (remoteAddress != null) {
Counter counter = counter("open-connections", remoteAddress.host());
counter.dec();
if (counter.getCount() == 0) {
remove("open-connections", remoteAddress.host());
}
}
if (closed) {
removeAll();
}
}
@Override
public void bytesRead(Long socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
if (numberOfBytes > 0L) {
bytesRead.inc(numberOfBytes);
}
}
@Override
public void bytesWritten(Long socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
if (numberOfBytes > 0L) {
bytesWritten.inc(numberOfBytes);
}
}
@Override
public void exceptionOccurred(Long socketMetric, SocketAddress remoteAddress, Throwable t) {
exceptions.inc();
}
protected long connections() {
if (openConnections == null) return 0;
return openConnections.getCount();
}
static String addressName(SocketAddress address) {
if (address == null) return null;
return address.host() + ":" + address.port();
}
}