psmetrics.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright © 2025 ESO Maintainer Team
  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 psmetrics provides metrics for PushSecret controller.
  14. package psmetrics
  15. import (
  16. "github.com/prometheus/client_golang/prometheus"
  17. v1 "k8s.io/api/core/v1"
  18. "sigs.k8s.io/controller-runtime/pkg/metrics"
  19. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  20. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  21. )
  22. const (
  23. // PushSecretSubsystem is the subsystem name for PushSecret metrics.
  24. PushSecretSubsystem = "pushsecret"
  25. // PushSecretReconcileDurationKey is the key for the reconcile duration metric.
  26. PushSecretReconcileDurationKey = "reconcile_duration"
  27. // PushSecretStatusConditionKey is the key for the status condition metric.
  28. PushSecretStatusConditionKey = "status_condition"
  29. )
  30. var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  31. // SetUpMetrics is called at the root to set-up the metric logic using the
  32. // config flags provided.
  33. func SetUpMetrics() {
  34. pushSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  35. Subsystem: PushSecretSubsystem,
  36. Name: PushSecretStatusConditionKey,
  37. Help: "The status condition of a specific Push Secret",
  38. }, ctrlmetrics.ConditionMetricLabelNames)
  39. pushSecretReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  40. Subsystem: PushSecretSubsystem,
  41. Name: PushSecretReconcileDurationKey,
  42. Help: "The duration time to reconcile the Push Secret",
  43. }, ctrlmetrics.NonConditionMetricLabelNames)
  44. metrics.Registry.MustRegister(pushSecretReconcileDuration, pushSecretCondition)
  45. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  46. PushSecretStatusConditionKey: pushSecretCondition,
  47. PushSecretReconcileDurationKey: pushSecretReconcileDuration,
  48. }
  49. }
  50. // UpdatePushSecretCondition updates the condition metrics for a PushSecret.
  51. func UpdatePushSecretCondition(ps *esapi.PushSecret, condition *esapi.PushSecretStatusCondition, value float64) {
  52. psInfo := make(map[string]string)
  53. psInfo["name"] = ps.Name
  54. psInfo["namespace"] = ps.Namespace
  55. for k, v := range ps.Labels {
  56. psInfo[k] = v
  57. }
  58. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(psInfo)
  59. pushSecretCondition := GetGaugeVec(PushSecretStatusConditionKey)
  60. // This allows us to delete metrics even when other labels (like helm annotations) have changed
  61. baseLabels := prometheus.Labels{
  62. "name": ps.Name,
  63. "namespace": ps.Namespace,
  64. }
  65. switch condition.Type {
  66. case esapi.PushSecretReady:
  67. // Toggle opposite Status to 0, but first delete any stale metrics with old labels
  68. switch condition.Status {
  69. case v1.ConditionFalse:
  70. // delete any existing metrics with status True (regardless of other labels)
  71. baseLabels["condition"] = string(esapi.PushSecretReady)
  72. baseLabels["status"] = string(v1.ConditionTrue)
  73. pushSecretCondition.DeletePartialMatch(baseLabels)
  74. delete(baseLabels, "condition")
  75. delete(baseLabels, "status")
  76. // Set the metric with current labels
  77. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  78. map[string]string{
  79. "condition": string(esapi.PushSecretReady),
  80. "status": string(v1.ConditionTrue),
  81. })).Set(0)
  82. case v1.ConditionTrue:
  83. // delete any existing metrics with status False (regardless of other labels)
  84. baseLabels["condition"] = string(esapi.PushSecretReady)
  85. baseLabels["status"] = string(v1.ConditionFalse)
  86. pushSecretCondition.DeletePartialMatch(baseLabels)
  87. delete(baseLabels, "condition")
  88. delete(baseLabels, "status")
  89. // finally, set the metric with current labels
  90. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  91. map[string]string{
  92. "condition": string(esapi.PushSecretReady),
  93. "status": string(v1.ConditionFalse),
  94. })).Set(0)
  95. case v1.ConditionUnknown:
  96. break
  97. default:
  98. break
  99. }
  100. default:
  101. break
  102. }
  103. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  104. map[string]string{
  105. "condition": string(condition.Type),
  106. "status": string(condition.Status),
  107. })).Set(value)
  108. }
  109. // GetGaugeVec returns a GaugeVec for the given metric key.
  110. func GetGaugeVec(key string) *prometheus.GaugeVec {
  111. return gaugeVecMetrics[key]
  112. }