metrics.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package metrics
  14. import (
  15. "sync"
  16. "time"
  17. "github.com/prometheus/client_golang/prometheus"
  18. )
  19. var (
  20. cacheHitCounter = prometheus.NewCounter(
  21. prometheus.CounterOpts{
  22. Name: "etcd_helper_cache_hit_count",
  23. Help: "Counter of etcd helper cache hits.",
  24. },
  25. )
  26. cacheMissCounter = prometheus.NewCounter(
  27. prometheus.CounterOpts{
  28. Name: "etcd_helper_cache_miss_count",
  29. Help: "Counter of etcd helper cache miss.",
  30. },
  31. )
  32. cacheEntryCounter = prometheus.NewCounter(
  33. prometheus.CounterOpts{
  34. Name: "etcd_helper_cache_entry_count",
  35. Help: "Counter of etcd helper cache entries. This can be different from etcd_helper_cache_miss_count " +
  36. "because two concurrent threads can miss the cache and generate the same entry twice.",
  37. },
  38. )
  39. cacheGetLatency = prometheus.NewSummary(
  40. prometheus.SummaryOpts{
  41. Name: "etcd_request_cache_get_latencies_summary",
  42. Help: "Latency in microseconds of getting an object from etcd cache",
  43. },
  44. )
  45. cacheAddLatency = prometheus.NewSummary(
  46. prometheus.SummaryOpts{
  47. Name: "etcd_request_cache_add_latencies_summary",
  48. Help: "Latency in microseconds of adding an object to etcd cache",
  49. },
  50. )
  51. etcdRequestLatenciesSummary = prometheus.NewSummaryVec(
  52. prometheus.SummaryOpts{
  53. Name: "etcd_request_latencies_summary",
  54. Help: "Etcd request latency summary in microseconds for each operation and object type.",
  55. },
  56. []string{"operation", "type"},
  57. )
  58. )
  59. var registerMetrics sync.Once
  60. // Register all metrics.
  61. func Register() {
  62. // Register the metrics.
  63. registerMetrics.Do(func() {
  64. prometheus.MustRegister(cacheHitCounter)
  65. prometheus.MustRegister(cacheMissCounter)
  66. prometheus.MustRegister(cacheEntryCounter)
  67. prometheus.MustRegister(cacheAddLatency)
  68. prometheus.MustRegister(cacheGetLatency)
  69. prometheus.MustRegister(etcdRequestLatenciesSummary)
  70. })
  71. }
  72. func RecordEtcdRequestLatency(verb, resource string, startTime time.Time) {
  73. etcdRequestLatenciesSummary.WithLabelValues(verb, resource).Observe(float64(time.Since(startTime) / time.Microsecond))
  74. }
  75. func ObserveGetCache(startTime time.Time) {
  76. cacheGetLatency.Observe(float64(time.Since(startTime) / time.Microsecond))
  77. }
  78. func ObserveAddCache(startTime time.Time) {
  79. cacheAddLatency.Observe(float64(time.Since(startTime) / time.Microsecond))
  80. }
  81. func ObserveCacheHit() {
  82. cacheHitCounter.Inc()
  83. }
  84. func ObserveCacheMiss() {
  85. cacheMissCounter.Inc()
  86. }
  87. func ObserveNewEntry() {
  88. cacheEntryCounter.Inc()
  89. }
  90. func Reset() {
  91. cacheHitCounter.Set(0)
  92. cacheMissCounter.Set(0)
  93. cacheEntryCounter.Set(0)
  94. // TODO: Reset cacheAddLatency.
  95. // TODO: Reset cacheGetLatency.
  96. etcdRequestLatenciesSummary.Reset()
  97. }