psmetrics.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 psmetrics
  13. import (
  14. "github.com/prometheus/client_golang/prometheus"
  15. v1 "k8s.io/api/core/v1"
  16. "sigs.k8s.io/controller-runtime/pkg/metrics"
  17. esapi "github.com/external-secrets/external-secrets/apis/externalsecrets/v1alpha1"
  18. ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
  19. )
  20. const (
  21. PushSecretSubsystem = "pushsecret"
  22. PushSecretReconcileDurationKey = "reconcile_duration"
  23. PushSecretStatusConditionKey = "status_condition"
  24. )
  25. var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
  26. // SetUpMetrics is called at the root to set-up the metric logic using the
  27. // config flags provided.
  28. func SetUpMetrics() {
  29. pushSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  30. Subsystem: PushSecretSubsystem,
  31. Name: PushSecretStatusConditionKey,
  32. Help: "The status condition of a specific Push Secret",
  33. }, ctrlmetrics.ConditionMetricLabelNames)
  34. pushSecretReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
  35. Subsystem: PushSecretSubsystem,
  36. Name: PushSecretReconcileDurationKey,
  37. Help: "The duration time to reconcile the Push Secret",
  38. }, ctrlmetrics.NonConditionMetricLabelNames)
  39. metrics.Registry.MustRegister(pushSecretReconcileDuration, pushSecretCondition)
  40. gaugeVecMetrics = map[string]*prometheus.GaugeVec{
  41. PushSecretStatusConditionKey: pushSecretCondition,
  42. PushSecretReconcileDurationKey: pushSecretReconcileDuration,
  43. }
  44. }
  45. func UpdatePushSecretCondition(ps *esapi.PushSecret, condition *esapi.PushSecretStatusCondition, value float64) {
  46. psInfo := make(map[string]string)
  47. psInfo["name"] = ps.Name
  48. psInfo["namespace"] = ps.Namespace
  49. for k, v := range ps.Labels {
  50. psInfo[k] = v
  51. }
  52. conditionLabels := ctrlmetrics.RefineConditionMetricLabels(psInfo)
  53. pushSecretCondition := GetGaugeVec(PushSecretStatusConditionKey)
  54. switch condition.Type {
  55. case esapi.PushSecretReady:
  56. // Toggle opposite Status to 0
  57. switch condition.Status {
  58. case v1.ConditionFalse:
  59. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  60. map[string]string{
  61. "condition": string(esapi.PushSecretReady),
  62. "status": string(v1.ConditionTrue),
  63. })).Set(0)
  64. case v1.ConditionTrue:
  65. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  66. map[string]string{
  67. "condition": string(esapi.PushSecretReady),
  68. "status": string(v1.ConditionFalse),
  69. })).Set(0)
  70. case v1.ConditionUnknown:
  71. break
  72. default:
  73. break
  74. }
  75. default:
  76. break
  77. }
  78. pushSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
  79. map[string]string{
  80. "condition": string(condition.Type),
  81. "status": string(condition.Status),
  82. })).Set(value)
  83. }
  84. func GetGaugeVec(key string) *prometheus.GaugeVec {
  85. return gaugeVecMetrics[key]
  86. }