package io.vertx.micrometer.impl;
import io.micrometer.core.instrument.MeterRegistry;
import io.vertx.core.net.SocketAddress;
import io.vertx.core.spi.metrics.DatagramSocketMetrics;
import io.vertx.micrometer.Label;
import io.vertx.micrometer.MetricsDomain;
import io.vertx.micrometer.MetricsNaming;
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, MetricsNaming names) {
super(registry, MetricsDomain.DATAGRAM_SOCKET);
bytesReceived = summaries(names.getDatagramBytesRead(), "Total number of datagram bytes received", Label.LOCAL);
bytesSent = summaries(names.getDatagramBytesWritten(), "Total number of datagram bytes sent");
errorCount = counters(names.getDatagramErrorCount(), "Total number of datagram errors", Label.CLASS_NAME);
}
@Override
public void listening(String localName, SocketAddress localAddress) {
this.localAddress = Labels.address(localAddress, 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 void close() {
}
}