psmetrics.go 4.6 KB

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