From 21c91cab6e07b14e7dbf63375f22b96f065548dc Mon Sep 17 00:00:00 2001 From: Jonathan Engel <57597246+Jonathan-Eng@users.noreply.github.com> Date: Tue, 2 Apr 2024 13:47:37 +0300 Subject: [PATCH] DEV-43895: expose putAlerts on 10.4.x release branch (#14) * DEV-43895: expose putAlerts --- pkg/services/ngalert/api/api.go | 8 ++++ .../ngalert/api/logzio_alerting_api.go | 48 +++++++++++++++++++ .../ngalert/api/logzio_alerting_service.go | 45 +++++++++++++++++ .../api/tooling/definitions/alertmanager.go | 9 ++++ 4 files changed, 110 insertions(+) create mode 100644 pkg/services/ngalert/api/logzio_alerting_api.go create mode 100644 pkg/services/ngalert/api/logzio_alerting_service.go diff --git a/pkg/services/ngalert/api/api.go b/pkg/services/ngalert/api/api.go index 64ebc734b7798..ce2e50da53a2d 100644 --- a/pkg/services/ngalert/api/api.go +++ b/pkg/services/ngalert/api/api.go @@ -157,6 +157,14 @@ func (api *API) RegisterAPIEndpoints(m *metrics.API) { hist: api.Historian, }), m) + // LOGZ.IO GRAFANA CHANGE :: DEV-43895: add logzio alerting endpoints + api.RegisterLogzioAlertingApiEndpoints(NewLogzioAlertingApi( + NewLogzioAlertingService( + api.MultiOrgAlertmanager, + ), + ), m) + // LOGZ.IO GRAFANA CHANGE :: end + api.RegisterNotificationsApiEndpoints(NewNotificationsApi(&NotificationSrv{ logger: logger, receiverService: api.ReceiverService, diff --git a/pkg/services/ngalert/api/logzio_alerting_api.go b/pkg/services/ngalert/api/logzio_alerting_api.go new file mode 100644 index 0000000000000..a7273ca14ff47 --- /dev/null +++ b/pkg/services/ngalert/api/logzio_alerting_api.go @@ -0,0 +1,48 @@ +package api + +// LOGZ.IO GRAFANA CHANGE :: DEV-43895 (add endpoint to send alert notifications). +import ( + "github.com/grafana/grafana/pkg/api/response" + "github.com/grafana/grafana/pkg/api/routing" + contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" + apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" + "github.com/grafana/grafana/pkg/services/ngalert/metrics" + "github.com/grafana/grafana/pkg/web" + "net/http" +) + +type LogzioAlertingApi struct { + service *LogzioAlertingService +} + +// NewLogzioAlertingApi creates a new LogzioAlertingApi instance +func NewLogzioAlertingApi(service *LogzioAlertingService) *LogzioAlertingApi { + return &LogzioAlertingApi{ + service: service, + } +} + +func (api *API) RegisterLogzioAlertingApiEndpoints(srv *LogzioAlertingApi, m *metrics.API) { + api.RouteRegister.Group("", func(group routing.RouteRegister) { + group.Post( + toMacaronPath("/internal/alert/api/v1/send-notifications"), + metrics.Instrument( + http.MethodPost, + "/internal/alert/api/v1/send-notifications", + srv.RouteSendAlertNotifications, + m, + ), + ) + }) +} + +func (api *LogzioAlertingApi) RouteSendAlertNotifications(ctx *contextmodel.ReqContext) response.Response { + body := apimodels.AlertSendNotificationsRequest{} + if err := web.Bind(ctx.Req, &body); err != nil { + return response.Error(http.StatusBadRequest, "bad request data", err) + } + + return api.service.RouteSendAlertNotifications(ctx, body) +} + +// LOGZ.IO GRAFANA CHANGE :: end diff --git a/pkg/services/ngalert/api/logzio_alerting_service.go b/pkg/services/ngalert/api/logzio_alerting_service.go new file mode 100644 index 0000000000000..8a85e889cd0cc --- /dev/null +++ b/pkg/services/ngalert/api/logzio_alerting_service.go @@ -0,0 +1,45 @@ +package api + +// LOGZ.IO GRAFANA CHANGE :: DEV-43895 (add endpoint to send alert notifications). +import ( + "errors" + "github.com/grafana/grafana/pkg/api/response" + contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" + apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" + "github.com/grafana/grafana/pkg/services/ngalert/notifier" + "net/http" +) + +type LogzioAlertingService struct { + MultiOrgAlertmanager *notifier.MultiOrgAlertmanager +} + +func NewLogzioAlertingService( + MultiOrgAlertmanager *notifier.MultiOrgAlertmanager, +) *LogzioAlertingService { + return &LogzioAlertingService{ + MultiOrgAlertmanager: MultiOrgAlertmanager, + } +} + +func (srv *LogzioAlertingService) RouteSendAlertNotifications(c *contextmodel.ReqContext, sendNotificationsRequest apimodels.AlertSendNotificationsRequest) response.Response { + c.Logger.Info("Sending alerts to local notifier", "count", len(sendNotificationsRequest.Alerts.PostableAlerts)) + n, err := srv.MultiOrgAlertmanager.AlertmanagerFor(sendNotificationsRequest.AlertRuleKey.OrgID) + if err == nil { + if err := n.PutAlerts(c.Req.Context(), sendNotificationsRequest.Alerts); err != nil { + c.Logger.Error("Failed to put alerts in the local notifier", "count", len(sendNotificationsRequest.Alerts.PostableAlerts), "error", err) + } else { + return response.Success("Put alerts was successful") + } + } else { + if errors.Is(err, notifier.ErrNoAlertmanagerForOrg) { + c.Logger.Debug("Local notifier was not found") + } else { + c.Logger.Error("Local notifier is not available", "error", err) + } + } + + return response.Error(http.StatusInternalServerError, "Failed to put alerts", err) +} + +// LOGZ.IO GRAFANA CHANGE :: end diff --git a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go index 26b540e40174d..66dfccc606e4f 100644 --- a/pkg/services/ngalert/api/tooling/definitions/alertmanager.go +++ b/pkg/services/ngalert/api/tooling/definitions/alertmanager.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/grafana/grafana/pkg/services/ngalert/models" "reflect" "sort" "strings" @@ -551,6 +552,14 @@ type PostableUserConfig struct { amSimple map[string]interface{} `yaml:"-" json:"-"` } +// LOGZ.IO GRAFANA CHANGE :: DEV-43895 (add endpoint to send alert notifications). +type AlertSendNotificationsRequest struct { + AlertRuleKey models.AlertRuleKey `json:"alertRuleKey"` + Alerts PostableAlerts `json:"alerts"` +} + +// LOGZ.IO GRAFANA CHANGE :: end + func (c *PostableUserConfig) UnmarshalJSON(b []byte) error { type plain PostableUserConfig if err := json.Unmarshal(b, (*plain)(c)); err != nil {