Skip to content

Commit 362f26f

Browse files
authored
RSDK-9885: Add network stats to ftdc. (viamrobotics#4768)
1 parent 827a907 commit 362f26f

File tree

3 files changed

+85
-1
lines changed

3 files changed

+85
-1
lines changed

ftdc/cmd/parser.go

+8-1
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,10 @@ var ratioMetricToFields = map[string]ratioMetric{
156156
"SystemCPU": {"SystemCPUSecs", "ElapsedTimeSecs"},
157157
// PerSec ratios use an empty string denominator.
158158
"HeadersProcessedPerSec": {"HeadersProcessed", ""},
159+
"TxPacketsPerSec": {"TxPackets", ""},
160+
"RxPacketsPerSec": {"RxPackets", ""},
161+
"TxBytesPerSec": {"TxBytes", ""},
162+
"RxBytesPerSec": {"RxBytes", ""},
159163
}
160164

161165
// ratioReading is a reading of two metrics described by `ratioMetric`. This is what will be graphed.
@@ -359,7 +363,10 @@ func (gpw *gnuplotWriter) CompileAndClose() string {
359363
// We're making separate graphs instead of a single big graph. The graphs will be arranged in a
360364
// rectangle with 1 column and X rows. Where X is the number of metrics. Add some margins for
361365
// aesthetics.
362-
writelnf(gnuFile, "set multiplot layout %v,1 margins 0.05,0.9, 0.05,0.9 spacing screen 0, char 5", len(gpw.metricFiles))
366+
//
367+
// The first margin is the left-hand margin. We set it a bit bigger to allow for larger numbers
368+
// for labeling the Y-axis values.
369+
writelnf(gnuFile, "set multiplot layout %v,1 margins 0.10,0.9, 0.05,0.9 spacing screen 0, char 5", len(gpw.metricFiles))
363370

364371
// Axis labeling/formatting/type information.
365372
writeln(gnuFile, "set timefmt '%s'")

ftdc/sys/net.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package sys
2+
3+
import (
4+
"github.com/prometheus/procfs"
5+
)
6+
7+
type netStatser struct {
8+
fs procfs.FS
9+
}
10+
11+
// NewNetUsage returns an object that can interpreted as an `ftdc.Statser`.
12+
//
13+
//nolint:revive
14+
func NewNetUsage() (*netStatser, error) {
15+
fs, err := procfs.NewDefaultFS()
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
return &netStatser{fs}, nil
21+
}
22+
23+
type netDevLine struct {
24+
RxBytes uint64
25+
RxPackets uint64
26+
RxErrors uint64
27+
RxDropped uint64
28+
TxBytes uint64
29+
TxPackets uint64
30+
TxErrors uint64
31+
TxDropped uint64
32+
}
33+
34+
type ifaceStats struct {
35+
TxQueueLength uint64
36+
RxQueueLength uint64
37+
UsedSockets uint64
38+
Drops uint64
39+
}
40+
41+
type networkStats struct {
42+
Ifaces map[string]netDevLine
43+
TCP ifaceStats
44+
UDP ifaceStats
45+
}
46+
47+
func (netStatser *netStatser) Stats() any {
48+
ret := networkStats{
49+
Ifaces: make(map[string]netDevLine),
50+
}
51+
if dev, err := netStatser.fs.NetDev(); err == nil {
52+
for ifaceName, stats := range dev {
53+
ret.Ifaces[ifaceName] = netDevLine{
54+
stats.RxBytes, stats.RxPackets, stats.RxErrors, stats.RxDropped,
55+
stats.TxBytes, stats.TxPackets, stats.TxErrors, stats.TxDropped,
56+
}
57+
}
58+
}
59+
60+
if netTCPSummary, err := netStatser.fs.NetTCPSummary(); err == nil {
61+
ret.TCP.TxQueueLength = netTCPSummary.TxQueueLength
62+
ret.TCP.RxQueueLength = netTCPSummary.RxQueueLength
63+
ret.TCP.UsedSockets = netTCPSummary.UsedSockets
64+
}
65+
66+
if netUDPSummary, err := netStatser.fs.NetUDPSummary(); err == nil {
67+
ret.UDP.TxQueueLength = netUDPSummary.TxQueueLength
68+
ret.UDP.RxQueueLength = netUDPSummary.RxQueueLength
69+
ret.UDP.UsedSockets = netUDPSummary.UsedSockets
70+
ret.UDP.Drops = *netUDPSummary.Drops
71+
}
72+
73+
return ret
74+
}

robot/impl/local_robot.go

+3
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ func newWithResources(
377377
if statser, err := sys.NewSelfSysUsageStatser(); err == nil {
378378
ftdcWorker.Add("proc.viam-server", statser)
379379
}
380+
if statser, err := sys.NewNetUsage(); err == nil {
381+
ftdcWorker.Add("net", statser)
382+
}
380383
}
381384

382385
closeCtx, cancel := context.WithCancel(ctx)

0 commit comments

Comments
 (0)