Add statsclient - pure Go implementation for stats API
[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         "errors"
19         "fmt"
20 )
21
22 var (
23         ErrStatDirBusy  = errors.New("stat dir busy")
24         ErrStatDumpBusy = errors.New("stat dump busy")
25 )
26
27 var (
28         // DefaultStatsSocket is the default path for the VPP stat socket file.
29         DefaultStatsSocket = "/run/vpp/stats.sock"
30 )
31
32 // StatsAPI provides connection to VPP stats API.
33 type StatsAPI interface {
34         // Connect establishes client connection to the stats API.
35         Connect() error
36
37         // Disconnect terminates client connection.
38         Disconnect() error
39
40         // ListStats lists names for all stats.
41         ListStats(patterns ...string) (statNames []string, err error)
42
43         // DumpStats dumps all stat entries.
44         DumpStats(patterns ...string) ([]*StatEntry, error)
45 }
46
47 // StatType represents type of stat directory and simply
48 // defines what type of stat data is stored in the stat entry.
49 type StatType int
50
51 const (
52         _                     StatType = 0
53         ScalarIndex           StatType = 1
54         SimpleCounterVector   StatType = 2
55         CombinedCounterVector StatType = 3
56         ErrorIndex            StatType = 4
57         NameVector            StatType = 5
58 )
59
60 func (d StatType) String() string {
61         switch d {
62         case ScalarIndex:
63                 return "ScalarIndex"
64         case SimpleCounterVector:
65                 return "SimpleCounterVector"
66         case CombinedCounterVector:
67                 return "CombinedCounterVector"
68         case ErrorIndex:
69                 return "ErrorIndex"
70         case NameVector:
71                 return "NameVector"
72         }
73         return fmt.Sprintf("UnknownStatType(%d)", d)
74 }
75
76 // StatEntry represents single stat entry. The type of stat stored in Data
77 // is defined by Type.
78 type StatEntry struct {
79         Name string
80         Type StatType
81         Data Stat
82 }
83
84 // Counter represents simple counter with single value.
85 type Counter uint64
86
87 // CombinedCounter represents counter with two values, for packet count and bytes count.
88 type CombinedCounter struct {
89         Packets Counter
90         Bytes   Counter
91 }
92
93 // Name represents string value stored under name vector.
94 type Name string
95
96 // ScalarStat represents stat for ScalarIndex.
97 type ScalarStat float64
98
99 // ErrorStat represents stat for ErrorIndex.
100 type ErrorStat Counter
101
102 // SimpleCounterStat represents stat for SimpleCounterVector.
103 // The outer array represents workers and the inner array represents interface/node/.. indexes.
104 // Values should be aggregated per interface/node for every worker.
105 type SimpleCounterStat [][]Counter
106
107 // CombinedCounterStat represents stat for CombinedCounterVector.
108 // The outer array represents workers and the inner array represents interface/node/.. indexes.
109 // Values should be aggregated per interface/node for every worker.
110 type CombinedCounterStat [][]CombinedCounter
111
112 // NameStat represents stat for NameVector.
113 type NameStat []Name
114
115 // Data represents some type of stat which is usually defined by StatType.
116 type Stat interface {
117         // isStat is unexported to limit implementations of Data interface to this package,
118         isStat()
119 }
120
121 func (ScalarStat) isStat()          {}
122 func (ErrorStat) isStat()           {}
123 func (SimpleCounterStat) isStat()   {}
124 func (CombinedCounterStat) isStat() {}
125 func (NameStat) isStat()            {}