forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_poller.go
152 lines (130 loc) · 4.05 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package telemetry
import (
"context"
"fmt"
"time"
govppapi "git.fd.io/govpp.git/api"
"github.com/golang/protobuf/proto"
"go.ligato.io/cn-infra/v2/logging"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"go.ligato.io/vpp-agent/v3/plugins/telemetry/vppcalls"
"go.ligato.io/vpp-agent/v3/plugins/vpp/ifplugin/ifaceidx"
"go.ligato.io/vpp-agent/v3/proto/ligato/configurator"
"go.ligato.io/vpp-agent/v3/proto/ligato/vpp"
vpp_interfaces "go.ligato.io/vpp-agent/v3/proto/ligato/vpp/interfaces"
)
type statsPollerServer struct {
configurator.UnimplementedStatsPollerServiceServer
handler vppcalls.TelemetryVppAPI
ifIndex ifaceidx.IfaceMetadataIndex
log logging.Logger
}
func (s *statsPollerServer) PollStats(req *configurator.PollStatsRequest, svr configurator.StatsPollerService_PollStatsServer) error {
if req.GetPeriodSec() == 0 && req.GetNumPolls() > 1 {
return status.Error(codes.InvalidArgument, "period must be > 0 if number of polls is > 1")
}
if s.handler == nil {
return status.Errorf(codes.Unavailable, "VPP telemetry handler not available")
}
ctx := svr.Context()
streamStats := func(pollSeq uint32) (err error) {
vppStatsCh := make(chan *vpp.Stats)
go func() {
err = s.streamVppStats(ctx, vppStatsCh)
close(vppStatsCh)
}()
for vppStats := range vppStatsCh {
VppStats := proto.Clone(vppStats).(*vpp.Stats)
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 err != nil {
s.log.Errorf("polling vpp stats failed: %v", err)
return err
}
return nil
}
if req.GetPeriodSec() == 0 {
return streamStats(0)
}
period := time.Duration(req.GetPeriodSec()) * time.Second
s.log.Debugf("start polling stats every %v", period)
tick := time.NewTicker(period)
defer tick.Stop()
for pollSeq := uint32(1); ; pollSeq++ {
s.log.WithField("seq", pollSeq).Debugf("polling stats..")
if err := streamStats(pollSeq); err != nil {
return err
}
if req.GetNumPolls() > 0 && pollSeq >= req.GetNumPolls() {
s.log.Debugf("reached %d pollings", req.GetNumPolls())
return nil
}
select {
case <-tick.C:
// period passed
case <-ctx.Done():
return ctx.Err()
}
}
}
func (s *statsPollerServer) streamVppStats(ctx context.Context, ch chan *vpp.Stats) error {
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 {
name, _, exists := s.ifIndex.LookupBySwIfIndex(iface.InterfaceIndex)
if !exists {
// fallback to internal name
name = iface.InterfaceName
}
vppStats := &vpp.Stats{
Interface: &vpp_interfaces.InterfaceStats{
Name: name,
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,
},
}
select {
case ch <- vppStats:
// stats sent
case <-ctx.Done():
return ctx.Err()
}
}
return nil
}
func convertInterfaceCombined(c govppapi.InterfaceCounterCombined) *vpp_interfaces.InterfaceStats_CombinedCounter {
return &vpp_interfaces.InterfaceStats_CombinedCounter{
Bytes: c.Bytes,
Packets: c.Packets,
}
}