Skip to content

Commit

Permalink
DEV-43895: expose putAlerts on 10.4.x release branch (#14)
Browse files Browse the repository at this point in the history
* DEV-43895: expose putAlerts
  • Loading branch information
Jonathan-Eng authored Apr 2, 2024
1 parent 05db783 commit 21c91ca
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
8 changes: 8 additions & 0 deletions pkg/services/ngalert/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
48 changes: 48 additions & 0 deletions pkg/services/ngalert/api/logzio_alerting_api.go
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
45 changes: 45 additions & 0 deletions pkg/services/ngalert/api/logzio_alerting_service.go
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
9 changes: 9 additions & 0 deletions pkg/services/ngalert/api/tooling/definitions/alertmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/grafana/grafana/pkg/services/ngalert/models"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 21c91ca

Please sign in to comment.