Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a formatter for config test routes (fixes #3741) #3844

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Formatter interface {
FormatAlerts([]*models.GettableAlert) error
FormatConfig(*models.AlertmanagerStatus) error
FormatClusterStatus(status *models.ClusterStatus) error
FormatAlertReceivers([]string) error
}

// Formatters is a map of cli argument names to formatter interface object.
Expand Down
14 changes: 14 additions & 0 deletions cli/format/format_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ func (formatter *ExtendedFormatter) FormatClusterStatus(status *models.ClusterSt
return w.Flush()
}

func (formatter *ExtendedFormatter) FormatAlertReceivers(receivers []string) error {
w := tabwriter.NewWriter(formatter.writer, 0, 0, 2, ' ', 0)
sort.Strings(receivers)
fmt.Fprintln(w, "Receiver\t")
for _, receiver := range receivers {
fmt.Fprintf(
w,
"%s\t\n",
receiver,
)
}
return w.Flush()
}

func extendedFormatLabels(labels models.LabelSet) string {
output := []string{}
for name, value := range labels {
Expand Down
5 changes: 5 additions & 0 deletions cli/format/format_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,8 @@ func (formatter *JSONFormatter) FormatClusterStatus(status *models.ClusterStatus
enc := json.NewEncoder(formatter.writer)
return enc.Encode(status)
}

func (formatter *JSONFormatter) FormatAlertReceivers(receivers []string) error {
enc := json.NewEncoder(formatter.writer)
return enc.Encode(receivers)
}
6 changes: 6 additions & 0 deletions cli/format/format_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ func (formatter *SimpleFormatter) FormatClusterStatus(status *models.ClusterStat
return w.Flush()
}

func (formatter *SimpleFormatter) FormatAlertReceivers(receivers []string) error {
receiversSlug := strings.Join(receivers, ",")
fmt.Fprintf(formatter.writer, "%s\n", receiversSlug)
return nil
}

func simpleFormatMatchers(matchers models.Matchers) string {
output := []string{}
for _, matcher := range matchers {
Expand Down
13 changes: 11 additions & 2 deletions cli/test_routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/xlab/treeprint"

"github.com/prometheus/alertmanager/api/v2/models"
"github.com/prometheus/alertmanager/cli/format"
"github.com/prometheus/alertmanager/dispatch"
"github.com/prometheus/alertmanager/matchers/compat"
"github.com/prometheus/alertmanager/pkg/labels"
Expand Down Expand Up @@ -79,6 +80,11 @@ func (c *routingShow) routingTestAction(ctx context.Context, _ *kingpin.ParseCon
return err
}

formatter, found := format.Formatters[output]
if !found {
return fmt.Errorf("unknown output formatter: %q", output)
}

mainRoute := dispatch.NewRoute(cfg.Route, nil)

// Parse labels to LabelSet.
Expand All @@ -99,9 +105,12 @@ func (c *routingShow) routingTestAction(ctx context.Context, _ *kingpin.ParseCon
}

receivers, err := resolveAlertReceivers(mainRoute, &ls)
receiversSlug := strings.Join(receivers, ",")
fmt.Printf("%s\n", receiversSlug)
if err := formatter.FormatAlertReceivers(receivers); err != nil {
kingpin.Fatalf("failed to format receivers: %v", err)
return err
}

receiversSlug := strings.Join(receivers, ",")
if c.expectedReceivers != "" && c.expectedReceivers != receiversSlug {
fmt.Printf("WARNING: Expected receivers did not match resolved receivers.\n")
os.Exit(1)
Expand Down