-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbinding.go
138 lines (122 loc) · 3.47 KB
/
binding.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
package metricsprometheus
import (
"strings"
logging "github.com/ipfs/go-log/v2"
metrics "github.com/ipfs/go-metrics-interface"
pro "github.com/prometheus/client_golang/prometheus"
)
var log logging.EventLogger = logging.Logger("metrics-prometheus")
func Inject() error {
return metrics.InjectImpl(newCreator)
}
func newCreator(name, helptext string) metrics.Creator {
return &creator{
name: strings.Replace(name, ".", "_", -1),
helptext: helptext,
}
}
var _ metrics.Creator = &creator{}
type creator struct {
name string
helptext string
}
func (c *creator) Counter() metrics.Counter {
res := pro.NewCounter(pro.CounterOpts{
Name: c.name,
Help: c.helptext,
})
err := pro.Register(res)
if err != nil {
if registered, ok := err.(pro.AlreadyRegisteredError); ok {
if existing, ok := registered.ExistingCollector.(pro.Counter); ok {
log.Warnf("using existing prometheus collector: %s", c.name)
return existing
}
}
log.Errorf("Registering prometheus collector, name: %s, error: %s", c.name, err.Error())
}
return res
}
type counterVec struct {
*pro.CounterVec
}
func (cv *counterVec) WithLabelValues(lvs ...string) metrics.Counter {
return cv.CounterVec.WithLabelValues(lvs...)
}
func (c *creator) CounterVec(labelNames []string) metrics.CounterVec {
res := pro.NewCounterVec(pro.CounterOpts{
Name: c.name,
Help: c.helptext,
}, labelNames)
err := pro.Register(res)
if err != nil {
if registered, ok := err.(pro.AlreadyRegisteredError); ok {
if existing, ok := registered.ExistingCollector.(*pro.CounterVec); ok {
log.Warnf("using existing prometheus collector: %s", c.name)
return &counterVec{
CounterVec: existing,
}
}
}
log.Errorf("Registering prometheus collector, name: %s, error: %s", c.name, err.Error())
}
return &counterVec{
CounterVec: res,
}
}
func (c *creator) Gauge() metrics.Gauge {
res := pro.NewGauge(pro.GaugeOpts{
Name: c.name,
Help: c.helptext,
})
err := pro.Register(res)
if err != nil {
if registered, ok := err.(pro.AlreadyRegisteredError); ok {
if existing, ok := registered.ExistingCollector.(pro.Gauge); ok {
log.Warnf("using existing prometheus collector: %s", c.name)
return existing
}
}
log.Errorf("Registering prometheus collector, name: %s, error: %s", c.name, err.Error())
}
return res
}
func (c *creator) Histogram(buckets []float64) metrics.Histogram {
res := pro.NewHistogram(pro.HistogramOpts{
Name: c.name,
Help: c.helptext,
Buckets: buckets,
})
err := pro.Register(res)
if err != nil {
if registered, ok := err.(pro.AlreadyRegisteredError); ok {
if existing, ok := registered.ExistingCollector.(pro.Histogram); ok {
log.Warnf("using existing prometheus collector: %s", c.name)
return existing
}
}
log.Errorf("Registering prometheus collector, name: %s, error: %s", c.name, err.Error())
}
return res
}
func (c *creator) Summary(opts metrics.SummaryOpts) metrics.Summary {
res := pro.NewSummary(pro.SummaryOpts{
Name: c.name,
Help: c.helptext,
Objectives: opts.Objectives,
MaxAge: opts.MaxAge,
AgeBuckets: opts.AgeBuckets,
BufCap: opts.BufCap,
})
err := pro.Register(res)
if err != nil {
if registered, ok := err.(pro.AlreadyRegisteredError); ok {
if existing, ok := registered.ExistingCollector.(pro.Summary); ok {
log.Warn("using existing prometheus collector: %s", c.name)
return existing
}
}
log.Errorf("Registering prometheus collector, name: %s, error: %s", c.name, err.Error())
}
return res
}