metric_recorder.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. Copyright 2019 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. "github.com/prometheus/client_golang/prometheus"
  16. )
  17. // MetricRecorder represents a metric recorder which takes action when the
  18. // metric Inc(), Dec() and Clear()
  19. type MetricRecorder interface {
  20. Inc()
  21. Dec()
  22. Clear()
  23. }
  24. var _ MetricRecorder = &PendingPodsRecorder{}
  25. // PendingPodsRecorder is an implementation of MetricRecorder
  26. type PendingPodsRecorder struct {
  27. recorder prometheus.Gauge
  28. }
  29. // NewActivePodsRecorder returns ActivePods in a Prometheus metric fashion
  30. func NewActivePodsRecorder() *PendingPodsRecorder {
  31. return &PendingPodsRecorder{
  32. recorder: ActivePods,
  33. }
  34. }
  35. // NewUnschedulablePodsRecorder returns UnschedulablePods in a Prometheus metric fashion
  36. func NewUnschedulablePodsRecorder() *PendingPodsRecorder {
  37. return &PendingPodsRecorder{
  38. recorder: UnschedulablePods,
  39. }
  40. }
  41. // NewBackoffPodsRecorder returns BackoffPods in a Prometheus metric fashion
  42. func NewBackoffPodsRecorder() *PendingPodsRecorder {
  43. return &PendingPodsRecorder{
  44. recorder: BackoffPods,
  45. }
  46. }
  47. // Inc increases a metric counter by 1, in an atomic way
  48. func (r *PendingPodsRecorder) Inc() {
  49. r.recorder.Inc()
  50. }
  51. // Dec decreases a metric counter by 1, in an atomic way
  52. func (r *PendingPodsRecorder) Dec() {
  53. r.recorder.Dec()
  54. }
  55. // Clear set a metric counter to 0, in an atomic way
  56. func (r *PendingPodsRecorder) Clear() {
  57. r.recorder.Set(float64(0))
  58. }