reflector_metrics.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. // MetricsProvider generates various metrics used by the reflector.
  39. type MetricsProvider interface {
  40. NewListsMetric(name string) CounterMetric
  41. NewListDurationMetric(name string) SummaryMetric
  42. NewItemsInListMetric(name string) SummaryMetric
  43. NewWatchesMetric(name string) CounterMetric
  44. NewShortWatchesMetric(name string) CounterMetric
  45. NewWatchDurationMetric(name string) SummaryMetric
  46. NewItemsInWatchMetric(name string) SummaryMetric
  47. NewLastResourceVersionMetric(name string) GaugeMetric
  48. }
  49. type noopMetricsProvider struct{}
  50. func (noopMetricsProvider) NewListsMetric(name string) CounterMetric { return noopMetric{} }
  51. func (noopMetricsProvider) NewListDurationMetric(name string) SummaryMetric { return noopMetric{} }
  52. func (noopMetricsProvider) NewItemsInListMetric(name string) SummaryMetric { return noopMetric{} }
  53. func (noopMetricsProvider) NewWatchesMetric(name string) CounterMetric { return noopMetric{} }
  54. func (noopMetricsProvider) NewShortWatchesMetric(name string) CounterMetric { return noopMetric{} }
  55. func (noopMetricsProvider) NewWatchDurationMetric(name string) SummaryMetric { return noopMetric{} }
  56. func (noopMetricsProvider) NewItemsInWatchMetric(name string) SummaryMetric { return noopMetric{} }
  57. func (noopMetricsProvider) NewLastResourceVersionMetric(name string) GaugeMetric {
  58. return noopMetric{}
  59. }
  60. var metricsFactory = struct {
  61. metricsProvider MetricsProvider
  62. setProviders sync.Once
  63. }{
  64. metricsProvider: noopMetricsProvider{},
  65. }
  66. // SetReflectorMetricsProvider sets the metrics provider
  67. func SetReflectorMetricsProvider(metricsProvider MetricsProvider) {
  68. metricsFactory.setProviders.Do(func() {
  69. metricsFactory.metricsProvider = metricsProvider
  70. })
  71. }