diff --git a/cmd/pint/ci.go b/cmd/pint/ci.go index eff87b83..3f655f43 100644 --- a/cmd/pint/ci.go +++ b/cmd/pint/ci.go @@ -105,6 +105,10 @@ func actionCI(c *cli.Context) error { gen := config.NewPrometheusGenerator(meta.cfg, metricsRegistry) defer gen.Stop() + if err = gen.GenerateStatic(); err != nil { + return err + } + slog.Debug("Generated all Prometheus servers", slog.Int("count", gen.Count())) summary, err := checkRules(ctx, meta.workers, gen, meta.cfg, entries) diff --git a/cmd/pint/lint.go b/cmd/pint/lint.go index c20f1de5..d402121f 100644 --- a/cmd/pint/lint.go +++ b/cmd/pint/lint.go @@ -65,6 +65,10 @@ func actionLint(c *cli.Context) error { gen := config.NewPrometheusGenerator(meta.cfg, metricsRegistry) defer gen.Stop() + if err = gen.GenerateStatic(); err != nil { + return err + } + summary, err := checkRules(ctx, meta.workers, gen, meta.cfg, entries) if err != nil { return err diff --git a/cmd/pint/scan.go b/cmd/pint/scan.go index 5fe06b57..79010fd9 100644 --- a/cmd/pint/scan.go +++ b/cmd/pint/scan.go @@ -56,7 +56,7 @@ func tryDecodingYamlError(err error) (l int, s string) { func checkRules(ctx context.Context, workers int, gen *config.PrometheusGenerator, cfg config.Config, entries []discovery.Entry) (summary reporter.Summary, err error) { if len(entries) > 0 { - if err = gen.Generate(ctx); err != nil { + if err = gen.GenerateDynamic(ctx); err != nil { return summary, err } slog.Debug("Generated all Prometheus servers", slog.Int("count", gen.Count())) diff --git a/cmd/pint/tests/0145_discovery_filepath_dup.txt b/cmd/pint/tests/0145_discovery_filepath_dup.txt index 9ddbeabb..f32b82d7 100644 --- a/cmd/pint/tests/0145_discovery_filepath_dup.txt +++ b/cmd/pint/tests/0145_discovery_filepath_dup.txt @@ -6,6 +6,7 @@ cmp stderr stderr.txt level=INFO msg="Loading configuration file" path=.pint.hcl level=INFO msg="Finding all rules to check" paths=["rules"] level=DEBUG msg="File parsed" path=rules/0001.yml rules=1 +level=INFO msg="Configured new Prometheus server" name=prom2 uris=1 tags=[] include=[] exclude=[] level=INFO msg="Finding Prometheus servers using file paths" dir=servers match=^(?P\w+).ya?ml$ level=DEBUG msg="Path discovery match" match=^(?P\w+).ya?ml$ path=prom1.yaml level=DEBUG msg="Extracted regexp variables" regexp=^(?P\w+).ya?ml$ vars={"name":"prom1"} @@ -19,6 +20,7 @@ level=DEBUG msg="Rendered Prometheus server" name=prom2 uri=https://prom2.exampl level=DEBUG msg="Path discovery match" match=^(?P\w+).ya?ml$ path=prom2.yml level=DEBUG msg="Extracted regexp variables" regexp=^(?P\w+).ya?ml$ vars={"name":"prom2"} level=DEBUG msg="Rendered Prometheus server" name=prom2 uri=https://prom2.example.com headers=[] timeout=5s concurrency=16 rateLimit=100 uptime=up tags=["name/prom2"] required=true +level=INFO msg="Configured new Prometheus server" name=prom1 uris=2 tags=["name/prom1"] include=[] exclude=[] level=ERROR msg="Fatal error" err="Duplicated name for Prometheus server definition: prom2" -- rules/0001.yml -- groups: diff --git a/cmd/pint/tests/0150_discovery_prom_dup_tags.txt b/cmd/pint/tests/0150_discovery_prom_dup_tags.txt index a4418d04..9543ee0e 100644 --- a/cmd/pint/tests/0150_discovery_prom_dup_tags.txt +++ b/cmd/pint/tests/0150_discovery_prom_dup_tags.txt @@ -18,6 +18,7 @@ level=DEBUG msg="Rendered Prometheus server" name=prom-ha uri=https://prom1.exam level=DEBUG msg="Rendered Prometheus server" name=prom-ha uri=https://prom2.example.com headers=["X-Host"] timeout=5s concurrency=16 rateLimit=100 uptime=up tags=["prom2"] required=false level=DEBUG msg="Stopping query workers" name=discovery uri=http://127.0.0.1:7150 level=WARN msg="Duplicated prometheus server with different tags" name=prom-ha a=["prom2"] b=["prom1"] +level=INFO msg="Configured new Prometheus server" name=prom-ha uris=1 tags=["prom1"] include=[] exclude=[] level=ERROR msg="Fatal error" err="Duplicated name for Prometheus server definition: prom-ha" -- rules/0001.yml -- groups: diff --git a/cmd/pint/tests/0155_discovery_prom_dup_include.txt b/cmd/pint/tests/0155_discovery_prom_dup_include.txt index f2b17f9f..0c69eea1 100644 --- a/cmd/pint/tests/0155_discovery_prom_dup_include.txt +++ b/cmd/pint/tests/0155_discovery_prom_dup_include.txt @@ -18,6 +18,7 @@ level=DEBUG msg="Rendered Prometheus server" name=prom-ha uri=https://prom1.exam level=DEBUG msg="Rendered Prometheus server" name=prom-ha uri=https://prom2.example.com headers=[] timeout=2m0s concurrency=16 rateLimit=100 uptime=up tags=[] required=false level=DEBUG msg="Stopping query workers" name=discovery uri=http://127.0.0.1:7155 level=WARN msg="Duplicated prometheus server with different include" name=prom-ha a=["^prom2$"] b=["^prom1$"] +level=INFO msg="Configured new Prometheus server" name=prom-ha uris=1 tags=[] include=["^prom1$"] exclude=[] level=ERROR msg="Fatal error" err="Duplicated name for Prometheus server definition: prom-ha" -- rules/0001.yml -- groups: diff --git a/cmd/pint/tests/0156_discovery_prom_dup_exclude.txt b/cmd/pint/tests/0156_discovery_prom_dup_exclude.txt index 3435e2dc..136227d7 100644 --- a/cmd/pint/tests/0156_discovery_prom_dup_exclude.txt +++ b/cmd/pint/tests/0156_discovery_prom_dup_exclude.txt @@ -18,6 +18,7 @@ level=DEBUG msg="Rendered Prometheus server" name=prom-ha uri=https://prom1.exam level=DEBUG msg="Rendered Prometheus server" name=prom-ha uri=https://prom2.example.com headers=[] timeout=2m0s concurrency=16 rateLimit=100 uptime=up tags=[] required=false level=DEBUG msg="Stopping query workers" name=discovery uri=http://127.0.0.1:7156 level=WARN msg="Duplicated prometheus server with different exclude" name=prom-ha a=["^prom2$"] b=["^prom1$"] +level=INFO msg="Configured new Prometheus server" name=prom-ha uris=1 tags=[] include=[] exclude=["^prom1$"] level=ERROR msg="Fatal error" err="Duplicated name for Prometheus server definition: prom-ha" -- rules/0001.yml -- groups: diff --git a/cmd/pint/watch.go b/cmd/pint/watch.go index d7e767aa..d71cf3c2 100644 --- a/cmd/pint/watch.go +++ b/cmd/pint/watch.go @@ -151,6 +151,9 @@ func actionWatch(c *cli.Context) error { interval := c.Duration(intervalFlag) gen := config.NewPrometheusGenerator(meta.cfg, metricsRegistry) + if err = gen.GenerateStatic(); err != nil { + return err + } // start timer to run every $interval ack := make(chan bool, 1) diff --git a/docs/changelog.md b/docs/changelog.md index abde429c..d3b9eca8 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,11 @@ # Changelog +## v0.48.2 + +### Fixed + +- Fixed `pint watch` to correctly work with `discovery`. + ## v0.48.1 - Prometheus `template` under `discovery` block can now template diff --git a/internal/config/config_test.go b/internal/config/config_test.go index e12633d7..d3465d71 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -47,7 +47,7 @@ prometheus "prom" { gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry()) defer gen.Stop() - require.NoError(t, gen.Generate(context.Background())) + require.NoError(t, gen.GenerateStatic()) require.Empty(t, cfg.Checks.Disabled) @@ -68,7 +68,7 @@ func TestDisableOnlineChecksWithoutPrometheus(t *testing.T) { gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry()) defer gen.Stop() - require.NoError(t, gen.Generate(context.Background())) + require.NoError(t, gen.GenerateStatic()) require.Empty(t, cfg.Checks.Disabled) @@ -94,7 +94,7 @@ prometheus "prom" { gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry()) defer gen.Stop() - require.NoError(t, gen.Generate(context.Background())) + require.NoError(t, gen.GenerateStatic()) require.Empty(t, cfg.Checks.Disabled) @@ -121,7 +121,7 @@ func TestSetDisabledChecks(t *testing.T) { gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry()) defer gen.Stop() - require.NoError(t, gen.Generate(context.Background())) + require.NoError(t, gen.GenerateStatic()) require.Empty(t, cfg.Checks.Disabled) @@ -1549,7 +1549,7 @@ rule { gen := config.NewPrometheusGenerator(cfg, prometheus.NewRegistry()) defer gen.Stop() - require.NoError(t, gen.Generate(ctx)) + require.NoError(t, gen.GenerateStatic()) checks := cfg.GetChecksForRule(ctx, gen, tc.path, tc.rule, tc.disabledChecks) checkNames := make([]string, 0, len(checks)) diff --git a/internal/config/prometheus.go b/internal/config/prometheus.go index a0b938f5..a8f3d484 100644 --- a/internal/config/prometheus.go +++ b/internal/config/prometheus.go @@ -227,17 +227,28 @@ func (pg *PrometheusGenerator) addServer(server *promapi.FailoverGroup) error { } } pg.servers = append(pg.servers, server) + slog.Info( + "Configured new Prometheus server", + slog.String("name", server.Name()), + slog.Int("uris", server.ServerCount()), + slog.Any("tags", server.Tags()), + slog.Any("include", server.Include()), + slog.Any("exclude", server.Exclude()), + ) return nil } -func (pg *PrometheusGenerator) Generate(ctx context.Context) (err error) { +func (pg *PrometheusGenerator) GenerateStatic() (err error) { for _, pc := range pg.cfg.Prometheus { err = pg.addServer(newFailoverGroup(pc)) if err != nil { return err } } + return nil +} +func (pg *PrometheusGenerator) GenerateDynamic(ctx context.Context) (err error) { if pg.cfg.Discovery != nil { servers, err := pg.cfg.Discovery.Discover(ctx) if err != nil { @@ -250,17 +261,5 @@ func (pg *PrometheusGenerator) Generate(ctx context.Context) (err error) { } } } - - for _, server := range pg.servers { - slog.Info( - "Configured new Prometheus server", - slog.String("name", server.Name()), - slog.Int("uris", server.ServerCount()), - slog.Any("tags", server.Tags()), - slog.Any("include", server.Include()), - slog.Any("exclude", server.Exclude()), - ) - } - return nil }