-
Notifications
You must be signed in to change notification settings - Fork 4
/
prometheusprovider.go
executable file
·83 lines (73 loc) · 2.44 KB
/
prometheusprovider.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
package metric
import (
"errors"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
//ErrMetricAlreadyExist is when trying to add a new metric but it collides with another named metric
ErrMetricAlreadyExist = errors.New("trying to add a metric that already exists, instead try to increment it")
// ErrMetricNotFound is when trying to increment a nonfound metric
ErrMetricNotFound = errors.New("trying to increment a missing metric")
)
// PrometheusProvider is a simple Provider for Prom metric
// for now only support Float64, maybe add more in the future
type PrometheusProvider struct {
Metrics map[string]*Metric `json:"metrics"`
// PromMetric is actually just a mirror of Metrics, its used to export the metric
// The reaason why we contain our own Metric aswell is because it seems hard to extract values from Prom package
PromMetrics map[string]prometheus.Counter `json:"-"`
sync.Mutex
}
// NewPrometheusProvider will generate a new metrics holder
func NewPrometheusProvider() *PrometheusProvider {
return &PrometheusProvider{
Metrics: make(map[string]*Metric, 0),
PromMetrics: make(map[string]prometheus.Counter, 0),
}
}
// AddMetric is used to add new metrics, or append to old metric
func (pp *PrometheusProvider) AddMetric(m *Metric) error {
if pp.Metrics == nil {
pp.Metrics = make(map[string]*Metric, 0)
}
if pp.PromMetrics == nil {
pp.PromMetrics = make(map[string]prometheus.Counter, 0)
}
if _, ok := pp.Metrics[m.Name]; ok {
return ErrMetricAlreadyExist
}
pp.Lock()
pp.Metrics[m.Name] = m
promCounter := promauto.NewCounter(prometheus.CounterOpts{
Name: m.Name,
Help: m.Description,
})
promCounter.Add(m.Value)
pp.PromMetrics[m.Name] = promCounter
pp.Unlock()
return nil
}
// IncrementMetric is used to increase value of metric
func (pp *PrometheusProvider) IncrementMetric(name string, value float64) error {
if pp.PromMetrics[name] == nil || pp.Metrics[name] == nil {
return ErrMetricNotFound
}
pp.Lock()
pp.PromMetrics[name].Add(value)
pp.Metrics[name].Value = pp.Metrics[name].Value + value
pp.Unlock()
return nil
}
// GetMetrics is used to extract all metrics
func (pp *PrometheusProvider) GetMetrics() map[string]*Metric {
return pp.Metrics
}
// GetMetric will return a metric if it exists, or nil if not
func (pp *PrometheusProvider) GetMetric(name string) *Metric {
if met, ok := pp.Metrics[name]; ok {
return met
}
return nil
}