aboutsummaryrefslogtreecommitdiffstats
path: root/examples/cmd/stats-api/stats_api.go
blob: 74454ab5a81ed01cede63b2df1f933a7cf40db0b (plain)
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
// 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)
	}
}