cssmetrics.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 cssmetrics provides metrics for ClusterSecretStore controllers.
  14. package cssmetrics
  15. import (
  16. "github.com/prometheus/client_golang/prometheus"
  17. "sigs.k8s.io/controller-runtime/pkg/metrics"
  18. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  19. commonmetrics "github.com/external-secrets/external-secrets/pkg/controllers/secretstore/metrics"
  20. )
  21. const (
  22. // ClusterSecretStoreSubsystem is the subsystem name for ClusterSecretStore metrics.
  23. ClusterSecretStoreSubsystem = "clustersecretstore"
  24. // ClusterSecretStoreReconcileDurationKey is the key for the reconcile duration metric.
  25. ClusterSecretStoreReconcileDurationKey = "reconcile_duration"
  26. )
  27. var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  28. // SetUpMetrics is called at the root to set-up the metric logic using the
  29. // config flags provided.
  30. func SetUpMetrics() {
  31. clusterSecretStoreReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  32. Subsystem: ClusterSecretStoreSubsystem,
  33. Name: ClusterSecretStoreReconcileDurationKey,
  34. Help: "The duration time to reconcile the Cluster Secret Store",
  35. }, ctrlmetrics.NonConditionMetricLabelNames)
  36. clusterSecretStoreCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  37. Subsystem: ClusterSecretStoreSubsystem,
  38. Name: commonmetrics.StatusConditionKey,
  39. Help: "The status condition of a specific Cluster Secret Store",
  40. }, ctrlmetrics.ConditionMetricLabelNames)
  41. metrics.Registry.MustRegister(clusterSecretStoreReconcileDuration, clusterSecretStoreCondition)
  42. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  43. ClusterSecretStoreReconcileDurationKey: clusterSecretStoreReconcileDuration,
  44. commonmetrics.StatusConditionKey: clusterSecretStoreCondition,
  45. }
  46. }
  47. // GetGaugeVec retrieves a Prometheus GaugeVec based on the provided key.
  48. func GetGaugeVec(key string) *prometheus.GaugeVec {
  49. return gaugeVecMetrics[key]
  50. }
  51. // RemoveMetrics deletes all metrics published by the resource.
  52. func RemoveMetrics(namespace, name string) {
  53. for _, gaugeVecMetric := range gaugeVecMetrics {
  54. gaugeVecMetric.DeletePartialMatch(
  55. map[string]string{
  56. "namespace": namespace,
  57. "name": name,
  58. },
  59. )
  60. }
  61. }