metrics.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. Copyright © The ESO 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. https://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 clientmanager
  14. import (
  15. "errors"
  16. "fmt"
  17. "github.com/prometheus/client_golang/prometheus"
  18. "sigs.k8s.io/controller-runtime/pkg/metrics"
  19. )
  20. var (
  21. // ClientManager gauges.
  22. clientsCachedTotal = prometheus.NewGaugeVec(
  23. prometheus.GaugeOpts{
  24. Name: "clientmanager_clients_cached_total",
  25. Help: "Total number of cached provider clients",
  26. },
  27. []string{"provider_type"},
  28. )
  29. // ClientManager counters.
  30. cacheHitsTotal = prometheus.NewCounterVec(
  31. prometheus.CounterOpts{
  32. Name: "clientmanager_cache_hits_total",
  33. Help: "Total number of client cache hits",
  34. },
  35. []string{"provider_type"},
  36. )
  37. cacheInvalidationsTotal = prometheus.NewCounterVec(
  38. prometheus.CounterOpts{
  39. Name: "clientmanager_cache_invalidations_total",
  40. Help: "Total number of client cache invalidations",
  41. },
  42. []string{"provider_type", "reason"},
  43. )
  44. )
  45. // Metrics provides test hooks for client manager metrics.
  46. type Metrics interface {
  47. RecordCacheHit(providerType string)
  48. RecordCacheMiss(providerType string)
  49. RecordCacheInvalidation(providerType, reason string)
  50. UpdateCachedClients(providerType string, count int)
  51. }
  52. // defaultMetrics implements Metrics using Prometheus.
  53. type defaultMetrics struct{}
  54. // RecordCacheHit records a cache hit.
  55. func (m *defaultMetrics) RecordCacheHit(providerType string) {
  56. cacheHitsTotal.WithLabelValues(providerType).Inc()
  57. }
  58. // RecordCacheMiss records a cache miss.
  59. func (m *defaultMetrics) RecordCacheMiss(_ string) {
  60. // Cache misses are implicit - we don't track them separately
  61. // The absence of a hit implies a miss
  62. }
  63. // RecordCacheInvalidation records a cache invalidation.
  64. func (m *defaultMetrics) RecordCacheInvalidation(providerType, reason string) {
  65. cacheInvalidationsTotal.WithLabelValues(providerType, reason).Inc()
  66. }
  67. // UpdateCachedClients updates the total cached clients gauge.
  68. func (m *defaultMetrics) UpdateCachedClients(providerType string, count int) {
  69. clientsCachedTotal.WithLabelValues(providerType).Set(float64(count))
  70. }
  71. // Global instance.
  72. var clientManagerMetrics Metrics = &defaultMetrics{}
  73. // RegisterMetrics registers all client manager metrics with the controller-runtime metrics registry.
  74. func RegisterMetrics() error {
  75. collectors := []prometheus.Collector{
  76. clientsCachedTotal,
  77. cacheHitsTotal,
  78. cacheInvalidationsTotal,
  79. }
  80. for _, collector := range collectors {
  81. if err := metrics.Registry.Register(collector); err != nil {
  82. var alreadyRegistered prometheus.AlreadyRegisteredError
  83. if errors.As(err, &alreadyRegistered) {
  84. continue
  85. }
  86. return fmt.Errorf("failed to register clientmanager metric: %w", err)
  87. }
  88. }
  89. // Initialize metrics with zero values so they appear in /metrics output
  90. // This ensures metrics are visible even before any cache operations occur
  91. for _, providerType := range []string{providerMetricsLabel, clusterProviderMetricsLabel} {
  92. clientsCachedTotal.WithLabelValues(providerType).Set(0)
  93. cacheHitsTotal.WithLabelValues(providerType).Add(0)
  94. cacheInvalidationsTotal.WithLabelValues(providerType, cacheInvalidationGeneration).Add(0)
  95. cacheInvalidationsTotal.WithLabelValues(providerType, cacheInvalidationMismatch).Add(0)
  96. }
  97. return nil
  98. }
  99. // GetClientManagerMetrics returns the client manager metrics instance for tests.
  100. func GetClientManagerMetrics() Metrics {
  101. return clientManagerMetrics
  102. }