diff --git a/go.mod b/go.mod index d43734739..ae5ea344b 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/go-logr/logr v1.4.2 github.com/go-logr/zapr v1.3.0 github.com/kubevirt/monitoring/pkg/metrics/parser v0.0.0-20230710120526-cc1644c90b64 - github.com/machadovilaca/operator-observability v0.0.21 + github.com/machadovilaca/operator-observability v0.0.24 github.com/onsi/ginkgo/v2 v2.22.0 github.com/onsi/gomega v1.35.1 github.com/openshift/api v3.9.1-0.20190924102528-32369d4db2ad+incompatible diff --git a/go.sum b/go.sum index f4cd35f55..0bbecc1ac 100644 --- a/go.sum +++ b/go.sum @@ -147,8 +147,8 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/kubevirt/monitoring/pkg/metrics/parser v0.0.0-20230710120526-cc1644c90b64 h1:WghbbS7pdWAsMSouy7KG8TEP/RIAYDTJdrth1bpXUXk= github.com/kubevirt/monitoring/pkg/metrics/parser v0.0.0-20230710120526-cc1644c90b64/go.mod h1:qGj2agzgwQ27nYhP3xhLs+IBzE5+ALNUg8bDfMcwPqo= -github.com/machadovilaca/operator-observability v0.0.21 h1:H0nH+45wtqsxCrJOm8wmcrJLuRZ5QY6iTlHKMlMEZss= -github.com/machadovilaca/operator-observability v0.0.21/go.mod h1:trC+BjI6zhvZMBcX0q7vHrKKRX3lWZdwJxgVUmRJCfw= +github.com/machadovilaca/operator-observability v0.0.24 h1:VQuTH7FAYzd/0HC11bHumHCNdOji7zMragQEpgcWRSI= +github.com/machadovilaca/operator-observability v0.0.24/go.mod h1:trC+BjI6zhvZMBcX0q7vHrKKRX3lWZdwJxgVUmRJCfw= github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc= github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/collector.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/collector.go index 361d206aa..6cbbd4a3a 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/collector.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/collector.go @@ -3,7 +3,6 @@ package operatormetrics import ( "fmt" "strings" - "time" "github.com/prometheus/client_golang/prometheus" ) @@ -20,14 +19,6 @@ type Collector struct { CollectCallback func() []CollectorResult } -type CollectorResult struct { - Metric Metric - Labels []string - ConstLabels map[string]string - Value float64 - Timestamp time.Time -} - func (c Collector) hash() string { var sb strings.Builder @@ -40,7 +31,7 @@ func (c Collector) hash() string { func (c Collector) Describe(ch chan<- *prometheus.Desc) { for _, cm := range c.Metrics { - cm.getCollector().Describe(ch) + cm.GetCollector().Describe(ch) } } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/collector_result.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/collector_result.go new file mode 100644 index 000000000..40d0e8966 --- /dev/null +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/collector_result.go @@ -0,0 +1,24 @@ +package operatormetrics + +import ( + "errors" + "time" +) + +type CollectorResult struct { + Metric Metric + Labels []string + ConstLabels map[string]string + Value float64 + Timestamp time.Time +} + +func (cr CollectorResult) GetLabelValue(key string) (string, error) { + for i, label := range cr.Metric.GetOpts().labels { + if label == key { + return cr.Labels[i], nil + } + } + + return "", errors.New("label not found") +} diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/counter.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/counter.go index ddca79de4..4d1e57f0e 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/counter.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/counter.go @@ -31,6 +31,6 @@ func (c *Counter) GetBaseType() MetricType { return CounterType } -func (c *Counter) getCollector() prometheus.Collector { +func (c *Counter) GetCollector() prometheus.Collector { return c.Counter } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/counter_vec.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/counter_vec.go index 568ce6c7b..5554513a8 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/counter_vec.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/counter_vec.go @@ -33,6 +33,6 @@ func (c *CounterVec) GetBaseType() MetricType { return CounterType } -func (c *CounterVec) getCollector() prometheus.Collector { +func (c *CounterVec) GetCollector() prometheus.Collector { return c.CounterVec } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/gauge.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/gauge.go index de2699b2c..43250ed42 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/gauge.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/gauge.go @@ -31,6 +31,6 @@ func (c *Gauge) GetBaseType() MetricType { return GaugeType } -func (c *Gauge) getCollector() prometheus.Collector { +func (c *Gauge) GetCollector() prometheus.Collector { return c.Gauge } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/gauge_vec.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/gauge_vec.go index c771dece9..d9dca423f 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/gauge_vec.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/gauge_vec.go @@ -33,6 +33,6 @@ func (c *GaugeVec) GetBaseType() MetricType { return GaugeType } -func (c *GaugeVec) getCollector() prometheus.Collector { +func (c *GaugeVec) GetCollector() prometheus.Collector { return c.GaugeVec } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/histogram.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/histogram.go index b764835e2..43f2dc365 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/histogram.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/histogram.go @@ -46,6 +46,6 @@ func (c *Histogram) GetBaseType() MetricType { return HistogramType } -func (c *Histogram) getCollector() prometheus.Collector { +func (c *Histogram) GetCollector() prometheus.Collector { return c.Histogram } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/histogram_vec.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/histogram_vec.go index f272495f1..1157be4e9 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/histogram_vec.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/histogram_vec.go @@ -37,6 +37,6 @@ func (c *HistogramVec) GetBaseType() MetricType { return HistogramType } -func (c *HistogramVec) getCollector() prometheus.Collector { +func (c *HistogramVec) GetCollector() prometheus.Collector { return c.HistogramVec } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/metric.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/metric.go index 108d86bb9..932541eec 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/metric.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/metric.go @@ -15,8 +15,7 @@ type Metric interface { GetOpts() MetricOpts GetType() MetricType GetBaseType() MetricType - - getCollector() prometheus.Collector + GetCollector() prometheus.Collector } type MetricType string diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/summary.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/summary.go index 3bf9d37c8..de561ef8b 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/summary.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/summary.go @@ -46,6 +46,6 @@ func (c *Summary) GetBaseType() MetricType { return SummaryType } -func (c *Summary) getCollector() prometheus.Collector { +func (c *Summary) GetCollector() prometheus.Collector { return c.Summary } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/summary_vec.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/summary_vec.go index 6ea8bc542..db314b53a 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/summary_vec.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/summary_vec.go @@ -37,6 +37,6 @@ func (c *SummaryVec) GetBaseType() MetricType { return SummaryType } -func (c *SummaryVec) getCollector() prometheus.Collector { +func (c *SummaryVec) GetCollector() prometheus.Collector { return c.SummaryVec } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/wrapper_registry.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/wrapper_registry.go index 02a59a422..09977ba93 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/wrapper_registry.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatormetrics/wrapper_registry.go @@ -122,7 +122,7 @@ func metricExists(metric Metric) bool { } func unregisterMetric(metric Metric) error { - if succeeded := Unregister(metric.getCollector()); succeeded { + if succeeded := Unregister(metric.GetCollector()); succeeded { delete(operatorRegistry.registeredMetrics, metric.GetOpts().Name) return nil } @@ -131,7 +131,7 @@ func unregisterMetric(metric Metric) error { } func registerMetric(metric Metric) error { - err := Register(metric.getCollector()) + err := Register(metric.GetCollector()) if err != nil { return err } diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/compatibility.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/compatibility.go new file mode 100644 index 000000000..a7277fc5c --- /dev/null +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/compatibility.go @@ -0,0 +1,37 @@ +package operatorrules + +import promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" + +// Deprecated: operatorRegistry is deprecated. +var operatorRegistry = NewRegistry() + +// Deprecated: RegisterRecordingRules is deprecated. +func RegisterRecordingRules(recordingRules ...[]RecordingRule) error { + return operatorRegistry.RegisterRecordingRules(recordingRules...) +} + +// Deprecated: RegisterAlerts is deprecated. +func RegisterAlerts(alerts ...[]promv1.Rule) error { + return operatorRegistry.RegisterAlerts(alerts...) +} + +// Deprecated: ListRecordingRules is deprecated. +func ListRecordingRules() []RecordingRule { + return operatorRegistry.ListRecordingRules() +} + +// Deprecated: ListAlerts is deprecated. +func ListAlerts() []promv1.Rule { + return operatorRegistry.ListAlerts() +} + +// Deprecated: CleanRegistry is deprecated. +func CleanRegistry() error { + operatorRegistry = NewRegistry() + return nil +} + +// Deprecated: BuildPrometheusRule is deprecated. +func BuildPrometheusRule(name, namespace string, labels map[string]string) (*promv1.PrometheusRule, error) { + return operatorRegistry.BuildPrometheusRule(name, namespace, labels) +} diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/prometheusrules.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/prometheusrules.go index 88000e635..fd3f7e7f3 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/prometheusrules.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/prometheusrules.go @@ -11,8 +11,8 @@ import ( ) // BuildPrometheusRule builds a PrometheusRule object from the registered recording rules and alerts. -func BuildPrometheusRule(name, namespace string, labels map[string]string) (*promv1.PrometheusRule, error) { - spec, err := buildPrometheusRuleSpec() +func (r *Registry) BuildPrometheusRule(name, namespace string, labels map[string]string) (*promv1.PrometheusRule, error) { + spec, err := r.buildPrometheusRuleSpec() if err != nil { return nil, err } @@ -31,20 +31,20 @@ func BuildPrometheusRule(name, namespace string, labels map[string]string) (*pro }, nil } -func buildPrometheusRuleSpec() (*promv1.PrometheusRuleSpec, error) { +func (r *Registry) buildPrometheusRuleSpec() (*promv1.PrometheusRuleSpec, error) { var groups []promv1.RuleGroup - if len(operatorRegistry.registeredRecordingRules) != 0 { + if len(r.registeredRecordingRules) != 0 { groups = append(groups, promv1.RuleGroup{ Name: "recordingRules.rules", - Rules: buildRecordingRulesRules(), + Rules: r.buildRecordingRulesRules(), }) } - if len(operatorRegistry.registeredAlerts) != 0 { + if len(r.registeredAlerts) != 0 { groups = append(groups, promv1.RuleGroup{ Name: "alerts.rules", - Rules: ListAlerts(), + Rules: r.ListAlerts(), }) } @@ -55,10 +55,10 @@ func buildPrometheusRuleSpec() (*promv1.PrometheusRuleSpec, error) { return &promv1.PrometheusRuleSpec{Groups: groups}, nil } -func buildRecordingRulesRules() []promv1.Rule { +func (r *Registry) buildRecordingRulesRules() []promv1.Rule { var rules []promv1.Rule - for _, recordingRule := range operatorRegistry.registeredRecordingRules { + for _, recordingRule := range r.registeredRecordingRules { rules = append(rules, promv1.Rule{ Record: recordingRule.MetricsOpts.Name, Expr: recordingRule.Expr, diff --git a/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/registry.go b/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/registry.go index fdef00604..a5e16f659 100644 --- a/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/registry.go +++ b/vendor/github.com/machadovilaca/operator-observability/pkg/operatorrules/registry.go @@ -7,26 +7,24 @@ import ( promv1 "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1" ) -var operatorRegistry = newRegistry() - -type operatorRegisterer struct { +type Registry struct { registeredRecordingRules map[string]RecordingRule registeredAlerts map[string]promv1.Rule } -func newRegistry() operatorRegisterer { - return operatorRegisterer{ +func NewRegistry() *Registry { + return &Registry{ registeredRecordingRules: map[string]RecordingRule{}, registeredAlerts: map[string]promv1.Rule{}, } } // RegisterRecordingRules registers the given recording rules. -func RegisterRecordingRules(recordingRules ...[]RecordingRule) error { +func (r *Registry) RegisterRecordingRules(recordingRules ...[]RecordingRule) error { for _, recordingRuleList := range recordingRules { for _, recordingRule := range recordingRuleList { key := recordingRule.MetricsOpts.Name + ":" + recordingRule.Expr.String() - operatorRegistry.registeredRecordingRules[key] = recordingRule + r.registeredRecordingRules[key] = recordingRule } } @@ -34,10 +32,11 @@ func RegisterRecordingRules(recordingRules ...[]RecordingRule) error { } // RegisterAlerts registers the given alerts. -func RegisterAlerts(alerts ...[]promv1.Rule) error { +func (r *Registry) RegisterAlerts(alerts ...[]promv1.Rule) error { for _, alertList := range alerts { for _, alert := range alertList { - operatorRegistry.registeredAlerts[alert.Alert] = alert + key := alert.Alert + ":" + alert.Expr.String() + r.registeredAlerts[key] = alert } } @@ -45,9 +44,9 @@ func RegisterAlerts(alerts ...[]promv1.Rule) error { } // ListRecordingRules returns the registered recording rules. -func ListRecordingRules() []RecordingRule { +func (r *Registry) ListRecordingRules() []RecordingRule { var rules []RecordingRule - for _, rule := range operatorRegistry.registeredRecordingRules { + for _, rule := range r.registeredRecordingRules { rules = append(rules, rule) } @@ -62,21 +61,18 @@ func ListRecordingRules() []RecordingRule { } // ListAlerts returns the registered alerts. -func ListAlerts() []promv1.Rule { +func (r *Registry) ListAlerts() []promv1.Rule { var alerts []promv1.Rule - for _, alert := range operatorRegistry.registeredAlerts { + for _, alert := range r.registeredAlerts { alerts = append(alerts, alert) } slices.SortFunc(alerts, func(a, b promv1.Rule) int { - return cmp.Compare(a.Alert, b.Alert) + aKey := a.Alert + ":" + a.Expr.String() + bKey := b.Alert + ":" + b.Expr.String() + + return cmp.Compare(aKey, bKey) }) return alerts } - -// CleanRegistry removes all registered rules and alerts. -func CleanRegistry() error { - operatorRegistry = newRegistry() - return nil -} diff --git a/vendor/modules.txt b/vendor/modules.txt index dc1311ce3..3766694bb 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -110,7 +110,7 @@ github.com/json-iterator/go # github.com/kubevirt/monitoring/pkg/metrics/parser v0.0.0-20230710120526-cc1644c90b64 ## explicit; go 1.20 github.com/kubevirt/monitoring/pkg/metrics/parser -# github.com/machadovilaca/operator-observability v0.0.21 +# github.com/machadovilaca/operator-observability v0.0.24 ## explicit; go 1.21 github.com/machadovilaca/operator-observability/pkg/docs github.com/machadovilaca/operator-observability/pkg/operatormetrics