-
Notifications
You must be signed in to change notification settings - Fork 2k
/
metrics_store.go
145 lines (114 loc) · 4.02 KB
/
metrics_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
/*
Copyright 2018 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package metricsstore
import (
"sync"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/types"
"k8s.io/kube-state-metrics/v2/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[types.UID][][]byte
// headers contains the header (TYPE and HELP) of each metric family. It is
// later on zipped with with their corresponding metric families in
// MetricStore.WriteAll().
headers []string
// generateMetricsFunc generates metrics based on a given Kubernetes object
// and returns them grouped by metric family.
generateMetricsFunc func(interface{}) []metric.FamilyInterface
}
// NewMetricsStore returns a new MetricsStore
func NewMetricsStore(headers []string, generateFunc func(interface{}) []metric.FamilyInterface) *MetricsStore {
return &MetricsStore{
generateMetricsFunc: generateFunc,
headers: headers,
metrics: map[types.UID][][]byte{},
}
}
// 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)
familyStrings := make([][]byte, len(families))
for i, f := range families {
familyStrings[i] = f.ByteSlice()
}
s.metrics[o.GetUID()] = familyStrings
return 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, 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(_ interface{}) (item interface{}, exists bool, err error) {
return nil, false, nil
}
// GetByKey implements the GetByKey method of the store interface.
func (s *MetricsStore) GetByKey(_ 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 {
s.mutex.Lock()
s.metrics = map[types.UID][][]byte{}
s.mutex.Unlock()
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
}