-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbinding.go
105 lines (95 loc) · 2.7 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
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
}
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
}