2016-09-05 07:43:53 -05:00
|
|
|
package alerting
|
|
|
|
|
|
|
|
import (
|
2016-10-03 02:38:03 -05:00
|
|
|
"context"
|
2017-05-17 02:51:51 -05:00
|
|
|
"fmt"
|
2021-03-10 04:24:31 -06:00
|
|
|
"math/rand"
|
2021-02-03 13:47:45 -06:00
|
|
|
"net/http"
|
2016-10-03 02:38:03 -05:00
|
|
|
|
2017-01-13 05:32:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/null"
|
2016-09-05 07:43:53 -05:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2019-05-13 01:45:54 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2023-01-23 07:19:25 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/alerting/models"
|
2022-09-19 02:54:37 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/annotations/annotationstest"
|
2016-09-05 07:43:53 -05:00
|
|
|
)
|
|
|
|
|
2019-05-20 05:13:32 -05:00
|
|
|
// NotificationTestCommand initiates an test
|
|
|
|
// execution of an alert notification.
|
2016-09-05 07:43:53 -05:00
|
|
|
type NotificationTestCommand struct {
|
2020-07-08 03:17:05 -05:00
|
|
|
OrgID int64
|
|
|
|
ID int64
|
|
|
|
State models.AlertStateType
|
|
|
|
Name string
|
|
|
|
Type string
|
|
|
|
Settings *simplejson.Json
|
|
|
|
SecureSettings map[string]string
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2019-01-15 04:49:18 -06:00
|
|
|
var (
|
|
|
|
logger = log.New("alerting.testnotification")
|
|
|
|
)
|
|
|
|
|
2021-10-07 09:33:50 -05:00
|
|
|
func (s *AlertNotificationService) HandleNotificationTestCommand(ctx context.Context, cmd *NotificationTestCommand) error {
|
2022-02-03 06:26:05 -06:00
|
|
|
notificationSvc := newNotificationService(nil, nil, nil, nil)
|
2016-09-05 07:43:53 -05:00
|
|
|
|
2021-11-02 08:11:19 -05:00
|
|
|
model := models.AlertNotification{
|
|
|
|
Id: cmd.ID,
|
2022-01-05 03:19:45 -06:00
|
|
|
OrgId: cmd.OrgID,
|
2016-09-05 07:43:53 -05:00
|
|
|
Name: cmd.Name,
|
|
|
|
Type: cmd.Type,
|
|
|
|
Settings: cmd.Settings,
|
|
|
|
}
|
|
|
|
|
2021-11-02 08:11:19 -05:00
|
|
|
notifier, err := s.createNotifier(ctx, &model, cmd.SecureSettings)
|
2016-09-05 07:43:53 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-02 08:11:19 -05:00
|
|
|
return notificationSvc.sendNotifications(createTestEvalContext(cmd), notifierStateSlice{{notifier: notifier}})
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2017-02-13 05:43:12 -06:00
|
|
|
func createTestEvalContext(cmd *NotificationTestCommand) *EvalContext {
|
2016-09-05 07:43:53 -05:00
|
|
|
testRule := &Rule{
|
2019-06-03 03:25:58 -05:00
|
|
|
DashboardID: 1,
|
|
|
|
PanelID: 1,
|
2016-09-05 07:43:53 -05:00
|
|
|
Name: "Test notification",
|
2020-05-17 06:57:43 -05:00
|
|
|
Message: "Someone is testing the alert notification within Grafana.",
|
2019-05-14 01:15:05 -05:00
|
|
|
State: models.AlertStateAlerting,
|
2021-03-10 04:24:31 -06:00
|
|
|
ID: rand.Int63(),
|
2016-09-05 07:43:53 -05:00
|
|
|
}
|
|
|
|
|
2022-09-19 02:54:37 -05:00
|
|
|
ctx := NewEvalContext(context.Background(), testRule, fakeRequestValidator{}, nil, nil, nil, annotationstest.NewFakeAnnotationsRepo())
|
2017-02-13 05:43:12 -06:00
|
|
|
if cmd.Settings.Get("uploadImage").MustBool(true) {
|
2022-11-16 07:17:39 -06:00
|
|
|
ctx.ImagePublicURL = "https://grafana.com/static/assets/img/blog/mixed_styles.png"
|
2017-02-13 05:43:12 -06:00
|
|
|
}
|
2016-09-05 07:43:53 -05:00
|
|
|
ctx.IsTestRun = true
|
2016-09-13 13:14:18 -05:00
|
|
|
ctx.Firing = true
|
2020-11-05 04:57:20 -06:00
|
|
|
ctx.Error = fmt.Errorf("this is only a test")
|
2016-09-13 13:14:18 -05:00
|
|
|
ctx.EvalMatches = evalMatchesBasedOnState()
|
2016-09-05 07:43:53 -05:00
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|
|
|
|
|
2016-09-13 13:14:18 -05:00
|
|
|
func evalMatchesBasedOnState() []*EvalMatch {
|
2016-09-05 07:43:53 -05:00
|
|
|
matches := make([]*EvalMatch, 0)
|
|
|
|
matches = append(matches, &EvalMatch{
|
|
|
|
Metric: "High value",
|
2017-01-13 05:32:30 -06:00
|
|
|
Value: null.FloatFrom(100),
|
2016-09-05 07:43:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
matches = append(matches, &EvalMatch{
|
|
|
|
Metric: "Higher Value",
|
2017-01-13 05:32:30 -06:00
|
|
|
Value: null.FloatFrom(200),
|
2016-09-05 07:43:53 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
return matches
|
|
|
|
}
|
2021-02-03 13:47:45 -06:00
|
|
|
|
|
|
|
type fakeRequestValidator struct{}
|
|
|
|
|
|
|
|
func (fakeRequestValidator) Validate(_ string, _ *http.Request) error {
|
|
|
|
return nil
|
|
|
|
}
|