forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stats_poller.go
117 lines (98 loc) · 3.09 KB
/
stats_poller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package telemetry
import (
"context"
"fmt"
"time"
"git.fd.io/govpp.git/api"
"github.com/gogo/status"
"github.com/ligato/cn-infra/logging"
"google.golang.org/grpc/codes"
"github.com/ligato/vpp-agent/api/configurator"
"github.com/ligato/vpp-agent/api/models/vpp"
vpp_interfaces "github.com/ligato/vpp-agent/api/models/vpp/interfaces"
"github.com/ligato/vpp-agent/plugins/telemetry/vppcalls"
)
type statsPollerServer struct {
handler vppcalls.TelemetryVppAPI
log logging.Logger
}
func (s *statsPollerServer) PollStats(req *configurator.PollStatsRequest, svr configurator.StatsPoller_PollStatsServer) error {
var pollSeq uint32
if req.PeriodSec == 0 {
return status.Error(codes.InvalidArgument, "PeriodSec must be greater than 0")
}
period := time.Duration(req.PeriodSec) * time.Second
tick := time.NewTicker(period)
defer tick.Stop()
s.log.Debugf("starting to poll stats every %v", period)
for {
pollSeq++
s.log.WithField("seq", pollSeq).Debugf("polling stats..")
vppStatsCh := make(chan vpp.Stats)
var vppStatsErr error
go func() {
vppStatsErr = s.streamVppStats(vppStatsCh)
close(vppStatsCh)
}()
for vppStats := range vppStatsCh {
VppStats := vppStats
s.log.Debugf("sending vpp stats: %v", VppStats)
if err := svr.Send(&configurator.PollStatsResponse{
PollSeq: pollSeq,
Stats: &configurator.Stats{
Stats: &configurator.Stats_VppStats{VppStats: &VppStats},
},
}); err != nil {
s.log.Errorf("sending stats failed: %v", err)
return nil
}
}
if vppStatsErr != nil {
s.log.Errorf("polling vpp stats failed: %v", vppStatsErr)
return vppStatsErr
}
<-tick.C
}
}
func (s *statsPollerServer) streamVppStats(ch chan vpp.Stats) error {
ctx := context.Background()
ifStats, err := s.handler.GetInterfaceStats(ctx)
if err != nil {
return err
} else if ifStats == nil {
return fmt.Errorf("interface stats not avaiable")
}
s.log.Debugf("streaming %d interface stats", len(ifStats.Interfaces))
for _, iface := range ifStats.Interfaces {
vppStats := vpp.Stats{
Interface: &vpp_interfaces.InterfaceStats{
Name: iface.InterfaceName,
Rx: convertInterfaceCombined(iface.Rx),
Tx: convertInterfaceCombined(iface.Tx),
RxUnicast: convertInterfaceCombined(iface.RxUnicast),
RxMulticast: convertInterfaceCombined(iface.RxMulticast),
RxBroadcast: convertInterfaceCombined(iface.RxBroadcast),
TxUnicast: convertInterfaceCombined(iface.TxUnicast),
TxMulticast: convertInterfaceCombined(iface.TxMulticast),
TxBroadcast: convertInterfaceCombined(iface.TxBroadcast),
RxError: iface.RxErrors,
TxError: iface.TxErrors,
RxNoBuf: iface.RxNoBuf,
RxMiss: iface.RxMiss,
Drops: iface.Drops,
Punts: iface.Punts,
Ip4: iface.IP4,
Ip6: iface.IP6,
Mpls: iface.Mpls,
},
}
ch <- vppStats
}
return nil
}
func convertInterfaceCombined(c api.InterfaceCounterCombined) *vpp_interfaces.InterfaceStats_CombinedCounter {
return &vpp_interfaces.InterfaceStats_CombinedCounter{
Bytes: c.Bytes,
Packets: c.Packets,
}
}