-
Notifications
You must be signed in to change notification settings - Fork 2.5k
amtool: command to test receivers #4807
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
Draft
SoloJacobs
wants to merge
18
commits into
prometheus:main
Choose a base branch
from
SoloJacobs:pull3491
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
90e4220
Test receivers command setup
OktarianTB 84e277e
First iteration to test receivers
OktarianTB 1614b04
Improvements to test receivers
OktarianTB a047f44
Refactor code
OktarianTB 27aa009
Add alert.file option to mock alert
OktarianTB de85f29
Add tests for test receivers command
OktarianTB 2b81b41
update mock url
OktarianTB 9bc100c
fix linting
OktarianTB af0bbca
Fix updated FromGlobs signature
alexweav ec90837
Update to latest config definition, use latest package path for kingpin
alexweav 8f0a520
Lint imports
alexweav 5e11f46
Lint again
alexweav 97ed856
One more time...
alexweav 27106b8
Add two missing receiver types
alexweav 9f63a48
fix compilation
SoloJacobs 139c67e
golangci-lint run --enable modernize --fix
SoloJacobs 715f078
go mod tidy
SoloJacobs d32d742
repair yamllint issues
SoloJacobs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,309 @@ | ||
| // Copyright 2022 Prometheus Team | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package cli | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "log/slog" | ||
| "maps" | ||
| "sort" | ||
| "time" | ||
|
|
||
| "github.com/prometheus/common/model" | ||
| "github.com/prometheus/common/promslog" | ||
| "golang.org/x/sync/errgroup" | ||
|
|
||
| "github.com/prometheus/alertmanager/config" | ||
| "github.com/prometheus/alertmanager/notify" | ||
| "github.com/prometheus/alertmanager/notify/discord" | ||
| "github.com/prometheus/alertmanager/notify/email" | ||
| "github.com/prometheus/alertmanager/notify/msteams" | ||
| "github.com/prometheus/alertmanager/notify/opsgenie" | ||
| "github.com/prometheus/alertmanager/notify/pagerduty" | ||
| "github.com/prometheus/alertmanager/notify/pushover" | ||
| "github.com/prometheus/alertmanager/notify/slack" | ||
| "github.com/prometheus/alertmanager/notify/sns" | ||
| "github.com/prometheus/alertmanager/notify/telegram" | ||
| "github.com/prometheus/alertmanager/notify/victorops" | ||
| "github.com/prometheus/alertmanager/notify/webex" | ||
| "github.com/prometheus/alertmanager/notify/webhook" | ||
| "github.com/prometheus/alertmanager/notify/wechat" | ||
| "github.com/prometheus/alertmanager/template" | ||
| "github.com/prometheus/alertmanager/types" | ||
| ) | ||
|
|
||
| const ( | ||
| maxTestReceiversWorkers = 10 | ||
| ) | ||
|
|
||
| var ErrNoReceivers = errors.New("no receivers with configuration set") | ||
|
|
||
| type TestReceiversParams struct { | ||
| Alert *TestReceiversAlertParams | ||
| Receivers []config.Receiver | ||
| } | ||
|
|
||
| type TestReceiversAlertParams struct { | ||
| Annotations model.LabelSet `yaml:"annotations,omitempty" json:"annotations,omitempty"` | ||
| Labels model.LabelSet `yaml:"labels,omitempty" json:"labels,omitempty"` | ||
| } | ||
|
|
||
| type TestReceiversResult struct { | ||
| Alert types.Alert | ||
| Receivers []TestReceiverResult | ||
| NotifedAt time.Time | ||
| } | ||
|
|
||
| type TestReceiverResult struct { | ||
| Name string | ||
| ConfigResults []TestReceiverConfigResult | ||
| } | ||
|
|
||
| type TestReceiverConfigResult struct { | ||
| Name string | ||
| Status string | ||
| Error error | ||
| } | ||
|
|
||
| func TestReceivers(ctx context.Context, c TestReceiversParams, tmpl *template.Template) (*TestReceiversResult, error) { | ||
| // now represents the start time of the test | ||
| now := time.Now() | ||
| testAlert := newTestAlert(c, now, now) | ||
|
|
||
| // we must set a group key that is unique per test as some receivers use this key to deduplicate alerts | ||
| ctx = notify.WithGroupKey(ctx, testAlert.Labels.String()+now.String()) | ||
| // we must set group labels to avoid issues with templating | ||
| ctx = notify.WithGroupLabels(ctx, testAlert.Labels) | ||
|
|
||
| logger := promslog.New(&promslog.Config{}) | ||
|
|
||
| // job contains all metadata required to test a receiver | ||
| type job struct { | ||
| Receiver config.Receiver | ||
| Integration *notify.Integration | ||
| } | ||
|
|
||
| // result contains the receiver that was tested and an error that is non-nil if the test failed | ||
| type result struct { | ||
| Receiver config.Receiver | ||
| Integration *notify.Integration | ||
| Error error | ||
| } | ||
|
|
||
| newTestReceiversResult := func(alert types.Alert, results []result, notifiedAt time.Time) *TestReceiversResult { | ||
| m := make(map[string]TestReceiverResult) | ||
| for _, receiver := range c.Receivers { | ||
| // set up the result for this receiver | ||
| m[receiver.Name] = TestReceiverResult{ | ||
| Name: receiver.Name, | ||
| ConfigResults: []TestReceiverConfigResult{}, | ||
| } | ||
| } | ||
| for _, result := range results { | ||
| tmp := m[result.Receiver.Name] | ||
| status := "ok" | ||
| if result.Error != nil { | ||
| status = "failed" | ||
| } | ||
| tmp.ConfigResults = append(tmp.ConfigResults, TestReceiverConfigResult{ | ||
| Name: result.Integration.Name(), | ||
| Status: status, | ||
| Error: result.Error, | ||
| }) | ||
| m[result.Receiver.Name] = tmp | ||
| } | ||
| v := new(TestReceiversResult) | ||
| v.Alert = alert | ||
| v.Receivers = make([]TestReceiverResult, 0, len(c.Receivers)) | ||
| v.NotifedAt = notifiedAt | ||
| for _, result := range m { | ||
| v.Receivers = append(v.Receivers, result) | ||
| } | ||
|
|
||
| // Make sure the return order is deterministic. | ||
| sort.Slice(v.Receivers, func(i, j int) bool { | ||
| return v.Receivers[i].Name < v.Receivers[j].Name | ||
| }) | ||
|
|
||
| return v | ||
| } | ||
|
|
||
| // invalid keeps track of all invalid receiver configurations | ||
| var invalid []result | ||
| // jobs keeps track of all receivers that need to be sent test notifications | ||
| var jobs []job | ||
|
|
||
| for _, receiver := range c.Receivers { | ||
| integrations := buildReceiverIntegrations(receiver, tmpl, logger) | ||
| for _, integration := range integrations { | ||
| if integration.Error != nil { | ||
| invalid = append(invalid, result{ | ||
| Receiver: receiver, | ||
| Integration: &integration.Integration, | ||
| Error: integration.Error, | ||
| }) | ||
| } else { | ||
| jobs = append(jobs, job{ | ||
| Receiver: receiver, | ||
| Integration: &integration.Integration, | ||
| }) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if len(invalid)+len(jobs) == 0 { | ||
| return nil, ErrNoReceivers | ||
| } | ||
|
|
||
| if len(jobs) == 0 { | ||
| return newTestReceiversResult(testAlert, invalid, now), nil | ||
| } | ||
|
|
||
| numWorkers := min(maxTestReceiversWorkers, len(jobs)) | ||
|
|
||
| resultCh := make(chan result, len(jobs)) | ||
| jobCh := make(chan job, len(jobs)) | ||
| for _, job := range jobs { | ||
| jobCh <- job | ||
| } | ||
| close(jobCh) | ||
|
|
||
| g, ctx := errgroup.WithContext(ctx) | ||
| for range numWorkers { | ||
| g.Go(func() error { | ||
| for job := range jobCh { | ||
| v := result{ | ||
| Receiver: job.Receiver, | ||
| Integration: job.Integration, | ||
| } | ||
| if _, err := job.Integration.Notify(notify.WithReceiverName(ctx, job.Receiver.Name), &testAlert); err != nil { | ||
| v.Error = err | ||
| } | ||
| resultCh <- v | ||
| } | ||
| return nil | ||
| }) | ||
| } | ||
| g.Wait() // nolint | ||
| close(resultCh) | ||
|
|
||
| results := make([]result, 0, len(jobs)) | ||
| for next := range resultCh { | ||
| results = append(results, next) | ||
| } | ||
|
|
||
| return newTestReceiversResult(testAlert, append(invalid, results...), now), nil | ||
| } | ||
|
|
||
| func newTestAlert(c TestReceiversParams, startsAt, updatedAt time.Time) types.Alert { | ||
| var ( | ||
| defaultAnnotations = model.LabelSet{ | ||
| "summary": "Notification test", | ||
| "__value_string__": "[ metric='foo' labels={instance=bar} value=10 ]", | ||
| } | ||
| defaultLabels = model.LabelSet{ | ||
| "alertname": "TestAlert", | ||
| "instance": "Alertmanager", | ||
| } | ||
| ) | ||
|
|
||
| alert := types.Alert{ | ||
| Alert: model.Alert{ | ||
| Labels: defaultLabels, | ||
| Annotations: defaultAnnotations, | ||
| StartsAt: startsAt, | ||
| }, | ||
| UpdatedAt: updatedAt, | ||
| } | ||
|
|
||
| if c.Alert != nil { | ||
| if c.Alert.Annotations != nil { | ||
| maps.Copy(alert.Annotations, c.Alert.Annotations) | ||
| } | ||
| if c.Alert.Labels != nil { | ||
| maps.Copy(alert.Labels, c.Alert.Labels) | ||
| } | ||
| } | ||
|
|
||
| return alert | ||
| } | ||
|
|
||
| type ReceiverIntegration struct { | ||
| Integration notify.Integration | ||
| Error error | ||
| } | ||
|
|
||
| // buildReceiverIntegrations builds a list of integration notifiers off of a | ||
| // receiver config. | ||
| func buildReceiverIntegrations(nc config.Receiver, tmpl *template.Template, logger *slog.Logger) []ReceiverIntegration { | ||
| var ( | ||
| integrations []ReceiverIntegration | ||
| add = func(name string, i int, rs notify.ResolvedSender, f func(l *slog.Logger) (notify.Notifier, error)) { | ||
| n, err := f(logger.With("integration", name)) | ||
| if err != nil { | ||
| integrations = append(integrations, ReceiverIntegration{ | ||
| Integration: notify.NewIntegration(nil, rs, name, i, nc.Name), | ||
| Error: err, | ||
| }) | ||
| } else { | ||
| integrations = append(integrations, ReceiverIntegration{ | ||
| Integration: notify.NewIntegration(n, rs, name, i, nc.Name), | ||
| }) | ||
| } | ||
| } | ||
| ) | ||
|
|
||
| for i, c := range nc.WebhookConfigs { | ||
| add("webhook", i, c, func(l *slog.Logger) (notify.Notifier, error) { return webhook.New(c, tmpl, l) }) | ||
| } | ||
|
Comment on lines
+268
to
+270
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doing this per receiver seems excessive to me. |
||
| for i, c := range nc.EmailConfigs { | ||
| add("email", i, c, func(l *slog.Logger) (notify.Notifier, error) { return email.New(c, tmpl, l), nil }) | ||
| } | ||
| for i, c := range nc.PagerdutyConfigs { | ||
| add("pagerduty", i, c, func(l *slog.Logger) (notify.Notifier, error) { return pagerduty.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.OpsGenieConfigs { | ||
| add("opsgenie", i, c, func(l *slog.Logger) (notify.Notifier, error) { return opsgenie.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.WechatConfigs { | ||
| add("wechat", i, c, func(l *slog.Logger) (notify.Notifier, error) { return wechat.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.SlackConfigs { | ||
| add("slack", i, c, func(l *slog.Logger) (notify.Notifier, error) { return slack.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.VictorOpsConfigs { | ||
| add("victorops", i, c, func(l *slog.Logger) (notify.Notifier, error) { return victorops.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.PushoverConfigs { | ||
| add("pushover", i, c, func(l *slog.Logger) (notify.Notifier, error) { return pushover.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.SNSConfigs { | ||
| add("sns", i, c, func(l *slog.Logger) (notify.Notifier, error) { return sns.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.TelegramConfigs { | ||
| add("telegram", i, c, func(l *slog.Logger) (notify.Notifier, error) { return telegram.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.DiscordConfigs { | ||
| add("discord", i, c, func(l *slog.Logger) (notify.Notifier, error) { return discord.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.WebexConfigs { | ||
| add("webex", i, c, func(l *slog.Logger) (notify.Notifier, error) { return webex.New(c, tmpl, l) }) | ||
| } | ||
| for i, c := range nc.MSTeamsConfigs { | ||
| add("msteams", i, c, func(l *slog.Logger) (notify.Notifier, error) { return msteams.New(c, tmpl, l) }) | ||
| } | ||
|
|
||
| return integrations | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.