Introduce higer-level API for retrieving statistics
[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                    = 1
43         SimpleCounterVector            = 2
44         CombinedCounterVector          = 3
45         ErrorIndex                     = 4
46 )
47
48 func (d StatType) String() string {
49         switch d {
50         case ScalarIndex:
51                 return "ScalarIndex"
52         case SimpleCounterVector:
53                 return "SimpleCounterVector"
54         case CombinedCounterVector:
55                 return "CombinedCounterVector"
56         case ErrorIndex:
57                 return "ErrorIndex"
58         }
59         return fmt.Sprintf("UnknownStatType(%d)", d)
60 }
61
62 // StatEntry represents single stat entry. The type of stat stored in Data
63 // is defined by Type.
64 type StatEntry struct {
65         Name string
66         Type StatType
67         Data Stat
68 }
69
70 // Counter represents simple counter with single value.
71 type Counter uint64
72
73 // CombinedCounter represents counter with two values, for packet count and bytes count.
74 type CombinedCounter struct {
75         Packets Counter
76         Bytes   Counter
77 }
78
79 // ScalarStat represents stat for ScalarIndex.
80 type ScalarStat float64
81
82 // ErrorStat represents stat for ErrorIndex.
83 type ErrorStat uint64
84
85 // SimpleCounterStat represents stat for SimpleCounterVector.
86 // The outer array represents workers and the inner array represents interface/node/.. indexes.
87 // Values should be aggregated per interface/node for every worker.
88 type SimpleCounterStat [][]Counter
89
90 // CombinedCounterStat represents stat for CombinedCounterVector.
91 // The outer array represents workers and the inner array represents interface/node/.. indexes.
92 // Values should be aggregated per interface/node for every worker.
93 type CombinedCounterStat [][]CombinedCounter
94
95 // Data represents some type of stat which is usually defined by StatType.
96 type Stat interface {
97         // isStat is unexported to limit implementations of Data interface to this package,
98         isStat()
99 }
100
101 func (ScalarStat) isStat()          {}
102 func (ErrorStat) isStat()           {}
103 func (SimpleCounterStat) isStat()   {}
104 func (CombinedCounterStat) isStat() {}