-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
registry.go
333 lines (257 loc) · 8.12 KB
/
registry.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
package speed
import (
"fmt"
"regexp"
"sync"
"github.com/pkg/errors"
)
// Registry defines a valid set of instance domains and metrics
type Registry interface {
// checks if an instance domain of the passed name is already present or not
HasInstanceDomain(name string) bool
// checks if an metric of the passed name is already present or not
HasMetric(name string) bool
// returns the number of Metrics in the current registry
MetricCount() int
// returns the number of Values in the current registry
ValuesCount() int
// returns the number of Instance Domains in the current registry
InstanceDomainCount() int
// returns the number of instances across all instance domains in the current registry
InstanceCount() int
// returns the number of non null strings initialized in the current registry
StringCount() int
// adds a InstanceDomain object to the writer
AddInstanceDomain(InstanceDomain) error
// adds a InstanceDomain object after constructing it using passed name and instances
AddInstanceDomainByName(name string, instances []string) (InstanceDomain, error)
// adds a Metric object to the writer
AddMetric(Metric) error
// adds a Metric object after parsing the passed string for Instances and InstanceDomains
AddMetricByString(name string, val interface{}, t MetricType, s MetricSemantics, u MetricUnit) (Metric, error)
}
// PCPRegistry implements a registry for PCP as the client
type PCPRegistry struct {
instanceDomains map[string]*PCPInstanceDomain // a cache for instanceDomains
metrics map[string]PCPMetric // a cache for metrics
// locks
indomlock sync.RWMutex
metricslock sync.RWMutex
// offsets
instanceoffset int
indomoffset int
metricsoffset int
valuesoffset int
stringsoffset int
// counts
instanceCount int
valueCount int
stringcount int
mapped bool
version2 bool // a flag that maintains whether we need to write mmv version 2
}
// NewPCPRegistry creates a new PCPRegistry object
func NewPCPRegistry() *PCPRegistry {
return &PCPRegistry{
instanceDomains: make(map[string]*PCPInstanceDomain),
metrics: make(map[string]PCPMetric),
}
}
// InstanceCount returns the number of instances across all indoms in the registry
func (r *PCPRegistry) InstanceCount() int {
r.indomlock.RLock()
defer r.indomlock.RUnlock()
return r.instanceCount
}
// InstanceDomainCount returns the number of instance domains in the registry
func (r *PCPRegistry) InstanceDomainCount() int {
r.indomlock.RLock()
defer r.indomlock.RUnlock()
return len(r.instanceDomains)
}
// MetricCount returns the number of metrics in the registry
func (r *PCPRegistry) MetricCount() int {
r.metricslock.RLock()
defer r.metricslock.RUnlock()
return len(r.metrics)
}
// ValuesCount returns the number of values in the registry
func (r *PCPRegistry) ValuesCount() int { return r.valueCount }
// StringCount returns the number of strings in the registry
func (r *PCPRegistry) StringCount() int {
if r.version2 {
return r.stringcount + r.MetricCount() + r.InstanceCount()
}
return r.stringcount
}
// HasInstanceDomain returns true if the registry already has an indom of the specified name
func (r *PCPRegistry) HasInstanceDomain(name string) bool {
r.indomlock.RLock()
defer r.indomlock.RUnlock()
_, present := r.instanceDomains[name]
return present
}
// HasMetric returns true if the registry already has a metric of the specified name
func (r *PCPRegistry) HasMetric(name string) bool {
r.metricslock.RLock()
defer r.metricslock.RUnlock()
_, present := r.metrics[name]
return present
}
// AddInstanceDomain will add a new instance domain to the current registry
func (r *PCPRegistry) AddInstanceDomain(indom InstanceDomain) error {
if r.HasInstanceDomain(indom.Name()) {
return errors.New("InstanceDomain is already defined for the current registry")
}
r.indomlock.Lock()
defer r.indomlock.Unlock()
if r.mapped {
return errors.New("Cannot add an indom when a mapping is active")
}
r.instanceDomains[indom.Name()] = indom.(*PCPInstanceDomain)
r.instanceCount += indom.InstanceCount()
if !r.version2 {
for _, v := range indom.Instances() {
if len(v) > MaxV1NameLength {
r.version2 = true
}
}
}
if indom.(*PCPInstanceDomain).shortDescription != "" {
r.stringcount++
}
if indom.(*PCPInstanceDomain).longDescription != "" {
r.stringcount++
}
return nil
}
func (r *PCPRegistry) addMetric(m PCPMetric) {
r.metrics[m.Name()] = m
if len(m.Name()) > MaxV1NameLength && !r.version2 {
r.version2 = true
}
currentValues := 1
if m.Indom() != nil {
currentValues = m.Indom().InstanceCount()
}
r.valueCount += currentValues
if m.Type() == StringType {
r.stringcount += currentValues
}
if m.ShortDescription() != "" {
r.stringcount++
}
if m.LongDescription() != "" {
r.stringcount++
}
}
// AddMetric will add a new metric to the current registry
func (r *PCPRegistry) AddMetric(m Metric) error {
if r.mapped {
return errors.New("cannot add a metric when a mapping is active")
}
if r.HasMetric(m.Name()) {
return errors.New("metric is already defined for the current registry")
}
pcpm := m.(PCPMetric)
// if it is an indom metric
if pcpm.Indom() != nil && !r.HasInstanceDomain(pcpm.Indom().Name()) {
err := r.AddInstanceDomain(pcpm.Indom())
if err != nil {
return err
}
}
r.metricslock.Lock()
defer r.metricslock.Unlock()
r.addMetric(pcpm)
return nil
}
// AddInstanceDomainByName adds an instance domain using passed parameters
func (r *PCPRegistry) AddInstanceDomainByName(name string, instances []string) (InstanceDomain, error) {
if r.HasInstanceDomain(name) {
return nil, errors.New("The InstanceDomain already exists for this registry")
}
indom, err := NewPCPInstanceDomain(name, instances)
if err != nil {
return nil, err
}
err = r.AddInstanceDomain(indom)
if err != nil {
return nil, err
}
return indom, nil
}
const id = "[\\p{L}\\p{N}_]+"
var instancesPattern = fmt.Sprintf("(%v)((,\\s?(%v))*)", id, id)
var pattern = fmt.Sprintf("\\A((%v)(\\.%v)*?)(\\[(%v)\\])?((\\.%v)*)\\z", id, id, instancesPattern, id)
var ireg, _ = regexp.Compile(id)
var reg, _ = regexp.Compile(pattern)
func parseString(s string) (metric string, indom string, instances []string, err error) {
if !reg.MatchString(s) {
return "", "", nil, errors.New("Invalid String")
}
matches := reg.FindStringSubmatch(s)
n := len(matches)
indom = matches[1]
metric = indom + matches[n-2]
iarr := matches[5]
if iarr != "" {
instances = ireg.FindAllString(iarr, -1)
} else {
instances = nil
indom = ""
}
return
}
func (r *PCPRegistry) addSingletonMetricByString(name string, val interface{}, t MetricType, s MetricSemantics, u MetricUnit) (Metric, error) {
m, err := NewPCPSingletonMetric(val, name, t, s, u)
if err != nil {
return nil, err
}
err = r.AddMetric(m)
if err != nil {
return nil, err
}
return m, nil
}
func (r *PCPRegistry) addInstanceMetricByString(name string, val interface{}, indom string, instances []string, t MetricType, s MetricSemantics, u MetricUnit) (Metric, error) {
// instance metric
mp, ok := val.(Instances)
if !ok {
return nil, errors.New("to define an instance metric, a Instances type is required")
}
var (
id InstanceDomain
err error
)
if !r.HasInstanceDomain(indom) {
id, err = r.AddInstanceDomainByName(indom, instances)
if err != nil {
return nil, err
}
} else if r.instanceDomains[indom].MatchInstances(instances) {
id = r.instanceDomains[indom]
} else {
return nil, errors.Errorf("a different instance domain under the name %v already exists in the registry", indom)
}
m, err := NewPCPInstanceMetric(mp, name, id.(*PCPInstanceDomain), t, s, u)
if err != nil {
return nil, err
}
err = r.AddMetric(m)
if err != nil {
return nil, err
}
return m, nil
}
// AddMetricByString dynamically creates a PCPMetric
func (r *PCPRegistry) AddMetricByString(str string, val interface{}, t MetricType, s MetricSemantics, u MetricUnit) (Metric, error) {
metric, indom, instances, err := parseString(str)
if err != nil {
return nil, err
}
if instances == nil {
return r.addSingletonMetricByString(metric, val, t, s, u)
}
return r.addInstanceMetricByString(metric, val, indom, instances, t, s, u)
}