psmetrics.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. switch condition.Type {
  61. case esapi.PushSecretReady:
  62. // Toggle opposite Status to 0
  63. switch condition.Status {
  64. case v1.ConditionFalse:
  65. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  66. map[string]string{
  67. "condition": string(esapi.PushSecretReady),
  68. "status": string(v1.ConditionTrue),
  69. })).Set(0)
  70. case v1.ConditionTrue:
  71. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  72. map[string]string{
  73. "condition": string(esapi.PushSecretReady),
  74. "status": string(v1.ConditionFalse),
  75. })).Set(0)
  76. case v1.ConditionUnknown:
  77. break
  78. default:
  79. break
  80. }
  81. default:
  82. break
  83. }
  84. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  85. map[string]string{
  86. "condition": string(condition.Type),
  87. "status": string(condition.Status),
  88. })).Set(value)
  89. }
  90. // GetGaugeVec returns a GaugeVec for the given metric key.
  91. func GetGaugeVec(key string) *prometheus.GaugeVec {
  92. return gaugeVecMetrics[key]
  93. }