metrics.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 externalsecret
  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. esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
  18. )
  19. const (
  20. ExternalSecretSubsystem = "externalsecret"
  21. SyncCallsKey = "sync_calls_total"
  22. SyncCallsErrorKey = "sync_calls_error"
  23. externalSecretStatusConditionKey = "status_condition"
  24. )
  25. var (
  26. syncCallsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
  27. Subsystem: ExternalSecretSubsystem,
  28. Name: SyncCallsKey,
  29. Help: "Total number of the External Secret sync calls",
  30. }, []string{"name", "namespace"})
  31. syncCallsError = prometheus.NewCounterVec(prometheus.CounterOpts{
  32. Subsystem: ExternalSecretSubsystem,
  33. Name: SyncCallsErrorKey,
  34. Help: "Total number of the External Secret sync errors",
  35. }, []string{"name", "namespace"})
  36. externalSecretCondition = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  37. Subsystem: ExternalSecretSubsystem,
  38. Name: externalSecretStatusConditionKey,
  39. Help: "The status condition of a specific External Secret",
  40. }, []string{"name", "namespace", "condition", "status"})
  41. )
  42. // updateExternalSecretCondition updates the ExternalSecret conditions.
  43. func updateExternalSecretCondition(es *esv1beta1.ExternalSecret, condition *esv1beta1.ExternalSecretStatusCondition, value float64) {
  44. switch condition.Type {
  45. case esv1beta1.ExternalSecretDeleted:
  46. // Remove condition=Ready metrics when the object gets deleted.
  47. externalSecretCondition.Delete(prometheus.Labels{
  48. "name": es.Name,
  49. "namespace": es.Namespace,
  50. "condition": string(esv1beta1.ExternalSecretReady),
  51. "status": string(v1.ConditionFalse),
  52. })
  53. externalSecretCondition.Delete(prometheus.Labels{
  54. "name": es.Name,
  55. "namespace": es.Namespace,
  56. "condition": string(esv1beta1.ExternalSecretReady),
  57. "status": string(v1.ConditionTrue),
  58. })
  59. case esv1beta1.ExternalSecretReady:
  60. // Remove condition=Deleted metrics when the object gets ready.
  61. externalSecretCondition.Delete(prometheus.Labels{
  62. "name": es.Name,
  63. "namespace": es.Namespace,
  64. "condition": string(esv1beta1.ExternalSecretDeleted),
  65. "status": string(v1.ConditionFalse),
  66. })
  67. externalSecretCondition.Delete(prometheus.Labels{
  68. "name": es.Name,
  69. "namespace": es.Namespace,
  70. "condition": string(esv1beta1.ExternalSecretDeleted),
  71. "status": string(v1.ConditionTrue),
  72. })
  73. // Toggle opposite Status to 0
  74. switch condition.Status {
  75. case v1.ConditionFalse:
  76. externalSecretCondition.With(prometheus.Labels{
  77. "name": es.Name,
  78. "namespace": es.Namespace,
  79. "condition": string(esv1beta1.ExternalSecretReady),
  80. "status": string(v1.ConditionTrue),
  81. }).Set(0)
  82. case v1.ConditionTrue:
  83. externalSecretCondition.With(prometheus.Labels{
  84. "name": es.Name,
  85. "namespace": es.Namespace,
  86. "condition": string(esv1beta1.ExternalSecretReady),
  87. "status": string(v1.ConditionFalse),
  88. }).Set(0)
  89. case v1.ConditionUnknown:
  90. break
  91. default:
  92. break
  93. }
  94. default:
  95. break
  96. }
  97. externalSecretCondition.With(prometheus.Labels{
  98. "name": es.Name,
  99. "namespace": es.Namespace,
  100. "condition": string(condition.Type),
  101. "status": string(condition.Status),
  102. }).Set(value)
  103. }
  104. func init() {
  105. metrics.Registry.MustRegister(syncCallsTotal, syncCallsError, externalSecretCondition)
  106. }