forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEV-43895: expose putAlerts on 10.4.x release branch (#14)
* DEV-43895: expose putAlerts
- Loading branch information
1 parent
05db783
commit 21c91ca
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,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 |
This file contains 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,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 |
This file contains 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