mirror of
https://github.com/netbirdio/gvisor.git
synced 2026-05-22 17:12:49 -07:00
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
This commit is contained in:
+24
-3
@@ -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")
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user