From 56cd46057beefb23a36942f24bce0566ef68a5fb Mon Sep 17 00:00:00 2001 From: Jing Chen Date: Wed, 17 Apr 2024 16:06:43 -0700 Subject: [PATCH] Export network stats via `runsc events`. The stats have been collected via cAdvisor which is being deprecated. This change dosen't migrate the metric. PiperOrigin-RevId: 625835710 --- runsc/boot/events.go | 27 ++++++++++++++++++++++++--- runsc/boot/loader.go | 23 +++++++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/runsc/boot/events.go b/runsc/boot/events.go index 98d5d792f..326563dde 100644 --- a/runsc/boot/events.go +++ b/runsc/boot/events.go @@ -23,6 +23,20 @@ import ( "gvisor.dev/gvisor/pkg/sentry/usage" ) +// NetworkInterface is the network statistics of the particular network interface +type NetworkInterface struct { + // Name is the name of the network interface. + Name string + RxBytes uint64 + RxPackets uint64 + RxErrors uint64 + RxDropped uint64 + TxBytes uint64 + TxPackets uint64 + TxErrors uint64 + TxDropped uint64 +} + // EventOut is the return type of the Event command. type EventOut struct { Event Event `json:"event"` @@ -42,9 +56,10 @@ type Event struct { // Stats is the runc specific stats structure for stability when encoding and // decoding stats. type Stats struct { - CPU CPU `json:"cpu"` - Memory Memory `json:"memory"` - Pids Pids `json:"pids"` + CPU CPU `json:"cpu"` + Memory Memory `json:"memory"` + Pids Pids `json:"pids"` + NetworkInterfaces []*NetworkInterface `json:"network_interfaces"` } // Pids contains stats on processes. @@ -127,6 +142,12 @@ func (cm *containerManager) Event(cid *string, out *EventOut) error { } out.Event.Data.Pids.Current = uint64(pids) + networkStats, err := cm.l.networkStats() + if err != nil { + return err + } + out.Event.Data.NetworkInterfaces = networkStats + numContainers := cm.l.containerCount() if numContainers == 0 { return fmt.Errorf("no container was found") diff --git a/runsc/boot/loader.go b/runsc/boot/loader.go index 28bd17bb2..fcc33e49f 100644 --- a/runsc/boot/loader.go +++ b/runsc/boot/loader.go @@ -1718,3 +1718,26 @@ func (l *Loader) pidsCount(cid string) (int, error) { } return l.k.TaskSet().Root.NumTasksPerContainer(cid), nil } + +func (l *Loader) networkStats() ([]*NetworkInterface, error) { + var stats []*NetworkInterface + stack := l.k.RootNetworkNamespace().Stack() + for _, i := range stack.Interfaces() { + var stat inet.StatDev + if err := stack.Statistics(&stat, i.Name); err != nil { + return nil, err + } + stats = append(stats, &NetworkInterface{ + Name: i.Name, + RxBytes: stat[0], + RxPackets: stat[1], + RxErrors: stat[2], + RxDropped: stat[3], + TxBytes: stat[8], + TxPackets: stat[9], + TxErrors: stat[10], + TxDropped: stat[11], + }) + } + return stats, nil +}