-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcore.go
113 lines (100 loc) · 3.28 KB
/
core.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
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package metrics
import (
"fmt"
"sync"
"github.com/prometheus/client_golang/prometheus"
promproto "github.com/prometheus/client_model/go"
"go.uber.org/net/metrics/push"
)
const _defaultCollectionSize = 128
// A core is a collection of metrics. Uniqueness is enforced with two checks,
// explained in the documentation of the Root struct. The checks are slightly
// stricter than those used by the official Prometheus client.
//
// The test suite for metric uniqueness is well-commented and explores the
// consequences of these two rules.
type core struct {
sync.RWMutex
dimsByName map[string]string
ids map[string]struct{}
metrics []metric
gatherer prometheus.Gatherer
}
func newCore() *core {
c := &core{
dimsByName: make(map[string]string, _defaultCollectionSize),
ids: make(map[string]struct{}, _defaultCollectionSize),
metrics: make([]metric, 0, _defaultCollectionSize),
}
c.gatherer = prometheus.GathererFunc(func() ([]*promproto.MetricFamily, error) {
c.RLock()
protos := make([]*promproto.MetricFamily, 0, len(c.metrics))
for _, m := range c.metrics {
p := m.proto()
if p != nil && len(p.Metric) > 0 {
protos = append(protos, p)
}
}
c.RUnlock()
return protos, nil
})
return c
}
func (c *core) register(m metric) error {
id := newDigester()
defer id.free()
meta := m.describe()
meta.writeID(id)
c.Lock()
if existing, ok := c.dimsByName[*meta.Name]; ok && existing != meta.Dims {
c.Unlock()
return fmt.Errorf("a metric with name %q and different tag "+
"names is already registered", *meta.Name)
}
if _, ok := c.ids[string(id.digest())]; ok {
c.Unlock()
return fmt.Errorf("a metric with name %q and the same constant "+
"tag names and values is already registered: %q", *meta.Name, string(id.digest()))
}
c.dimsByName[*meta.Name] = meta.Dims
c.ids[string(id.digest())] = struct{}{}
c.metrics = append(c.metrics, m)
c.Unlock()
return nil
}
func (c *core) snapshot() *RootSnapshot {
c.RLock()
defer c.RUnlock()
s := &RootSnapshot{}
for _, m := range c.metrics {
s.add(m)
}
s.sort()
return s
}
func (c *core) push(target push.Target) {
c.RLock()
for _, m := range c.metrics {
m.push(target)
}
c.RUnlock()
}