Skip to content

Commit

Permalink
Add StatsPoller service for retrieving stats periodically
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
  • Loading branch information
ondrej-fabry committed Oct 4, 2019
1 parent 74f4db3 commit 5520045
Show file tree
Hide file tree
Showing 11 changed files with 1,055 additions and 152 deletions.
393 changes: 347 additions & 46 deletions api/configurator/configurator.pb.go

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions api/configurator/configurator.proto
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ message Notification {
}
}

message Stats {
oneof stats {
vpp.Stats vpp_stats = 1;
}
}

// Configurator provides basic operations for managing configuration
// and monitoring state.
service Configurator {
Expand Down Expand Up @@ -95,3 +101,20 @@ message NotificationResponse {
// Notification data
Notification notification = 2;
}

// StatsPoller provides operations for retrieving statistics.
service StatsPoller {
// PollStats is used for polling metrics using poll period.
rpc PollStats(PollStatsRequest) returns (stream PollStatsResponse) {};
}

message PollStatsRequest {
// PeriodSec defines polling period (in seconds)
uint32 period_sec = 1;
}


message PollStatsResponse {
uint32 poll_seq = 1;
Stats stats = 2;
}
353 changes: 299 additions & 54 deletions api/models/vpp/interfaces/state.pb.go

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions api/models/vpp/interfaces/state.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,35 @@ option (gogoproto.messagename_all) = true;

import "models/vpp/interfaces/interface.proto";

message InterfaceStats {
string name = 1;

message CombinedCounter {
uint64 packets = 1;
uint64 bytes = 2;
}
CombinedCounter rx = 2;
CombinedCounter tx = 3;

CombinedCounter rx_unicast = 4;
CombinedCounter rx_multicast = 5;
CombinedCounter rx_broadcast = 6;
CombinedCounter tx_unicast = 7;
CombinedCounter tx_multicast = 8;
CombinedCounter tx_broadcast = 9;

uint64 rx_error = 12;
uint64 tx_error = 13;

uint64 rx_no_buf = 10;
uint64 rx_miss = 11;
uint64 drops = 14;
uint64 punts = 15;
uint64 ip4 = 16;
uint64 ip6 = 17;
uint64 mpls = 18;
}

message InterfaceState {
string name = 1;
string internal_name = 2;
Expand Down
145 changes: 93 additions & 52 deletions api/models/vpp/vpp.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions api/models/vpp/vpp.proto
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ message ConfigData {
message Notification {
interfaces.InterfaceNotification interface = 1;
}

message Stats {
interfaces.InterfaceStats interface = 1;
}
116 changes: 116 additions & 0 deletions examples/grpc_vpp/stats_poller/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright (c) 2019 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"log"
"net"
"time"

"github.com/ligato/cn-infra/agent"
"github.com/ligato/cn-infra/infra"
"github.com/ligato/cn-infra/logging/logrus"
"github.com/namsral/flag"
"golang.org/x/net/context"
"google.golang.org/grpc"

"github.com/ligato/vpp-agent/api/configurator"
)

var (
address = flag.String("address", "localhost:9111", "address of GRPC server")
socketType = flag.String("socket-type", "tcp", "[tcp, tcp4, tcp6, unix, unixpacket]")
period = flag.Uint("period", 3, "Polling period (in seconds)")
)

func main() {
ep := &ExamplePlugin{}
ep.SetName("stats-poller-example")
ep.Setup()

a := agent.NewAgent(
agent.AllPlugins(ep),
)
if err := a.Run(); err != nil {
log.Fatal()
}
}

// ExamplePlugin demonstrates the use of grpc to watch on VPP notifications using vpp-agent.
type ExamplePlugin struct {
infra.PluginDeps

conn *grpc.ClientConn
}

// Init initializes example plugin.
func (p *ExamplePlugin) Init() (err error) {
// Set up connection to the server.
p.conn, err = grpc.Dial("unix",
grpc.WithInsecure(),
grpc.WithDialer(dialer(*socketType, *address, time.Second*3)))

if err != nil {
return err
}

client := configurator.NewStatsPollerClient(p.conn)

// Start stats poller.
go p.pollStats(client)

return err
}

// Get is an implementation of client-side statistics streaming.
func (p *ExamplePlugin) pollStats(client configurator.StatsPollerClient) {
logrus.DefaultLogger().Info("Polling every %v seconds..", *period)

req := &configurator.PollStatsRequest{
PeriodSec: uint32(*period),
}

ctx := context.Background()
stream, err := client.PollStats(ctx, req)
if err != nil {
p.Log.Fatalln("PollStats failed:", err)
}

var lastSeq uint32
for {
resp, err := stream.Recv()
if err != nil {
p.Log.Fatalln("Recv failed:", err)
}

if resp.PollSeq != lastSeq {
p.Log.Infof(" --- Poll sequence: %-3v", resp.PollSeq)
}
lastSeq = resp.PollSeq

vppStats := resp.GetStats().GetVppStats()
p.Log.Infof("VPP stats: %v", vppStats)
}
}

// Dialer for unix domain socket
func dialer(socket, address string, timeoutVal time.Duration) func(string, time.Duration) (net.Conn, error) {
return func(addr string, timeout time.Duration) (net.Conn, error) {
// Pass values
addr, timeout = address, timeoutVal
// Dial with timeout
return net.DialTimeout(socket, addr, timeoutVal)
}
}
File renamed without changes.
3 changes: 3 additions & 0 deletions plugins/telemetry/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package telemetry

import (
"github.com/ligato/cn-infra/rpc/grpc"
"github.com/ligato/cn-infra/rpc/prometheus"
"github.com/ligato/cn-infra/servicelabel"

"github.com/ligato/vpp-agent/plugins/govppmux"
)

Expand All @@ -17,6 +19,7 @@ func NewPlugin(opts ...Option) *Plugin {
p.ServiceLabel = &servicelabel.DefaultPlugin
p.GoVppmux = &govppmux.DefaultPlugin
p.Prometheus = &prometheus.DefaultPlugin
p.GRPC = &grpc.DefaultPlugin

for _, o := range opts {
o(p)
Expand Down
Loading

0 comments on commit 5520045

Please sign in to comment.