Skip to content

Commit

Permalink
Merge pull request #766 from cloudflare/watch
Browse files Browse the repository at this point in the history
Fixed watch mode
  • Loading branch information
prymitive authored Oct 30, 2023
2 parents 6dcd319 + c78a195 commit 8a7b01d
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 19 deletions.
4 changes: 4 additions & 0 deletions cmd/pint/ci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions cmd/pint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion cmd/pint/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
Expand Down
2 changes: 2 additions & 0 deletions cmd/pint/tests/0145_discovery_filepath_dup.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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<name>\w+).ya?ml$
level=DEBUG msg="Path discovery match" match=^(?P<name>\w+).ya?ml$ path=prom1.yaml
level=DEBUG msg="Extracted regexp variables" regexp=^(?P<name>\w+).ya?ml$ vars={"name":"prom1"}
Expand All @@ -19,6 +20,7 @@ level=DEBUG msg="Rendered Prometheus server" name=prom2 uri=https://prom2.exampl
level=DEBUG msg="Path discovery match" match=^(?P<name>\w+).ya?ml$ path=prom2.yml
level=DEBUG msg="Extracted regexp variables" regexp=^(?P<name>\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:
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0150_discovery_prom_dup_tags.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0155_discovery_prom_dup_include.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions cmd/pint/tests/0156_discovery_prom_dup_exclude.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 3 additions & 0 deletions cmd/pint/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -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))
Expand Down
25 changes: 12 additions & 13 deletions internal/config/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 8a7b01d

Please sign in to comment.