ssmetrics.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 ssmetrics provides metrics for SecretStore controllers.
  14. package ssmetrics
  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. // SecretStoreSubsystem is the subsystem name for SecretStore metrics.
  23. SecretStoreSubsystem = "secretstore"
  24. // SecretStoreReconcileDurationKey is the key for the reconcile duration metric.
  25. SecretStoreReconcileDurationKey = "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. secretStoreReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  32. Subsystem: SecretStoreSubsystem,
  33. Name: SecretStoreReconcileDurationKey,
  34. Help: "The duration time to reconcile the Secret Store",
  35. }, ctrlmetrics.NonConditionMetricLabelNames)
  36. secretStoreCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  37. Subsystem: SecretStoreSubsystem,
  38. Name: commonmetrics.StatusConditionKey,
  39. Help: "The status condition of a specific Secret Store",
  40. }, ctrlmetrics.ConditionMetricLabelNames)
  41. metrics.Registry.MustRegister(secretStoreReconcileDuration, secretStoreCondition)
  42. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  43. SecretStoreReconcileDurationKey: secretStoreReconcileDuration,
  44. commonmetrics.StatusConditionKey: secretStoreCondition,
  45. }
  46. }
  47. // GetGaugeVec returns the GaugeVec for the given 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. }