-
Notifications
You must be signed in to change notification settings - Fork 212
/
metric_descriptor_cache.go
184 lines (164 loc) · 6.72 KB
/
metric_descriptor_cache.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
/*
Copyright 2017 Google Inc.
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 translator
import (
"github.com/golang/glog"
dto "github.com/prometheus/client_model/go"
v3 "google.golang.org/api/monitoring/v3"
"github.com/GoogleCloudPlatform/k8s-stackdriver/prometheus-to-sd/config"
)
// MetricDescriptorCache is responsible for fetching, creating and updating metric descriptors from the stackdriver.
type MetricDescriptorCache struct {
descriptors map[string]*v3.MetricDescriptor
broken map[string]bool
service *v3.Service
config *config.CommonConfig
component string
fresh bool
}
// NewMetricDescriptorCache creates empty metric descriptor cache for the given component.
func NewMetricDescriptorCache(service *v3.Service, config *config.CommonConfig, component string) *MetricDescriptorCache {
return &MetricDescriptorCache{
descriptors: make(map[string]*v3.MetricDescriptor),
broken: make(map[string]bool),
service: service,
config: config,
component: component,
fresh: false,
}
}
// IsMetricBroken returns true if this metric descriptor assumed to invalid (for examples it has too many labels).
func (cache *MetricDescriptorCache) IsMetricBroken(name string) bool {
broken, ok := cache.broken[name]
return ok && broken
}
// GetMetricNames returns a list of all metric names from the cache.
func (cache *MetricDescriptorCache) GetMetricNames() []string {
keys := make([]string, 0, len(cache.descriptors))
for k := range cache.descriptors {
keys = append(keys, k)
}
return keys
}
// MarkStale marks all records in the cache as stale until next Refresh() call.
func (cache *MetricDescriptorCache) MarkStale() {
cache.fresh = false
}
// ValidateMetricDescriptors checks if metric descriptors differs from the values kept in the cache.
// If the value has changed then metric family is marked is broken. Use this method to verify that
// metrics with prefix "container.googleapis.com" haven't changed.
func (cache *MetricDescriptorCache) ValidateMetricDescriptors(metrics map[string]*dto.MetricFamily, whitelisted []string) {
// Perform cache operation only if cache was recently refreshed. This is done mostly from the optimization point
// of view, we don't want to check all metric descriptors too often, as they should change rarely.
if !cache.fresh {
return
}
for _, metricFamily := range metrics {
if !isMetricWhitelisted(metricFamily.GetName(), whitelisted) {
continue
}
metricDescriptor, ok := cache.descriptors[metricFamily.GetName()]
if !ok {
continue
}
updatedMetricDescriptor := MetricFamilyToMetricDescriptor(cache.config, metricFamily, metricDescriptor)
if descriptorLabelSetChanged(metricDescriptor, updatedMetricDescriptor) {
cache.broken[metricFamily.GetName()] = true
metricFamilyDropped.WithLabelValues(cache.component, metricFamily.GetName()).Set(1.0)
glog.Warningf("Definition of the metric %s was changed and metric is not going to be pushed", metricFamily.GetName())
} else {
metricFamilyDropped.WithLabelValues(cache.component, metricFamily.GetName()).Set(0.0)
}
}
}
// UpdateMetricDescriptors iterates over all metricFamilies and updates metricDescriptors in the Stackdriver if required.
func (cache *MetricDescriptorCache) UpdateMetricDescriptors(metrics map[string]*dto.MetricFamily, whitelisted []string) {
// Perform cache operation only if cache was recently refreshed. This is done mostly from the optimization point
// of view, we don't want to check all metric descriptors too often, as they should change rarely.
if !cache.fresh {
return
}
for _, metricFamily := range metrics {
if isMetricWhitelisted(metricFamily.GetName(), whitelisted) {
cache.updateMetricDescriptorIfStale(metricFamily)
}
}
}
func isMetricWhitelisted(metric string, whitelisted []string) bool {
// Empty list means that we want to fetch all metrics.
if len(whitelisted) == 0 {
return true
}
for _, whitelistedMetric := range whitelisted {
if whitelistedMetric == metric {
return true
}
}
return false
}
// updateMetricDescriptorIfStale checks if descriptor created from MetricFamily object differs from the existing one
// and updates if needed.
func (cache *MetricDescriptorCache) updateMetricDescriptorIfStale(metricFamily *dto.MetricFamily) {
metricDescriptor, ok := cache.descriptors[metricFamily.GetName()]
updatedMetricDescriptor := MetricFamilyToMetricDescriptor(cache.config, metricFamily, metricDescriptor)
if !ok || descriptorChanged(metricDescriptor, updatedMetricDescriptor) {
if updateMetricDescriptorInStackdriver(cache.service, cache.config.GceConfig, updatedMetricDescriptor) {
cache.descriptors[metricFamily.GetName()] = updatedMetricDescriptor
} else {
cache.broken[metricFamily.GetName()] = true
}
}
}
func (cache *MetricDescriptorCache) getMetricDescriptor(metric string) *v3.MetricDescriptor {
value, ok := cache.descriptors[metric]
if !ok {
glog.V(4).Infof("Metric %s was not found in the cache for component %v", metric, cache.component)
}
return value
}
func descriptorChanged(original *v3.MetricDescriptor, checked *v3.MetricDescriptor) bool {
return descriptorDescriptionChanged(original, checked) || descriptorLabelSetChanged(original, checked)
}
func descriptorDescriptionChanged(original *v3.MetricDescriptor, checked *v3.MetricDescriptor) bool {
if original.Description != checked.Description {
glog.V(4).Infof("Description is different, %v != %v", original.Description, checked.Description)
return true
}
return false
}
func descriptorLabelSetChanged(original *v3.MetricDescriptor, checked *v3.MetricDescriptor) bool {
for _, label := range checked.Labels {
found := false
for _, labelFromOriginal := range original.Labels {
if label.Key == labelFromOriginal.Key {
found = true
break
}
}
if !found {
glog.V(4).Infof("Missing label %v in the original metric descriptor", label)
return true
}
}
return false
}
// Refresh function fetches all metric descriptors of all metrics defined for given component with a defined prefix
// and puts them into cache.
func (cache *MetricDescriptorCache) Refresh() {
metricDescriptors, err := getMetricDescriptors(cache.service, cache.config.GceConfig, cache.component)
if err == nil {
cache.descriptors = metricDescriptors
cache.broken = make(map[string]bool)
cache.fresh = true
}
}