-
-
Notifications
You must be signed in to change notification settings - Fork 199
/
prometheus.go
61 lines (49 loc) · 1.82 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
package metrics
import (
"github.com/eko/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 {
name string
collector *prometheus.GaugeVec
}
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 {
return &Prometheus{service, cacheCollector}
}
// 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.name, store, metric).Set(value)
}
// RecordFromCodec records metrics in prometheus by retrieving values from a codec instance
func (m *Prometheus) RecordFromCodec(codec codec.CodecInterface) {
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))
}