| 1 |
/* |
| 2 |
Copyright 2021 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package metrics |
| 18 |
|
| 19 |
import ( |
| 20 |
"sync" |
| 21 |
|
| 22 |
compbasemetrics "k8s.io/component-base/metrics" |
| 23 |
"k8s.io/component-base/metrics/legacyregistry" |
| 24 |
) |
| 25 |
|
| 26 |
const ( |
| 27 |
namespace = "apiserver" |
| 28 |
subsystem = "watch_cache" |
| 29 |
) |
| 30 |
|
| 31 |
/* |
| 32 |
* By default, all the following metrics are defined as falling under |
| 33 |
* ALPHA stability level https://github.com/kubernetes/enhancements/blob/master/keps/sig-instrumentation/1209-metrics-stability/kubernetes-control-plane-metrics-stability.md#stability-classes) |
| 34 |
* |
| 35 |
* Promoting the stability level of the metric is a responsibility of the component owner, since it |
| 36 |
* involves explicitly acknowledging support for the metric across multiple releases, in accordance with |
| 37 |
* the metric stability policy. |
| 38 |
*/ |
| 39 |
var ( |
| 40 |
listCacheCount = compbasemetrics.NewCounterVec( |
| 41 |
&compbasemetrics.CounterOpts{ |
| 42 |
Namespace: namespace, |
| 43 |
Name: "cache_list_total", |
| 44 |
Help: "Number of LIST requests served from watch cache", |
| 45 |
StabilityLevel: compbasemetrics.ALPHA, |
| 46 |
}, |
| 47 |
[]string{"resource_prefix", "index"}, |
| 48 |
) |
| 49 |
listCacheNumFetched = compbasemetrics.NewCounterVec( |
| 50 |
&compbasemetrics.CounterOpts{ |
| 51 |
Namespace: namespace, |
| 52 |
Name: "cache_list_fetched_objects_total", |
| 53 |
Help: "Number of objects read from watch cache in the course of serving a LIST request", |
| 54 |
StabilityLevel: compbasemetrics.ALPHA, |
| 55 |
}, |
| 56 |
[]string{"resource_prefix", "index"}, |
| 57 |
) |
| 58 |
listCacheNumReturned = compbasemetrics.NewCounterVec( |
| 59 |
&compbasemetrics.CounterOpts{ |
| 60 |
Namespace: namespace, |
| 61 |
Name: "cache_list_returned_objects_total", |
| 62 |
Help: "Number of objects returned for a LIST request from watch cache", |
| 63 |
StabilityLevel: compbasemetrics.ALPHA, |
| 64 |
}, |
| 65 |
[]string{"resource_prefix"}, |
| 66 |
) |
| 67 |
InitCounter = compbasemetrics.NewCounterVec( |
| 68 |
&compbasemetrics.CounterOpts{ |
| 69 |
Namespace: namespace, |
| 70 |
Name: "init_events_total", |
| 71 |
Help: "Counter of init events processed in watch cache broken by resource type.", |
| 72 |
StabilityLevel: compbasemetrics.ALPHA, |
| 73 |
}, |
| 74 |
[]string{"resource"}, |
| 75 |
) |
| 76 |
|
| 77 |
EventsReceivedCounter = compbasemetrics.NewCounterVec( |
| 78 |
&compbasemetrics.CounterOpts{ |
| 79 |
Namespace: namespace, |
| 80 |
Subsystem: subsystem, |
| 81 |
Name: "events_received_total", |
| 82 |
Help: "Counter of events received in watch cache broken by resource type.", |
| 83 |
StabilityLevel: compbasemetrics.ALPHA, |
| 84 |
}, |
| 85 |
[]string{"resource"}, |
| 86 |
) |
| 87 |
|
| 88 |
EventsCounter = compbasemetrics.NewCounterVec( |
| 89 |
&compbasemetrics.CounterOpts{ |
| 90 |
Namespace: namespace, |
| 91 |
Subsystem: subsystem, |
| 92 |
Name: "events_dispatched_total", |
| 93 |
Help: "Counter of events dispatched in watch cache broken by resource type.", |
| 94 |
StabilityLevel: compbasemetrics.ALPHA, |
| 95 |
}, |
| 96 |
[]string{"resource"}, |
| 97 |
) |
| 98 |
|
| 99 |
TerminatedWatchersCounter = compbasemetrics.NewCounterVec( |
| 100 |
&compbasemetrics.CounterOpts{ |
| 101 |
Namespace: namespace, |
| 102 |
Name: "terminated_watchers_total", |
| 103 |
Help: "Counter of watchers closed due to unresponsiveness broken by resource type.", |
| 104 |
StabilityLevel: compbasemetrics.ALPHA, |
| 105 |
}, |
| 106 |
[]string{"resource"}, |
| 107 |
) |
| 108 |
|
| 109 |
watchCacheResourceVersion = compbasemetrics.NewGaugeVec( |
| 110 |
&compbasemetrics.GaugeOpts{ |
| 111 |
Namespace: namespace, |
| 112 |
Subsystem: subsystem, |
| 113 |
Name: "resource_version", |
| 114 |
Help: "Current resource version of watch cache broken by resource type.", |
| 115 |
StabilityLevel: compbasemetrics.ALPHA, |
| 116 |
}, |
| 117 |
[]string{"resource"}, |
| 118 |
) |
| 119 |
|
| 120 |
watchCacheCapacityIncreaseTotal = compbasemetrics.NewCounterVec( |
| 121 |
&compbasemetrics.CounterOpts{ |
| 122 |
Subsystem: subsystem, |
| 123 |
Name: "capacity_increase_total", |
| 124 |
Help: "Total number of watch cache capacity increase events broken by resource type.", |
| 125 |
StabilityLevel: compbasemetrics.ALPHA, |
| 126 |
}, |
| 127 |
[]string{"resource"}, |
| 128 |
) |
| 129 |
|
| 130 |
watchCacheCapacityDecreaseTotal = compbasemetrics.NewCounterVec( |
| 131 |
&compbasemetrics.CounterOpts{ |
| 132 |
Subsystem: subsystem, |
| 133 |
Name: "capacity_decrease_total", |
| 134 |
Help: "Total number of watch cache capacity decrease events broken by resource type.", |
| 135 |
StabilityLevel: compbasemetrics.ALPHA, |
| 136 |
}, |
| 137 |
[]string{"resource"}, |
| 138 |
) |
| 139 |
|
| 140 |
WatchCacheCapacity = compbasemetrics.NewGaugeVec( |
| 141 |
&compbasemetrics.GaugeOpts{ |
| 142 |
Subsystem: subsystem, |
| 143 |
Name: "capacity", |
| 144 |
Help: "Total capacity of watch cache broken by resource type.", |
| 145 |
StabilityLevel: compbasemetrics.ALPHA, |
| 146 |
}, |
| 147 |
[]string{"resource"}, |
| 148 |
) |
| 149 |
|
| 150 |
WatchCacheInitializations = compbasemetrics.NewCounterVec( |
| 151 |
&compbasemetrics.CounterOpts{ |
| 152 |
Namespace: namespace, |
| 153 |
Subsystem: subsystem, |
| 154 |
Name: "initializations_total", |
| 155 |
Help: "Counter of watch cache initializations broken by resource type.", |
| 156 |
StabilityLevel: compbasemetrics.ALPHA, |
| 157 |
}, |
| 158 |
[]string{"resource"}, |
| 159 |
) |
| 160 |
|
| 161 |
WatchCacheReadWait = compbasemetrics.NewHistogramVec( |
| 162 |
&compbasemetrics.HistogramOpts{ |
| 163 |
Namespace: namespace, |
| 164 |
Subsystem: subsystem, |
| 165 |
Name: "read_wait_seconds", |
| 166 |
Help: "Histogram of time spent waiting for a watch cache to become fresh.", |
| 167 |
StabilityLevel: compbasemetrics.ALPHA, |
| 168 |
Buckets: []float64{0.005, 0.025, 0.05, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.25, 1.5, 2, 3}, |
| 169 |
}, []string{"resource"}) |
| 170 |
|
| 171 |
ConsistentReadTotal = compbasemetrics.NewCounterVec( |
| 172 |
&compbasemetrics.CounterOpts{ |
| 173 |
Namespace: namespace, |
| 174 |
Subsystem: subsystem, |
| 175 |
Name: "consistent_read_total", |
| 176 |
Help: "Counter for consistent reads from cache.", |
| 177 |
StabilityLevel: compbasemetrics.ALPHA, |
| 178 |
}, []string{"resource", "success", "fallback"}) |
| 179 |
|
| 180 |
StorageConsistencyCheckTotal = compbasemetrics.NewCounterVec( |
| 181 |
&compbasemetrics.CounterOpts{ |
| 182 |
Namespace: namespace, |
| 183 |
Name: "storage_consistency_checks_total", |
| 184 |
Help: "Counter for status of consistency checks between etcd and watch cache", |
| 185 |
StabilityLevel: compbasemetrics.INTERNAL, |
| 186 |
}, []string{"resource", "status"}) |
| 187 |
) |
| 188 |
|
| 189 |
var registerMetrics sync.Once |
| 190 |
|
| 191 |
// Register all metrics. |
| 192 |
func Register() { |
| 193 |
// Register the metrics. |
| 194 |
registerMetrics.Do(func() { |
| 195 |
legacyregistry.MustRegister(listCacheCount) |
| 196 |
legacyregistry.MustRegister(listCacheNumFetched) |
| 197 |
legacyregistry.MustRegister(listCacheNumReturned) |
| 198 |
legacyregistry.MustRegister(InitCounter) |
| 199 |
legacyregistry.MustRegister(EventsReceivedCounter) |
| 200 |
legacyregistry.MustRegister(EventsCounter) |
| 201 |
legacyregistry.MustRegister(TerminatedWatchersCounter) |
| 202 |
legacyregistry.MustRegister(watchCacheResourceVersion) |
| 203 |
legacyregistry.MustRegister(watchCacheCapacityIncreaseTotal) |
| 204 |
legacyregistry.MustRegister(watchCacheCapacityDecreaseTotal) |
| 205 |
legacyregistry.MustRegister(WatchCacheCapacity) |
| 206 |
legacyregistry.MustRegister(WatchCacheInitializations) |
| 207 |
legacyregistry.MustRegister(WatchCacheReadWait) |
| 208 |
legacyregistry.MustRegister(ConsistentReadTotal) |
| 209 |
legacyregistry.MustRegister(StorageConsistencyCheckTotal) |
| 210 |
}) |
| 211 |
} |
| 212 |
|
| 213 |
// RecordListCacheMetrics notes various metrics of the cost to serve a LIST request |
| 214 |
func RecordListCacheMetrics(resourcePrefix, indexName string, numFetched, numReturned int) { |
| 215 |
listCacheCount.WithLabelValues(resourcePrefix, indexName).Inc() |
| 216 |
listCacheNumFetched.WithLabelValues(resourcePrefix, indexName).Add(float64(numFetched)) |
| 217 |
listCacheNumReturned.WithLabelValues(resourcePrefix).Add(float64(numReturned)) |
| 218 |
} |
| 219 |
|
| 220 |
// RecordResourceVersion sets the current resource version for a given resource type. |
| 221 |
func RecordResourceVersion(resourcePrefix string, resourceVersion uint64) { |
| 222 |
watchCacheResourceVersion.WithLabelValues(resourcePrefix).Set(float64(resourceVersion)) |
| 223 |
} |
| 224 |
|
| 225 |
// RecordsWatchCacheCapacityChange record watchCache capacity resize(increase or decrease) operations. |
| 226 |
func RecordsWatchCacheCapacityChange(objType string, old, new int) { |
| 227 |
WatchCacheCapacity.WithLabelValues(objType).Set(float64(new)) |
| 228 |
if old < new { |
| 229 |
watchCacheCapacityIncreaseTotal.WithLabelValues(objType).Inc() |
| 230 |
return |
| 231 |
} |
| 232 |
watchCacheCapacityDecreaseTotal.WithLabelValues(objType).Inc() |
| 233 |
} |