blob: 96385f6e601a67bb40e64462282f6e7a4bd25843 [file] [log] [blame]
Matthias Andreas Benkard832a54e2019-01-29 09:27:38 +01001/*
2Copyright 2015 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package metrics
18
19import (
20 "sync"
21 "time"
22
23 "github.com/prometheus/client_golang/prometheus"
24)
25
26var (
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
71var registerMetrics sync.Once
72
73// Register all metrics.
74func 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
87func UpdateObjectCount(resourcePrefix string, count int64) {
88 objectCounts.WithLabelValues(resourcePrefix).Set(float64(count))
89}
90
91func RecordEtcdRequestLatency(verb, resource string, startTime time.Time) {
92 etcdRequestLatenciesSummary.WithLabelValues(verb, resource).Observe(float64(time.Since(startTime) / time.Microsecond))
93}
94
95func ObserveGetCache(startTime time.Time) {
96 cacheGetLatency.Observe(float64(time.Since(startTime) / time.Microsecond))
97}
98
99func ObserveAddCache(startTime time.Time) {
100 cacheAddLatency.Observe(float64(time.Since(startTime) / time.Microsecond))
101}
102
103func ObserveCacheHit() {
104 cacheHitCounter.Inc()
105}
106
107func ObserveCacheMiss() {
108 cacheMissCounter.Inc()
109}
110
111func ObserveNewEntry() {
112 cacheEntryCounter.Inc()
113}
114
115func 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}