util.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. "fmt"
  16. "sync"
  17. "k8s.io/client-go/util/flowcontrol"
  18. "github.com/prometheus/client_golang/prometheus"
  19. )
  20. var (
  21. metricsLock sync.Mutex
  22. rateLimiterMetrics = make(map[string]*rateLimiterMetric)
  23. )
  24. type rateLimiterMetric struct {
  25. metric prometheus.Gauge
  26. stopCh chan struct{}
  27. }
  28. func registerRateLimiterMetric(ownerName string) error {
  29. metricsLock.Lock()
  30. defer metricsLock.Unlock()
  31. if _, ok := rateLimiterMetrics[ownerName]; ok {
  32. // only register once in Prometheus. We happen to see an ownerName reused in parallel integration tests.
  33. return nil
  34. }
  35. metric := prometheus.NewGauge(prometheus.GaugeOpts{
  36. Name: "rate_limiter_use",
  37. Subsystem: ownerName,
  38. Help: fmt.Sprintf("A metric measuring the saturation of the rate limiter for %v", ownerName),
  39. })
  40. if err := prometheus.Register(metric); err != nil {
  41. return fmt.Errorf("error registering rate limiter usage metric: %v", err)
  42. }
  43. stopCh := make(chan struct{})
  44. rateLimiterMetrics[ownerName] = &rateLimiterMetric{
  45. metric: metric,
  46. stopCh: stopCh,
  47. }
  48. return nil
  49. }
  50. // RegisterMetricAndTrackRateLimiterUsage registers a metric ownerName_rate_limiter_use in prometheus to track
  51. // how much used rateLimiter is and starts a goroutine that updates this metric every updatePeriod
  52. func RegisterMetricAndTrackRateLimiterUsage(ownerName string, rateLimiter flowcontrol.RateLimiter) error {
  53. if err := registerRateLimiterMetric(ownerName); err != nil {
  54. return err
  55. }
  56. // TODO: determine how to track rate limiter saturation
  57. // See discussion at https://go-review.googlesource.com/c/time/+/29958#message-4caffc11669cadd90e2da4c05122cfec50ea6a22
  58. // go wait.Until(func() {
  59. // metricsLock.Lock()
  60. // defer metricsLock.Unlock()
  61. // rateLimiterMetrics[ownerName].metric.Set()
  62. // }, updatePeriod, rateLimiterMetrics[ownerName].stopCh)
  63. return nil
  64. }