metrics.go 3.7 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 clusterprovider exposes compatibility metrics for v2 ClusterProviderStore resources.
  14. package clusterprovider
  15. import (
  16. "sync"
  17. "github.com/prometheus/client_golang/prometheus"
  18. "sigs.k8s.io/controller-runtime/pkg/metrics"
  19. )
  20. const (
  21. // ClusterProviderSubsystem is the subsystem name for ClusterProvider metrics.
  22. ClusterProviderSubsystem = "clusterprovider"
  23. // ClusterProviderReconcileDurationKey is the key for the reconcile duration metric.
  24. ClusterProviderReconcileDurationKey = "reconcile_duration"
  25. // StatusConditionKey is the key for the status condition metric.
  26. StatusConditionKey = "status_condition"
  27. )
  28. var (
  29. gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  30. registerMetricsOnce sync.Once
  31. )
  32. // SetUpMetrics initializes the metrics for the ClusterProvider controller.
  33. func SetUpMetrics() {
  34. registerMetricsOnce.Do(func() {
  35. clusterProviderReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  36. Subsystem: ClusterProviderSubsystem,
  37. Name: ClusterProviderReconcileDurationKey,
  38. Help: "The duration time to reconcile the ClusterProvider",
  39. }, []string{"name"})
  40. clusterProviderCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  41. Subsystem: ClusterProviderSubsystem,
  42. Name: StatusConditionKey,
  43. Help: "The status condition of a specific ClusterProvider",
  44. }, []string{"name", "condition", "status"})
  45. metrics.Registry.MustRegister(clusterProviderReconcileDuration, clusterProviderCondition)
  46. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  47. ClusterProviderReconcileDurationKey: clusterProviderReconcileDuration,
  48. StatusConditionKey: clusterProviderCondition,
  49. }
  50. })
  51. }
  52. // GetGaugeVec returns the GaugeVec for the given key.
  53. func GetGaugeVec(key string) *prometheus.GaugeVec {
  54. return gaugeVecMetrics[key]
  55. }
  56. // RemoveMetrics deletes all metrics published by the resource.
  57. func RemoveMetrics(name string) {
  58. for _, gaugeVecMetric := range gaugeVecMetrics {
  59. gaugeVecMetric.DeletePartialMatch(
  60. map[string]string{
  61. "name": name,
  62. },
  63. )
  64. }
  65. }
  66. // UpdateStatusCondition updates the legacy ClusterProvider condition metrics for a v2 ClusterProviderStore.
  67. func UpdateStatusCondition(name, conditionType, conditionStatus string) {
  68. clusterProviderConditionGauge := GetGaugeVec(StatusConditionKey)
  69. if clusterProviderConditionGauge == nil {
  70. return
  71. }
  72. if conditionType == "Ready" {
  73. switch conditionStatus {
  74. case "False":
  75. clusterProviderConditionGauge.WithLabelValues(
  76. name,
  77. "Ready",
  78. "True",
  79. ).Set(0)
  80. case "True":
  81. clusterProviderConditionGauge.WithLabelValues(
  82. name,
  83. "Ready",
  84. "False",
  85. ).Set(0)
  86. case "Unknown":
  87. break
  88. }
  89. }
  90. clusterProviderConditionGauge.WithLabelValues(
  91. name,
  92. conditionType,
  93. conditionStatus,
  94. ).Set(1)
  95. }
  96. // RecordReconcileDuration updates the legacy ClusterProvider reconcile duration metric for a v2 ClusterProviderStore.
  97. func RecordReconcileDuration(name string, seconds float64) {
  98. clusterProviderReconcileDurationGauge := GetGaugeVec(ClusterProviderReconcileDurationKey)
  99. if clusterProviderReconcileDurationGauge == nil {
  100. return
  101. }
  102. clusterProviderReconcileDurationGauge.WithLabelValues(name).Set(seconds)
  103. }