This repository has been archived by the owner on Oct 6, 2020. It is now read-only.
forked from eko/gocache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
77 lines (62 loc) · 2.22 KB
/
prometheus.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
package metrics
import (
"github.com/CDNA-Technologies/gocache/codec"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
const (
namespaceCache = "cache"
)
var (
cacheCollector *prometheus.GaugeVec = initCacheCollector(namespaceCache)
)
// Prometheus represents the prometheus struct for collecting metrics
type Prometheus struct {
service string
collector *prometheus.GaugeVec
codecChannel chan codec.CodecInterface
}
func initCacheCollector(namespace string) *prometheus.GaugeVec {
c := promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "collector",
Namespace: namespace,
Help: "This represent the number of items in cache",
},
[]string{"service", "store", "metric"},
)
return c
}
// NewPrometheus initializes a new prometheus metric instance
func NewPrometheus(service string) *Prometheus {
prometheus := &Prometheus{
service: service,
collector: cacheCollector,
codecChannel: make(chan codec.CodecInterface, 10000),
}
go prometheus.recorder()
return prometheus
}
// Record records a metric in prometheus by specyfing the store name, metric name and value
func (m *Prometheus) record(store, metric string, value float64) {
m.collector.WithLabelValues(m.service, store, metric).Set(value)
}
// Recorder records metrics in prometheus by retrieving values from the codec channel
func (m *Prometheus) recorder() {
for codec := range m.codecChannel {
stats := codec.GetStats()
storeType := codec.GetStore().GetType()
m.record(storeType, "hit_count", float64(stats.Hits))
m.record(storeType, "miss_count", float64(stats.Miss))
m.record(storeType, "set_success", float64(stats.SetSuccess))
m.record(storeType, "set_error", float64(stats.SetError))
m.record(storeType, "delete_success", float64(stats.DeleteSuccess))
m.record(storeType, "delete_error", float64(stats.DeleteError))
m.record(storeType, "invalidate_success", float64(stats.InvalidateSuccess))
m.record(storeType, "invalidate_error", float64(stats.InvalidateError))
}
}
// RecordFromCodec sends the given codec into the codec channel to be read from recorder
func (m *Prometheus) RecordFromCodec(codec codec.CodecInterface) {
m.codecChannel <- codec
}