-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
188 lines (158 loc) · 4.76 KB
/
store.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package store
import (
"fmt"
"sync"
"github.com/DataDog/datadog-go/statsd"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/kube-state-metrics/pkg/metric"
)
// MetricsStore implements the k8s.io/client-go/tools/cache.Store
// interface. Instead of storing entire Kubernetes objects, it stores metrics
// generated based on those objects.
type MetricsStore struct {
// Protects metrics
mutex sync.RWMutex
// metrics is a map indexed by Kubernetes object id, containing a slice of
// metric families, containing a slice of metrics. We need to keep metrics
// grouped by metric families in order to zip families with their help text in
// MetricsStore.WriteAll().
metrics map[string]map[string]*metric.Family
// generateMetricsFunc generates metrics based on a given Kubernetes object
// and returns them grouped by metric family.
generateMetricsFunc func(interface{}) []metric.FamilyInterface
ddclient *statsd.Client
}
// NewMetricsStore returns a new MetricsStore
func NewMetricsStore(ddclient *statsd.Client, generateFunc func(interface{}) []metric.FamilyInterface) *MetricsStore {
return &MetricsStore{
ddclient: ddclient,
generateMetricsFunc: generateFunc,
metrics: make(map[string]map[string]*metric.Family),
}
}
// Implementing k8s.io/client-go/tools/cache.Store interface
// Add inserts adds to the MetricsStore by calling the metrics generator functions and
// adding the generated metrics to the metrics map that underlies the MetricStore.
func (s *MetricsStore) Add(obj interface{}) error {
o, err := meta.Accessor(obj)
if err != nil {
return err
}
s.mutex.Lock()
defer s.mutex.Unlock()
families := s.generateMetricsFunc(obj)
uid := string(o.GetUID())
if _, ok := s.metrics[uid]; !ok {
s.metrics[uid] = make(map[string]*metric.Family)
}
inspector := inspector{}
var errs []error
for _, f := range families {
f.Inspect(inspector.get)
s.metrics[uid][inspector.family.Name] = inspector.family
}
if len(errs) > 0 {
return errs[0]
}
return nil
}
type inspector struct {
family *metric.Family
}
func (i *inspector) get(f metric.Family) {
i.family = &f
}
func buildTags(uid string, metrics *metric.Metric) ([]string, error) {
tags := []string{fmt.Sprintf("uid:%s", uid)}
if len(metrics.LabelKeys) != len(metrics.LabelValues) {
return nil, fmt.Errorf("LabelKeys and LabelValues not same size")
}
for i := range metrics.LabelKeys {
tags = append(tags, fmt.Sprintf("%s:%s", metrics.LabelKeys[i], metrics.LabelValues[i]))
}
return tags, nil
}
// Update updates the existing entry in the MetricsStore.
func (s *MetricsStore) Update(obj interface{}) error {
// TODO: For now, just call Add, in the future one could check if the resource version changed?
return s.Add(obj)
}
// Delete deletes an existing entry in the MetricsStore.
func (s *MetricsStore) Delete(obj interface{}) error {
o, err := meta.Accessor(obj)
if err != nil {
return err
}
s.mutex.Lock()
defer s.mutex.Unlock()
delete(s.metrics, string(o.GetUID()))
return nil
}
// List implements the List method of the store interface.
func (s *MetricsStore) List() []interface{} {
return nil
}
// ListKeys implements the ListKeys method of the store interface.
func (s *MetricsStore) ListKeys() []string {
return nil
}
// Get implements the Get method of the store interface.
func (s *MetricsStore) Get(obj interface{}) (item interface{}, exists bool, err error) {
return nil, false, nil
}
// GetByKey implements the GetByKey method of the store interface.
func (s *MetricsStore) GetByKey(key string) (item interface{}, exists bool, err error) {
return nil, false, nil
}
// Replace will delete the contents of the store, using instead the
// given list.
func (s *MetricsStore) Replace(list []interface{}, _ string) error {
for _, o := range list {
err := s.Add(o)
if err != nil {
return err
}
}
return nil
}
// Resync implements the Resync method of the store interface.
func (s *MetricsStore) Resync() error {
return nil
}
// Push use to push metrics to dogstatsd
func (s *MetricsStore) Push() error {
s.mutex.RLock()
defer s.mutex.RUnlock()
var errs []error
for uid := range s.metrics {
for _, f := range s.metrics[uid] {
s.metrics[uid][f.Name] = f
switch f.Type {
case metric.Gauge:
for _, m := range f.Metrics {
tags, err := buildTags(uid, m)
if err != nil {
errs = append(errs, err)
continue
}
s.ddclient.Gauge(f.Name, m.Value, tags, 1)
}
case metric.Counter:
for _, m := range f.Metrics {
tags, err := buildTags(uid, m)
if err != nil {
errs = append(errs, err)
continue
}
s.ddclient.Count(f.Name, int64(m.Value), tags, 1)
}
default:
errs = append(errs, fmt.Errorf("metric type: %s not supported", f.Type))
}
}
}
if len(errs) > 0 {
return errs[0]
}
return nil
}