metrics.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 provider exposes compatibility metrics for v2 ProviderStore resources.
  14. package provider
  15. import (
  16. "maps"
  17. "sync"
  18. "github.com/prometheus/client_golang/prometheus"
  19. "sigs.k8s.io/controller-runtime/pkg/metrics"
  20. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  21. )
  22. const (
  23. // ProviderSubsystem is the subsystem name for Provider metrics.
  24. ProviderSubsystem = "provider"
  25. // ProviderReconcileDurationKey is the key for the reconcile duration metric.
  26. ProviderReconcileDurationKey = "reconcile_duration"
  27. // StatusConditionKey is the key for the status condition metric.
  28. StatusConditionKey = "status_condition"
  29. )
  30. var (
  31. gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  32. registerMetricsOnce sync.Once
  33. )
  34. // SetUpMetrics initializes the metrics for the Provider controller.
  35. func SetUpMetrics() {
  36. registerMetricsOnce.Do(func() {
  37. providerReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  38. Subsystem: ProviderSubsystem,
  39. Name: ProviderReconcileDurationKey,
  40. Help: "The duration time to reconcile the Provider",
  41. }, ctrlmetrics.NonConditionMetricLabelNames)
  42. providerCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  43. Subsystem: ProviderSubsystem,
  44. Name: StatusConditionKey,
  45. Help: "The status condition of a specific Provider",
  46. }, ctrlmetrics.ConditionMetricLabelNames)
  47. metrics.Registry.MustRegister(providerReconcileDuration, providerCondition)
  48. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  49. ProviderReconcileDurationKey: providerReconcileDuration,
  50. StatusConditionKey: providerCondition,
  51. }
  52. })
  53. }
  54. // GetGaugeVec returns the GaugeVec for the given key.
  55. func GetGaugeVec(key string) *prometheus.GaugeVec {
  56. return gaugeVecMetrics[key]
  57. }
  58. // RemoveMetrics deletes all metrics published by the resource.
  59. func RemoveMetrics(namespace, name string) {
  60. for _, gaugeVecMetric := range gaugeVecMetrics {
  61. gaugeVecMetric.DeletePartialMatch(
  62. map[string]string{
  63. "namespace": namespace,
  64. "name": name,
  65. },
  66. )
  67. }
  68. }
  69. // UpdateStatusCondition updates the legacy Provider condition metrics for a v2 ProviderStore.
  70. func UpdateStatusCondition(name, namespace string, labels map[string]string, conditionType, conditionStatus string) {
  71. providerInfo := map[string]string{
  72. "name": name,
  73. "namespace": namespace,
  74. }
  75. maps.Copy(providerInfo, labels)
  76. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(providerInfo)
  77. providerConditionGauge := GetGaugeVec(StatusConditionKey)
  78. if providerConditionGauge == nil {
  79. return
  80. }
  81. if conditionType == "Ready" {
  82. switch conditionStatus {
  83. case "False":
  84. providerConditionGauge.With(ctrlmetrics.RefineLabels(conditionLabels,
  85. map[string]string{
  86. "condition": "Ready",
  87. "status": "True",
  88. })).Set(0)
  89. case "True":
  90. providerConditionGauge.With(ctrlmetrics.RefineLabels(conditionLabels,
  91. map[string]string{
  92. "condition": "Ready",
  93. "status": "False",
  94. })).Set(0)
  95. case "Unknown":
  96. break
  97. }
  98. }
  99. providerConditionGauge.With(ctrlmetrics.RefineLabels(conditionLabels,
  100. map[string]string{
  101. "condition": conditionType,
  102. "status": conditionStatus,
  103. })).Set(1)
  104. }
  105. // RecordReconcileDuration updates the legacy Provider reconcile duration metric for a v2 ProviderStore.
  106. func RecordReconcileDuration(name, namespace string, labels map[string]string, seconds float64) {
  107. providerInfo := map[string]string{
  108. "name": name,
  109. "namespace": namespace,
  110. }
  111. maps.Copy(providerInfo, labels)
  112. providerReconcileDurationGauge := GetGaugeVec(ProviderReconcileDurationKey)
  113. if providerReconcileDurationGauge == nil {
  114. return
  115. }
  116. providerReconcileDurationGauge.With(ctrlmetrics.RefineNonConditionMetricLabels(providerInfo)).Set(seconds)
  117. }