Skip to content

Commit

Permalink
Fixed watch mode
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Oct 30, 2023
1 parent 6dcd319 commit 1d83c8d
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 7 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
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
5 changes: 4 additions & 1 deletion internal/config/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,17 @@ func (pg *PrometheusGenerator) addServer(server *promapi.FailoverGroup) error {
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 Down

0 comments on commit 1d83c8d

Please sign in to comment.