cesmetrics.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 cesmetrics provides functionality for tracking and exposing metrics related to ClusterExternalSecret resources.
  14. package cesmetrics
  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. esv1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1"
  20. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  21. )
  22. // Constants for metrics subsystem and keys.
  23. const (
  24. // ClusterExternalSecretSubsystem is the subsystem name used for ClusterExternalSecret metrics.
  25. ClusterExternalSecretSubsystem = "clusterexternalsecret"
  26. ClusterExternalSecretReconcileDurationKey = "reconcile_duration"
  27. ClusterExternalSecretStatusConditionKey = "status_condition"
  28. )
  29. var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  30. // SetUpMetrics is called at the root to set-up the metric logic using the
  31. // config flags provided.
  32. func SetUpMetrics() {
  33. clusterExternalSecretReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  34. Subsystem: ClusterExternalSecretSubsystem,
  35. Name: ClusterExternalSecretReconcileDurationKey,
  36. Help: "The duration time to reconcile the Cluster External Secret",
  37. }, ctrlmetrics.NonConditionMetricLabelNames)
  38. clusterExternalSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  39. Subsystem: ClusterExternalSecretSubsystem,
  40. Name: ClusterExternalSecretStatusConditionKey,
  41. Help: "The status condition of a specific Cluster External Secret",
  42. }, ctrlmetrics.ConditionMetricLabelNames)
  43. metrics.Registry.MustRegister(clusterExternalSecretReconcileDuration, clusterExternalSecretCondition)
  44. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  45. ClusterExternalSecretStatusConditionKey: clusterExternalSecretCondition,
  46. ClusterExternalSecretReconcileDurationKey: clusterExternalSecretReconcileDuration,
  47. }
  48. }
  49. // GetGaugeVec returns a GaugeVec for the given metric key.
  50. func GetGaugeVec(key string) *prometheus.GaugeVec {
  51. return gaugeVecMetrics[key]
  52. }
  53. // UpdateClusterExternalSecretCondition updates the metrics for a ClusterExternalSecret based on its condition.
  54. func UpdateClusterExternalSecretCondition(ces *esv1.ClusterExternalSecret, condition *esv1.ClusterExternalSecretStatusCondition) {
  55. if condition.Status != v1.ConditionTrue {
  56. // This should not happen
  57. return
  58. }
  59. cesInfo := make(map[string]string)
  60. cesInfo["name"] = ces.Name
  61. for k, v := range ces.Labels {
  62. cesInfo[k] = v
  63. }
  64. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(cesInfo)
  65. clusterExternalSecretCondition := GetGaugeVec(ClusterExternalSecretStatusConditionKey)
  66. // This handles cases where labels may have changed
  67. baseLabels := prometheus.Labels{
  68. "name": ces.Name,
  69. "condition": string(condition.Type),
  70. "status": string(v1.ConditionFalse),
  71. }
  72. clusterExternalSecretCondition.DeletePartialMatch(baseLabels)
  73. clusterExternalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  74. map[string]string{
  75. "condition": string(condition.Type),
  76. "status": string(condition.Status),
  77. })).Set(1)
  78. clusterExternalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  79. map[string]string{
  80. "condition": string(condition.Type),
  81. "status": string(v1.ConditionFalse),
  82. })).Set(0)
  83. }
  84. // RemoveMetrics deletes all metrics published by the resource.
  85. func RemoveMetrics(namespace, name string) {
  86. for _, gaugeVecMetric := range gaugeVecMetrics {
  87. gaugeVecMetric.DeletePartialMatch(
  88. map[string]string{
  89. "namespace": namespace,
  90. "name": name,
  91. },
  92. )
  93. }
  94. }