From 28df7c855784e784fb0e614c1cca0e565a7ef75f Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Mon, 22 Oct 2018 14:45:21 +0200 Subject: Introduce StatsAPI and it's initial implementation - this implementation is basically Go wrapper around VPP's vppapiclient C library Change-Id: I6f53dc3e228868834bf3a8a00c686ad05e22f3dd Signed-off-by: Ondrej Fabry --- examples/cmd/stats-api/stats_api.go | 66 +++++++++++++++++++++++++++++++ examples/cmd/stats-client/stats_client.go | 9 +++++ 2 files changed, 75 insertions(+) create mode 100644 examples/cmd/stats-api/stats_api.go (limited to 'examples/cmd') diff --git a/examples/cmd/stats-api/stats_api.go b/examples/cmd/stats-api/stats_api.go new file mode 100644 index 0000000..74454ab --- /dev/null +++ b/examples/cmd/stats-api/stats_api.go @@ -0,0 +1,66 @@ +// Copyright (c) 2018 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 ( + "fmt" + "log" + + "git.fd.io/govpp.git/adapter" + "git.fd.io/govpp.git/adapter/vppapiclient" +) + +// This example shows how to work with VPP's new stats API. + +func main() { + fmt.Println("Starting VPP stats API example..") + + client := vppapiclient.NewStatClient(vppapiclient.DefaultStatSocket) + + // connect to stats API + if err := client.Connect(); err != nil { + log.Fatalln("connecting client failed:", err) + } + defer client.Disconnect() + + // list stats by patterns + // you can omit parameters to list all stats + list, err := client.ListStats("/if", "/sys") + if err != nil { + log.Fatalln("listing stats failed:", err) + } + + for _, stat := range list { + fmt.Printf(" - %v\n", stat) + } + fmt.Printf("listed %d stats\n", len(list)) + + // dump stats by patterns to retrieve stats with the stats data + stats, err := client.DumpStats() + if err != nil { + log.Fatalln("dumping stats failed:", err) + } + + for _, stat := range stats { + switch data := stat.Data.(type) { + case adapter.ErrorStat: + if data == 0 { + // skip printing errors with 0 value + continue + } + } + fmt.Printf(" - %-25s %25v %+v\n", stat.Name, stat.Type, stat.Data) + } +} diff --git a/examples/cmd/stats-client/stats_client.go b/examples/cmd/stats-client/stats_client.go index f61f975..7a2c313 100644 --- a/examples/cmd/stats-client/stats_client.go +++ b/examples/cmd/stats-client/stats_client.go @@ -28,6 +28,15 @@ import ( "git.fd.io/govpp.git/examples/bin_api/stats" ) +/* + + IMPORTANT NOTICE! + + The binary API module stats used in this example will be deprecated in VPP 19.01. + VPP's new stats API should be used, you can find basic usage of new stats API in example stats-api. + +*/ + func main() { fmt.Println("Starting stats VPP client..") -- cgit 1.2.3-korg