Add support for names vector and fill name in interface/node stats
[govpp.git] / adapter / stats_api.go
1 // Copyright (c) 2019 Cisco and/or its affiliates.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package adapter
16
17 import (
18         "fmt"
19 )
20
21 // StatsAPI provides connection to VPP stats API.
22 type StatsAPI interface {
23         // Connect establishes client connection to the stats API.
24         Connect() error
25
26         // Disconnect terminates client connection.
27         Disconnect() error
28
29         // ListStats lists names for all stats.
30         ListStats(patterns ...string) (statNames []string, err error)
31
32         // DumpStats dumps all stat entries.
33         DumpStats(patterns ...string) ([]*StatEntry, error)
34 }
35
36 // StatType represents type of stat directory and simply
37 // defines what type of stat data is stored in the stat entry.
38 type StatType int
39
40 const (
41         _                     StatType = 0
42         ScalarIndex           StatType = 1
43         SimpleCounterVector   StatType = 2
44         CombinedCounterVector StatType = 3
45         ErrorIndex            StatType = 4
46         NameVector            StatType = 5
47 )
48
49 func (d StatType) String() string {
50         switch d {
51         case ScalarIndex:
52                 return "ScalarIndex"
53         case SimpleCounterVector:
54                 return "SimpleCounterVector"
55         case CombinedCounterVector:
56                 return "CombinedCounterVector"
57         case ErrorIndex:
58                 return "ErrorIndex"
59         case NameVector:
60                 return "NameVector"
61         }
62         return fmt.Sprintf("UnknownStatType(%d)", d)
63 }
64
65 // StatEntry represents single stat entry. The type of stat stored in Data
66 // is defined by Type.
67 type StatEntry struct {
68         Name string
69         Type StatType
70         Data Stat
71 }
72
73 // Counter represents simple counter with single value.
74 type Counter uint64
75
76 // CombinedCounter represents counter with two values, for packet count and bytes count.
77 type CombinedCounter struct {
78         Packets Counter
79         Bytes   Counter
80 }
81
82 // Name represents string value stored under name vector.
83 type Name string
84
85 // ScalarStat represents stat for ScalarIndex.
86 type ScalarStat float64
87
88 // ErrorStat represents stat for ErrorIndex.
89 type ErrorStat uint64
90
91 // SimpleCounterStat represents stat for SimpleCounterVector.
92 // The outer array represents workers and the inner array represents interface/node/.. indexes.
93 // Values should be aggregated per interface/node for every worker.
94 type SimpleCounterStat [][]Counter
95
96 // CombinedCounterStat represents stat for CombinedCounterVector.
97 // The outer array represents workers and the inner array represents interface/node/.. indexes.
98 // Values should be aggregated per interface/node for every worker.
99 type CombinedCounterStat [][]CombinedCounter
100
101 // NameStat represents stat for NameVector.
102 type NameStat []Name
103
104 // Data represents some type of stat which is usually defined by StatType.
105 type Stat interface {
106         // isStat is unexported to limit implementations of Data interface to this package,
107         isStat()
108 }
109
110 func (ScalarStat) isStat()          {}
111 func (ErrorStat) isStat()           {}
112 func (SimpleCounterStat) isStat()   {}
113 func (CombinedCounterStat) isStat() {}
114 func (NameStat) isStat()            {}