reflector_metrics.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright 2016 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. // This file provides abstractions for setting the provider (e.g., prometheus)
  14. // of metrics.
  15. package cache
  16. import (
  17. "sync"
  18. )
  19. // GaugeMetric represents a single numerical value that can arbitrarily go up
  20. // and down.
  21. type GaugeMetric interface {
  22. Set(float64)
  23. }
  24. // CounterMetric represents a single numerical value that only ever
  25. // goes up.
  26. type CounterMetric interface {
  27. Inc()
  28. }
  29. // SummaryMetric captures individual observations.
  30. type SummaryMetric interface {
  31. Observe(float64)
  32. }
  33. type noopMetric struct{}
  34. func (noopMetric) Inc() {}
  35. func (noopMetric) Dec() {}
  36. func (noopMetric) Observe(float64) {}
  37. func (noopMetric) Set(float64) {}
  38. type reflectorMetrics struct {
  39. numberOfLists CounterMetric
  40. listDuration SummaryMetric
  41. numberOfItemsInList SummaryMetric
  42. numberOfWatches CounterMetric
  43. numberOfShortWatches CounterMetric
  44. watchDuration SummaryMetric
  45. numberOfItemsInWatch SummaryMetric
  46. lastResourceVersion GaugeMetric
  47. }
  48. // MetricsProvider generates various metrics used by the reflector.
  49. type MetricsProvider interface {
  50. NewListsMetric(name string) CounterMetric
  51. NewListDurationMetric(name string) SummaryMetric
  52. NewItemsInListMetric(name string) SummaryMetric
  53. NewWatchesMetric(name string) CounterMetric
  54. NewShortWatchesMetric(name string) CounterMetric
  55. NewWatchDurationMetric(name string) SummaryMetric
  56. NewItemsInWatchMetric(name string) SummaryMetric
  57. NewLastResourceVersionMetric(name string) GaugeMetric
  58. }
  59. type noopMetricsProvider struct{}
  60. func (noopMetricsProvider) NewListsMetric(name string) CounterMetric { return noopMetric{} }
  61. func (noopMetricsProvider) NewListDurationMetric(name string) SummaryMetric { return noopMetric{} }
  62. func (noopMetricsProvider) NewItemsInListMetric(name string) SummaryMetric { return noopMetric{} }
  63. func (noopMetricsProvider) NewWatchesMetric(name string) CounterMetric { return noopMetric{} }
  64. func (noopMetricsProvider) NewShortWatchesMetric(name string) CounterMetric { return noopMetric{} }
  65. func (noopMetricsProvider) NewWatchDurationMetric(name string) SummaryMetric { return noopMetric{} }
  66. func (noopMetricsProvider) NewItemsInWatchMetric(name string) SummaryMetric { return noopMetric{} }
  67. func (noopMetricsProvider) NewLastResourceVersionMetric(name string) GaugeMetric {
  68. return noopMetric{}
  69. }
  70. var metricsFactory = struct {
  71. metricsProvider MetricsProvider
  72. setProviders sync.Once
  73. }{
  74. metricsProvider: noopMetricsProvider{},
  75. }
  76. func newReflectorMetrics(name string) *reflectorMetrics {
  77. var ret *reflectorMetrics
  78. if len(name) == 0 {
  79. return ret
  80. }
  81. return &reflectorMetrics{
  82. numberOfLists: metricsFactory.metricsProvider.NewListsMetric(name),
  83. listDuration: metricsFactory.metricsProvider.NewListDurationMetric(name),
  84. numberOfItemsInList: metricsFactory.metricsProvider.NewItemsInListMetric(name),
  85. numberOfWatches: metricsFactory.metricsProvider.NewWatchesMetric(name),
  86. numberOfShortWatches: metricsFactory.metricsProvider.NewShortWatchesMetric(name),
  87. watchDuration: metricsFactory.metricsProvider.NewWatchDurationMetric(name),
  88. numberOfItemsInWatch: metricsFactory.metricsProvider.NewItemsInWatchMetric(name),
  89. lastResourceVersion: metricsFactory.metricsProvider.NewLastResourceVersionMetric(name),
  90. }
  91. }
  92. // SetReflectorMetricsProvider sets the metrics provider
  93. func SetReflectorMetricsProvider(metricsProvider MetricsProvider) {
  94. metricsFactory.setProviders.Do(func() {
  95. metricsFactory.metricsProvider = metricsProvider
  96. })
  97. }