Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2015 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 | "time" |
| 22 | |
| 23 | "github.com/prometheus/client_golang/prometheus" |
| 24 | ) |
| 25 | |
| 26 | var ( |
| 27 | cacheHitCounterOpts = prometheus.CounterOpts{ |
| 28 | Name: "etcd_helper_cache_hit_count", |
| 29 | Help: "Counter of etcd helper cache hits.", |
| 30 | } |
| 31 | cacheHitCounter = prometheus.NewCounter(cacheHitCounterOpts) |
| 32 | cacheMissCounterOpts = prometheus.CounterOpts{ |
| 33 | Name: "etcd_helper_cache_miss_count", |
| 34 | Help: "Counter of etcd helper cache miss.", |
| 35 | } |
| 36 | cacheMissCounter = prometheus.NewCounter(cacheMissCounterOpts) |
| 37 | cacheEntryCounterOpts = prometheus.CounterOpts{ |
| 38 | Name: "etcd_helper_cache_entry_count", |
| 39 | Help: "Counter of etcd helper cache entries. This can be different from etcd_helper_cache_miss_count " + |
| 40 | "because two concurrent threads can miss the cache and generate the same entry twice.", |
| 41 | } |
| 42 | cacheEntryCounter = prometheus.NewCounter(cacheEntryCounterOpts) |
| 43 | cacheGetLatency = prometheus.NewSummary( |
| 44 | prometheus.SummaryOpts{ |
| 45 | Name: "etcd_request_cache_get_latencies_summary", |
| 46 | Help: "Latency in microseconds of getting an object from etcd cache", |
| 47 | }, |
| 48 | ) |
| 49 | cacheAddLatency = prometheus.NewSummary( |
| 50 | prometheus.SummaryOpts{ |
| 51 | Name: "etcd_request_cache_add_latencies_summary", |
| 52 | Help: "Latency in microseconds of adding an object to etcd cache", |
| 53 | }, |
| 54 | ) |
| 55 | etcdRequestLatenciesSummary = prometheus.NewSummaryVec( |
| 56 | prometheus.SummaryOpts{ |
| 57 | Name: "etcd_request_latencies_summary", |
| 58 | Help: "Etcd request latency summary in microseconds for each operation and object type.", |
| 59 | }, |
| 60 | []string{"operation", "type"}, |
| 61 | ) |
| 62 | objectCounts = prometheus.NewGaugeVec( |
| 63 | prometheus.GaugeOpts{ |
| 64 | Name: "etcd_object_counts", |
| 65 | Help: "Number of stored objects at the time of last check split by kind.", |
| 66 | }, |
| 67 | []string{"resource"}, |
| 68 | ) |
| 69 | ) |
| 70 | |
| 71 | var registerMetrics sync.Once |
| 72 | |
| 73 | // Register all metrics. |
| 74 | func Register() { |
| 75 | // Register the metrics. |
| 76 | registerMetrics.Do(func() { |
| 77 | prometheus.MustRegister(cacheHitCounter) |
| 78 | prometheus.MustRegister(cacheMissCounter) |
| 79 | prometheus.MustRegister(cacheEntryCounter) |
| 80 | prometheus.MustRegister(cacheAddLatency) |
| 81 | prometheus.MustRegister(cacheGetLatency) |
| 82 | prometheus.MustRegister(etcdRequestLatenciesSummary) |
| 83 | prometheus.MustRegister(objectCounts) |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | func UpdateObjectCount(resourcePrefix string, count int64) { |
| 88 | objectCounts.WithLabelValues(resourcePrefix).Set(float64(count)) |
| 89 | } |
| 90 | |
| 91 | func RecordEtcdRequestLatency(verb, resource string, startTime time.Time) { |
| 92 | etcdRequestLatenciesSummary.WithLabelValues(verb, resource).Observe(float64(time.Since(startTime) / time.Microsecond)) |
| 93 | } |
| 94 | |
| 95 | func ObserveGetCache(startTime time.Time) { |
| 96 | cacheGetLatency.Observe(float64(time.Since(startTime) / time.Microsecond)) |
| 97 | } |
| 98 | |
| 99 | func ObserveAddCache(startTime time.Time) { |
| 100 | cacheAddLatency.Observe(float64(time.Since(startTime) / time.Microsecond)) |
| 101 | } |
| 102 | |
| 103 | func ObserveCacheHit() { |
| 104 | cacheHitCounter.Inc() |
| 105 | } |
| 106 | |
| 107 | func ObserveCacheMiss() { |
| 108 | cacheMissCounter.Inc() |
| 109 | } |
| 110 | |
| 111 | func ObserveNewEntry() { |
| 112 | cacheEntryCounter.Inc() |
| 113 | } |
| 114 | |
| 115 | func Reset() { |
| 116 | cacheHitCounter = prometheus.NewCounter(cacheHitCounterOpts) |
| 117 | cacheMissCounter = prometheus.NewCounter(cacheMissCounterOpts) |
| 118 | cacheEntryCounter = prometheus.NewCounter(cacheEntryCounterOpts) |
| 119 | // TODO: Reset cacheAddLatency. |
| 120 | // TODO: Reset cacheGetLatency. |
| 121 | etcdRequestLatenciesSummary.Reset() |
| 122 | } |