package io.vertx.micrometer.impl;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.net.impl.SocketAddressImpl;
import io.vertx.core.spi.metrics.DatagramSocketMetrics;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.MetricsDomain;
import io.vertx.micrometer.impl.meters.Counters;
import io.vertx.micrometer.impl.meters.Summaries;
class VertxDatagramSocketMetrics extends AbstractMetrics implements DatagramSocketMetrics {
private final Summaries bytesReceived;
private final Summaries bytesSent;
private final Counters errorCount;
private volatile String localAddress;
VertxDatagramSocketMetrics(MeterRegistry registry) {
super(registry, MetricsDomain.DATAGRAM_SOCKET);
bytesReceived = summaries("bytesReceived", "Total number of datagram bytes received", Label.LOCAL);
bytesSent = summaries("bytesSent", "Total number of datagram bytes sent");
errorCount = counters("errors", "Total number of datagram errors", Label.CLASS_NAME);
}
@Override
public void listening(String localName, SocketAddress localAddress) {
this.localAddress = Labels.fromAddress(new SocketAddressImpl(localAddress.port(), localName));
}
@Override
public void bytesRead(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
if (localAddress != null) {
bytesReceived.get(localAddress).record(numberOfBytes);
}
}
@Override
public void bytesWritten(Void socketMetric, SocketAddress remoteAddress, long numberOfBytes) {
bytesSent.get().record(numberOfBytes);
}
@Override
public void exceptionOccurred(Void socketMetric, SocketAddress remoteAddress, Throwable t) {
errorCount.get(t.getClass().getSimpleName()).increment();
}
@Override
public boolean isEnabled() {
return true;
}
@Override
public void close() {
}
}