|
|
@@ -15,13 +15,12 @@ limitations under the License.
|
|
|
package esmetrics
|
|
|
|
|
|
import (
|
|
|
- "regexp"
|
|
|
-
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
"sigs.k8s.io/controller-runtime/pkg/metrics"
|
|
|
|
|
|
esv1beta1 "github.com/external-secrets/external-secrets/apis/externalsecrets/v1beta1"
|
|
|
+ ctrlmetrics "github.com/external-secrets/external-secrets/pkg/controllers/metrics"
|
|
|
)
|
|
|
|
|
|
const (
|
|
|
@@ -32,78 +31,37 @@ const (
|
|
|
ExternalSecretReconcileDurationKey = "reconcile_duration"
|
|
|
)
|
|
|
|
|
|
-var (
|
|
|
- NonConditionMetricLabelNames = make([]string, 0)
|
|
|
-
|
|
|
- ConditionMetricLabelNames = make([]string, 0)
|
|
|
+var counterVecMetrics = map[string]*prometheus.CounterVec{}
|
|
|
|
|
|
- NonConditionMetricLabels = make(map[string]string)
|
|
|
-
|
|
|
- ConditionMetricLabels = make(map[string]string)
|
|
|
-)
|
|
|
-
|
|
|
-var counterVecMetrics map[string]*prometheus.CounterVec = map[string]*prometheus.CounterVec{}
|
|
|
-
|
|
|
-var gaugeVecMetrics map[string]*prometheus.GaugeVec = map[string]*prometheus.GaugeVec{}
|
|
|
+var gaugeVecMetrics = map[string]*prometheus.GaugeVec{}
|
|
|
|
|
|
// Called at the root to set-up the metric logic using the
|
|
|
// config flags provided.
|
|
|
-func SetUpMetrics(addKubeStandardLabels bool) {
|
|
|
- // Figure out what the labels for the metrics are
|
|
|
- if addKubeStandardLabels {
|
|
|
- NonConditionMetricLabelNames = []string{
|
|
|
- "name", "namespace",
|
|
|
- "app_kubernetes_io_name", "app_kubernetes_io_instance",
|
|
|
- "app_kubernetes_io_version", "app_kubernetes_io_component",
|
|
|
- "app_kubernetes_io_part_of", "app_kubernetes_io_managed_by",
|
|
|
- }
|
|
|
-
|
|
|
- ConditionMetricLabelNames = []string{
|
|
|
- "name", "namespace",
|
|
|
- "condition", "status",
|
|
|
- "app_kubernetes_io_name", "app_kubernetes_io_instance",
|
|
|
- "app_kubernetes_io_version", "app_kubernetes_io_component",
|
|
|
- "app_kubernetes_io_part_of", "app_kubernetes_io_managed_by",
|
|
|
- }
|
|
|
- } else {
|
|
|
- NonConditionMetricLabelNames = []string{"name", "namespace"}
|
|
|
-
|
|
|
- ConditionMetricLabelNames = []string{"name", "namespace", "condition", "status"}
|
|
|
- }
|
|
|
-
|
|
|
- // Set default values for each label
|
|
|
- for _, k := range NonConditionMetricLabelNames {
|
|
|
- NonConditionMetricLabels[k] = ""
|
|
|
- }
|
|
|
-
|
|
|
- for _, k := range ConditionMetricLabelNames {
|
|
|
- ConditionMetricLabels[k] = ""
|
|
|
- }
|
|
|
-
|
|
|
+func SetUpMetrics() {
|
|
|
// Obtain the prometheus metrics and register
|
|
|
syncCallsTotal := prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
Subsystem: ExternalSecretSubsystem,
|
|
|
Name: SyncCallsKey,
|
|
|
Help: "Total number of the External Secret sync calls",
|
|
|
- }, NonConditionMetricLabelNames)
|
|
|
+ }, ctrlmetrics.NonConditionMetricLabelNames)
|
|
|
|
|
|
syncCallsError := prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
Subsystem: ExternalSecretSubsystem,
|
|
|
Name: SyncCallsErrorKey,
|
|
|
Help: "Total number of the External Secret sync errors",
|
|
|
- }, NonConditionMetricLabelNames)
|
|
|
+ }, ctrlmetrics.NonConditionMetricLabelNames)
|
|
|
|
|
|
externalSecretCondition := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
Subsystem: ExternalSecretSubsystem,
|
|
|
Name: ExternalSecretStatusConditionKey,
|
|
|
Help: "The status condition of a specific External Secret",
|
|
|
- }, ConditionMetricLabelNames)
|
|
|
+ }, ctrlmetrics.ConditionMetricLabelNames)
|
|
|
|
|
|
externalSecretReconcileDuration := prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
Subsystem: ExternalSecretSubsystem,
|
|
|
Name: ExternalSecretReconcileDurationKey,
|
|
|
Help: "The duration time to reconcile the External Secret",
|
|
|
- }, NonConditionMetricLabelNames)
|
|
|
+ }, ctrlmetrics.NonConditionMetricLabelNames)
|
|
|
|
|
|
metrics.Registry.MustRegister(syncCallsTotal, syncCallsError, externalSecretCondition, externalSecretReconcileDuration)
|
|
|
|
|
|
@@ -125,19 +83,19 @@ func UpdateExternalSecretCondition(es *esv1beta1.ExternalSecret, condition *esv1
|
|
|
for k, v := range es.Labels {
|
|
|
esInfo[k] = v
|
|
|
}
|
|
|
- conditionLabels := RefineConditionMetricLabels(esInfo)
|
|
|
+ conditionLabels := ctrlmetrics.RefineConditionMetricLabels(esInfo)
|
|
|
externalSecretCondition := GetGaugeVec(ExternalSecretStatusConditionKey)
|
|
|
|
|
|
switch condition.Type {
|
|
|
case esv1beta1.ExternalSecretDeleted:
|
|
|
// Remove condition=Ready metrics when the object gets deleted.
|
|
|
- externalSecretCondition.Delete(RefineLabels(conditionLabels,
|
|
|
+ externalSecretCondition.Delete(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
map[string]string{
|
|
|
"condition": string(esv1beta1.ExternalSecretReady),
|
|
|
"status": string(v1.ConditionFalse),
|
|
|
}))
|
|
|
|
|
|
- externalSecretCondition.Delete(RefineLabels(conditionLabels,
|
|
|
+ externalSecretCondition.Delete(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
map[string]string{
|
|
|
"condition": string(esv1beta1.ExternalSecretReady),
|
|
|
"status": string(v1.ConditionTrue),
|
|
|
@@ -145,13 +103,13 @@ func UpdateExternalSecretCondition(es *esv1beta1.ExternalSecret, condition *esv1
|
|
|
|
|
|
case esv1beta1.ExternalSecretReady:
|
|
|
// Remove condition=Deleted metrics when the object gets ready.
|
|
|
- externalSecretCondition.Delete(RefineLabels(conditionLabels,
|
|
|
+ externalSecretCondition.Delete(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
map[string]string{
|
|
|
"condition": string(esv1beta1.ExternalSecretDeleted),
|
|
|
"status": string(v1.ConditionFalse),
|
|
|
}))
|
|
|
|
|
|
- externalSecretCondition.Delete(RefineLabels(conditionLabels,
|
|
|
+ externalSecretCondition.Delete(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
map[string]string{
|
|
|
"condition": string(esv1beta1.ExternalSecretDeleted),
|
|
|
"status": string(v1.ConditionTrue),
|
|
|
@@ -160,13 +118,13 @@ func UpdateExternalSecretCondition(es *esv1beta1.ExternalSecret, condition *esv1
|
|
|
// Toggle opposite Status to 0
|
|
|
switch condition.Status {
|
|
|
case v1.ConditionFalse:
|
|
|
- externalSecretCondition.With(RefineLabels(conditionLabels,
|
|
|
+ externalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
map[string]string{
|
|
|
"condition": string(esv1beta1.ExternalSecretReady),
|
|
|
"status": string(v1.ConditionTrue),
|
|
|
})).Set(0)
|
|
|
case v1.ConditionTrue:
|
|
|
- externalSecretCondition.With(RefineLabels(conditionLabels,
|
|
|
+ externalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
map[string]string{
|
|
|
"condition": string(esv1beta1.ExternalSecretReady),
|
|
|
"status": string(v1.ConditionFalse),
|
|
|
@@ -181,7 +139,7 @@ func UpdateExternalSecretCondition(es *esv1beta1.ExternalSecret, condition *esv1
|
|
|
break
|
|
|
}
|
|
|
|
|
|
- externalSecretCondition.With(RefineLabels(conditionLabels,
|
|
|
+ externalSecretCondition.With(ctrlmetrics.RefineLabels(conditionLabels,
|
|
|
map[string]string{
|
|
|
"condition": string(condition.Type),
|
|
|
"status": string(condition.Status),
|
|
|
@@ -195,36 +153,3 @@ func GetCounterVec(key string) *prometheus.CounterVec {
|
|
|
func GetGaugeVec(key string) *prometheus.GaugeVec {
|
|
|
return gaugeVecMetrics[key]
|
|
|
}
|
|
|
-
|
|
|
-// Refine the given Prometheus Labels with values from a map `newLabels`
|
|
|
-// Only overwrite a value if the corresponding key is present in the
|
|
|
-// Prometheus' Labels already to avoid adding label names which are
|
|
|
-// not defined in a metric's description. Note that non-alphanumeric
|
|
|
-// characters from keys of `newLabels` are replaced by an underscore
|
|
|
-// because Prometheus does not accept non-alphanumeric, non-underscore
|
|
|
-// characters in label names.
|
|
|
-func RefineLabels(promLabels prometheus.Labels, newLabels map[string]string) prometheus.Labels {
|
|
|
- nonAlphanumericRegex := regexp.MustCompile(`[^a-zA-Z0-9 ]+`)
|
|
|
- var refinement = prometheus.Labels{}
|
|
|
-
|
|
|
- for k, v := range promLabels {
|
|
|
- refinement[k] = v
|
|
|
- }
|
|
|
-
|
|
|
- for k, v := range newLabels {
|
|
|
- cleanKey := nonAlphanumericRegex.ReplaceAllString(k, "_")
|
|
|
- if _, ok := refinement[cleanKey]; ok {
|
|
|
- refinement[cleanKey] = v
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return refinement
|
|
|
-}
|
|
|
-
|
|
|
-func RefineNonConditionMetricLabels(labels map[string]string) prometheus.Labels {
|
|
|
- return RefineLabels(NonConditionMetricLabels, labels)
|
|
|
-}
|
|
|
-
|
|
|
-func RefineConditionMetricLabels(labels map[string]string) prometheus.Labels {
|
|
|
- return RefineLabels(ConditionMetricLabels, labels)
|
|
|
-}
|