Skip to content

Commit

Permalink
display gauge during initial delay
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon committed May 10, 2024
1 parent a85b4c9 commit 89869f7
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions dash/dash.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/mum4k/termdash/linestyle"
"github.com/mum4k/termdash/terminal/tcell"
"github.com/mum4k/termdash/terminal/terminalapi"
"github.com/mum4k/termdash/widgets/gauge"
"github.com/mum4k/termdash/widgets/linechart"
"github.com/prometheus/prometheus/promql"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -78,8 +79,10 @@ func (d *Dash) Run(ctx context.Context, in <-chan promql.Matrix) error {
return fmt.Errorf("error creating terminal: %w", err)
}
defer terminal.Close()

c, err := container.New(
terminal,
container.ID("root"),
container.Border(linestyle.Light),
container.BorderTitle("PRESS Q TO QUIT"),
container.PlaceWidget(d.chart),
Expand All @@ -89,6 +92,14 @@ func (d *Dash) Run(ctx context.Context, in <-chan promql.Matrix) error {
}

group, ctx := errgroup.WithContext(ctx)

// ---- remove this once the initial Prometheus delay is fixed ----
err = d.setupInitialDelayGauge(ctx, c, group)
if err != nil {
return err
}
// ----------------------------------------------------------------

group.Go(func() error {
return d.drawLineChart(ctx)
})
Expand Down Expand Up @@ -197,3 +208,63 @@ func (d *Dash) xLabels() map[int]string {
}
return labels
}

// setupInitialDelayGauge displays a gauge to inform the user about the initial
// scrape delay.
func (d *Dash) setupInitialDelayGauge(ctx context.Context, c *container.Container, group *errgroup.Group) error {
const initialDelay = 5 // Prometheus has a hardcoded 5-second delay before it starts the first scrape
initialGaugeText := "%d - Please wait for Prometheus to start scraping the target..."
initialGauge, err := gauge.New(
gauge.Height(3),
gauge.TextLabel(fmt.Sprintf(initialGaugeText, initialDelay)),
gauge.Color(cell.ColorGreen),
gauge.FilledTextColor(cell.ColorBlack),
gauge.EmptyTextColor(cell.ColorGreen),
gauge.HideTextProgress(),
gauge.Border(linestyle.Light),
)
if err != nil {
return fmt.Errorf("error creating initial gauge: %w", err)
}
err = initialGauge.Absolute(0, initialDelay)
if err != nil {
return fmt.Errorf("error setting initial gauge: %w", err)
}
err = c.Update(
"root",
container.Border(linestyle.Light),
container.BorderTitle("PRESS Q TO QUIT"),
container.PlaceWidget(initialGauge),
)
if err != nil {
return fmt.Errorf("error updating container: %w", err)
}

group.Go(func() error {
for i := 0; i < initialDelay; i++ {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
err := initialGauge.Absolute(
i+1, initialDelay,
gauge.TextLabel(fmt.Sprintf(initialGaugeText, initialDelay-i-1)),
)
if err != nil {
return fmt.Errorf("error updating gauge: %w", err)
}
}
}
err := c.Update(
"root",
container.Border(linestyle.Light),
container.BorderTitle("PRESS Q TO QUIT"),
container.PlaceWidget(d.chart),
)
if err != nil {
return fmt.Errorf("error updating container: %w", err)
}
return nil
})
return nil
}

0 comments on commit 89869f7

Please sign in to comment.