Skip to content

Commit

Permalink
feat(monitor): add tcp out traffic and udp in traffic statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
zeusoo001 committed Aug 6, 2024
1 parent 54b0395 commit d6faee8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,19 @@ private void work() {
Metrics.histogramObserve(MetricKeys.Histogram.TCP_BYTES,
stats.getTcpInSize() - TCP_TRAFFIC_IN,
MetricLabels.Histogram.TRAFFIC_IN);

MetricsUtil.meterMark(MetricsKey.NET_TCP_OUT_TRAFFIC,
stats.getTcpOutSize() - TCP_TRAFFIC_OUT);
Metrics.histogramObserve(MetricKeys.Histogram.TCP_BYTES,
stats.getTcpOutSize() - TCP_TRAFFIC_OUT,
MetricLabels.Histogram.TRAFFIC_OUT);

MetricsUtil.meterMark(MetricsKey.NET_UDP_IN_TRAFFIC,
stats.getUdpInSize() - UDP_TRAFFIC_IN);
Metrics.histogramObserve(MetricKeys.Histogram.UDP_BYTES,
stats.getUdpInSize() - UDP_TRAFFIC_IN,
MetricLabels.Histogram.TRAFFIC_IN);

MetricsUtil.meterMark(MetricsKey.NET_UDP_OUT_TRAFFIC,
stats.getUdpOutSize() - UDP_TRAFFIC_OUT);
Metrics.histogramObserve(MetricKeys.Histogram.UDP_BYTES,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.tron.core.net.services;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.InetSocketAddress;

Expand All @@ -24,4 +26,38 @@ public void testOnDisconnect() {
Assert.assertEquals(Protocol.ReasonCode.UNKNOWN, statistics.getDisconnectReason());
}

@Test
public void testWork() throws Exception {
TronStatsManager manager = new TronStatsManager();
Field field1 = manager.getClass().getDeclaredField("TCP_TRAFFIC_IN");
field1.setAccessible(true);
field1.set(manager, 1L);

Field field2 = manager.getClass().getDeclaredField("TCP_TRAFFIC_OUT");
field2.setAccessible(true);
field2.set(manager, 1L);

Field field3 = manager.getClass().getDeclaredField("UDP_TRAFFIC_IN");
field3.setAccessible(true);
field3.set(manager, 1L);

Field field4 = manager.getClass().getDeclaredField("UDP_TRAFFIC_OUT");
field4.setAccessible(true);
field4.set(manager, 1L);

Assert.assertEquals(field1.get(manager), 1L);
Assert.assertEquals(field2.get(manager), 1L);
Assert.assertEquals(field3.get(manager), 1L);
Assert.assertEquals(field4.get(manager), 1L);

Method method = manager.getClass().getDeclaredMethod("work");
method.setAccessible(true);
method.invoke(manager);

Assert.assertEquals(field1.get(manager), 0L);
Assert.assertEquals(field2.get(manager), 0L);
Assert.assertEquals(field3.get(manager), 0L);
Assert.assertEquals(field4.get(manager), 0L);
}

}

0 comments on commit d6faee8

Please sign in to comment.