esmetrics.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 esmetrics provides metrics functionality for the ExternalSecret controller
  14. package esmetrics
  15. import (
  16. "maps"
  17. "github.com/prometheus/client_golang/prometheus"
  18. v1 "k8s.io/api/core/v1"
  19. "sigs.k8s.io/controller-runtime/pkg/metrics"
  20. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  21. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  22. )
  23. const (
  24. // ExternalSecretSubsystem is the subsystem for the external-secret controller.
  25. ExternalSecretSubsystem = "externalsecret"
  26. // SyncCallsKey is the metric key for sync calls.
  27. SyncCallsKey = "sync_calls_total"
  28. // SyncCallsErrorKey is the metric key for sync call errors.
  29. SyncCallsErrorKey = "sync_calls_error"
  30. // ExternalSecretStatusConditionKey is the metric key for the external secret status condition.
  31. ExternalSecretStatusConditionKey = "status_condition"
  32. // ExternalSecretReconcileDurationKey is the metric key for the external secret reconcile duration.
  33. ExternalSecretReconcileDurationKey = "reconcile_duration"
  34. )
  35. var counterVecMetrics = map[string]*prometheus.CounterVec{}
  36. var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  37. // SetUpMetrics is called at the root to set-up the metric logic using the
  38. // config flags provided.
  39. func SetUpMetrics() {
  40. // Obtain the prometheus metrics and register
  41. syncCallsTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
  42. Subsystem: ExternalSecretSubsystem,
  43. Name: SyncCallsKey,
  44. Help: "Total number of the External Secret sync calls",
  45. }, ctrlmetrics.NonConditionMetricLabelNames)
  46. syncCallsError := prometheus.NewCounterVec(prometheus.CounterOpts{
  47. Subsystem: ExternalSecretSubsystem,
  48. Name: SyncCallsErrorKey,
  49. Help: "Total number of the External Secret sync errors",
  50. }, ctrlmetrics.NonConditionMetricLabelNames)
  51. externalSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  52. Subsystem: ExternalSecretSubsystem,
  53. Name: ExternalSecretStatusConditionKey,
  54. Help: "The status condition of a specific External Secret",
  55. }, ctrlmetrics.ConditionMetricLabelNames)
  56. externalSecretReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  57. Subsystem: ExternalSecretSubsystem,
  58. Name: ExternalSecretReconcileDurationKey,
  59. Help: "The duration time to reconcile the External Secret",
  60. }, ctrlmetrics.NonConditionMetricLabelNames)
  61. metrics.Registry.MustRegister(syncCallsTotal, syncCallsError, externalSecretCondition, externalSecretReconcileDuration)
  62. counterVecMetrics = map[string]*prometheus.CounterVec{
  63. SyncCallsKey: syncCallsTotal,
  64. SyncCallsErrorKey: syncCallsError,
  65. }
  66. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  67. ExternalSecretStatusConditionKey: externalSecretCondition,
  68. ExternalSecretReconcileDurationKey: externalSecretReconcileDuration,
  69. }
  70. }
  71. // UpdateExternalSecretCondition is a function that updates the condition of an external secret.
  72. func UpdateExternalSecretCondition(es *esv1.ExternalSecret, condition *esv1.ExternalSecretStatusCondition, value float64) {
  73. esInfo := make(map[string]string)
  74. esInfo["name"] = es.Name
  75. esInfo["namespace"] = es.Namespace
  76. maps.Copy(esInfo, es.Labels)
  77. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(esInfo)
  78. externalSecretCondition := GetGaugeVec(ExternalSecretStatusConditionKey)
  79. // this allows us to delete metrics even when other labels (like helm annotations) have changed
  80. baseLabels := prometheus.Labels{
  81. "name": es.Name,
  82. "namespace": es.Namespace,
  83. }
  84. switch condition.Type {
  85. case esv1.ExternalSecretDeleted:
  86. // Remove condition=Ready metrics when the object gets deleted.
  87. baseLabels["condition"] = string(esv1.ExternalSecretReady)
  88. baseLabels["status"] = string(v1.ConditionFalse)
  89. externalSecretCondition.DeletePartialMatch(baseLabels)
  90. baseLabels["status"] = string(v1.ConditionTrue)
  91. externalSecretCondition.DeletePartialMatch(baseLabels)
  92. delete(baseLabels, "condition")
  93. delete(baseLabels, "status")
  94. case esv1.ExternalSecretReady:
  95. // Remove condition=Deleted metrics when the object gets ready.
  96. baseLabels["condition"] = string(esv1.ExternalSecretDeleted)
  97. baseLabels["status"] = string(v1.ConditionFalse)
  98. externalSecretCondition.DeletePartialMatch(baseLabels)
  99. baseLabels["status"] = string(v1.ConditionTrue)
  100. externalSecretCondition.DeletePartialMatch(baseLabels)
  101. delete(baseLabels, "condition")
  102. delete(baseLabels, "status")
  103. // Toggle opposite Status to 0, but first delete any stale metrics with old labels
  104. switch condition.Status {
  105. case v1.ConditionFalse:
  106. // delete any existing metrics with status True (regardless of other labels)
  107. // condition is fixed to ExternalSecretReady because other statuses were already handled above.
  108. baseLabels["condition"] = string(esv1.ExternalSecretReady)
  109. baseLabels["status"] = string(v1.ConditionTrue)
  110. externalSecretCondition.DeletePartialMatch(baseLabels)
  111. delete(baseLabels, "condition")
  112. delete(baseLabels, "status")
  113. // Set the metric with current labels
  114. externalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  115. map[string]string{
  116. "condition": string(esv1.ExternalSecretReady),
  117. "status": string(v1.ConditionTrue),
  118. })).Set(0)
  119. case v1.ConditionTrue:
  120. // delete any existing metrics with status False (regardless of other labels)
  121. baseLabels["condition"] = string(esv1.ExternalSecretReady)
  122. baseLabels["status"] = string(v1.ConditionFalse)
  123. externalSecretCondition.DeletePartialMatch(baseLabels)
  124. delete(baseLabels, "condition")
  125. delete(baseLabels, "status")
  126. // finally, set the metric with current labels
  127. externalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  128. map[string]string{
  129. "condition": string(esv1.ExternalSecretReady),
  130. "status": string(v1.ConditionFalse),
  131. })).Set(0)
  132. case v1.ConditionUnknown:
  133. break
  134. default:
  135. break
  136. }
  137. default:
  138. break
  139. }
  140. externalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  141. map[string]string{
  142. "condition": string(condition.Type),
  143. "status": string(condition.Status),
  144. })).Set(value)
  145. }
  146. // GetCounterVec returns the counter vec for the given key.
  147. func GetCounterVec(key string) *prometheus.CounterVec {
  148. return counterVecMetrics[key]
  149. }
  150. // GetGaugeVec returns the gauge vec for the given key.
  151. func GetGaugeVec(key string) *prometheus.GaugeVec {
  152. return gaugeVecMetrics[key]
  153. }