cssmetrics.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package cssmetrics
  13. import (
  14. "github.com/prometheus/client_golang/prometheus"
  15. "sigs.k8s.io/controller-runtime/pkg/metrics"
  16. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  17. commonmetrics "github.com/external-secrets/external-secrets/pkg/controllers/secretstore/metrics"
  18. )
  19. const (
  20. ClusterSecretStoreSubsystem = "clustersecretstore"
  21. ClusterSecretStoreReconcileDurationKey = "reconcile_duration"
  22. )
  23. var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  24. // SetUpMetrics is called at the root to set-up the metric logic using the
  25. // config flags provided.
  26. func SetUpMetrics() {
  27. clusterSecretStoreReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  28. Subsystem: ClusterSecretStoreSubsystem,
  29. Name: ClusterSecretStoreReconcileDurationKey,
  30. Help: "The duration time to reconcile the Cluster Secret Store",
  31. }, ctrlmetrics.NonConditionMetricLabelNames)
  32. clusterSecretStoreCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  33. Subsystem: ClusterSecretStoreSubsystem,
  34. Name: commonmetrics.StatusConditionKey,
  35. Help: "The status condition of a specific Cluster Secret Store",
  36. }, ctrlmetrics.ConditionMetricLabelNames)
  37. metrics.Registry.MustRegister(clusterSecretStoreReconcileDuration, clusterSecretStoreCondition)
  38. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  39. ClusterSecretStoreReconcileDurationKey: clusterSecretStoreReconcileDuration,
  40. commonmetrics.StatusConditionKey: clusterSecretStoreCondition,
  41. }
  42. }
  43. func GetGaugeVec(key string) *prometheus.GaugeVec {
  44. return gaugeVecMetrics[key]
  45. }
  46. // RemoveMetrics deletes all metrics published by the resource.
  47. func RemoveMetrics(namespace, name string) {
  48. for _, gaugeVecMetric := range gaugeVecMetrics {
  49. gaugeVecMetric.DeletePartialMatch(
  50. map[string]string{
  51. "namespace": namespace,
  52. "name": name,
  53. },
  54. )
  55. }
  56. }