From 16ed30af70ae38258fd38e56f81f39232e251e81 Mon Sep 17 00:00:00 2001 From: Augustin Husson Date: Mon, 28 Oct 2024 15:03:30 +0100 Subject: [PATCH] give the possibility to define a set of a rule collectors instead of a single one Signed-off-by: Augustin Husson --- README.md | 10 ++++++---- config/config.go | 8 ++++---- dev/config.yaml | 8 ++++---- main.go | 13 +++++++------ source/rules/rules.go | 2 +- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index be14c24..f690f5b 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,8 @@ metric_collector: This collector gets the Prometheus Rules Group using the HTTP API. Then it extracts the metric used in the alert rule or in the recording rule. +You define a set of rule collectors as we are assuming rules can com from various Prometheus / Thanos rulers. + #### Configuration See the doc for the complete configuration [here](./docs/configuration.md#rules_collector-config) @@ -99,10 +101,10 @@ See the doc for the complete configuration [here](./docs/configuration.md#rules_ Example: ```yaml -rules_collector: - enable: true - prometheus_client: - url: "https://prometheus.demo.do.prometheus.io" +rules_collectors: + - enable: true + prometheus_client: + url: "https://prometheus.demo.do.prometheus.io" ``` ### Perses Collector diff --git a/config/config.go b/config/config.go index 88351f3..5892313 100644 --- a/config/config.go +++ b/config/config.go @@ -3,10 +3,10 @@ package config import "github.com/perses/common/config" type Config struct { - MetricCollector MetricCollector `yaml:"metric_collector,omitempty"` - RulesCollector RulesCollector `yaml:"rules_collector,omitempty"` - PersesCollector PersesCollector `yaml:"perses_collector,omitempty"` - GrafanaCollector GrafanaCollector `yaml:"grafana_collector,omitempty"` + MetricCollector MetricCollector `yaml:"metric_collector,omitempty"` + RulesCollectors []*RulesCollector `yaml:"rules_collectors,omitempty"` + PersesCollector PersesCollector `yaml:"perses_collector,omitempty"` + GrafanaCollector GrafanaCollector `yaml:"grafana_collector,omitempty"` } func Resolve(configFile string) (Config, error) { diff --git a/dev/config.yaml b/dev/config.yaml index 2c66013..0e30a71 100644 --- a/dev/config.yaml +++ b/dev/config.yaml @@ -3,10 +3,10 @@ metric_collector: http_client: url: "https://prometheus.demo.do.prometheus.io" -rules_collector: - enable: true - http_client: - url: "https://prometheus.demo.do.prometheus.io" +rules_collectors: + - enable: true + http_client: + url: "https://prometheus.demo.do.prometheus.io" perses_collector: enable: true diff --git a/main.go b/main.go index cfc412d..920334e 100644 --- a/main.go +++ b/main.go @@ -37,13 +37,14 @@ func main() { runner.WithTimerTasks(time.Duration(metricCollectorConfig.Period), metricCollector) } - if conf.RulesCollector.Enable { - rulesCollectorConfig := conf.RulesCollector - rulesCollector, collectorErr := rules.NewCollector(db, rulesCollectorConfig) - if collectorErr != nil { - logrus.WithError(collectorErr).Fatal("unable to create the rules collector") + for i, rulesCollectorConfig := range conf.RulesCollectors { + if rulesCollectorConfig.Enable { + rulesCollector, collectorErr := rules.NewCollector(db, rulesCollectorConfig) + if collectorErr != nil { + logrus.WithError(collectorErr).Fatalf("unable to create the rules collector number %d", i) + } + runner.WithTimerTasks(time.Duration(rulesCollectorConfig.Period), rulesCollector) } - runner.WithTimerTasks(time.Duration(rulesCollectorConfig.Period), rulesCollector) } if conf.PersesCollector.Enable { diff --git a/source/rules/rules.go b/source/rules/rules.go index 282040b..628b794 100644 --- a/source/rules/rules.go +++ b/source/rules/rules.go @@ -13,7 +13,7 @@ import ( "github.com/sirupsen/logrus" ) -func NewCollector(db database.Database, cfg config.RulesCollector) (async.SimpleTask, error) { +func NewCollector(db database.Database, cfg *config.RulesCollector) (async.SimpleTask, error) { promClient, err := prometheus.NewClient(cfg.HTTPClient) if err != nil { return nil, err