-
Notifications
You must be signed in to change notification settings - Fork 0
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
DEV-43744-alert-eval-api #1
Changes from 1 commit
8e55785
9367eef
45278bb
6b35d6f
2d6abcd
4c1562d
40f495d
46080fc
0d114f4
ef76ff8
6d38146
3cacb1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
|
||
"github.com/grafana/grafana/pkg/api/dtos" | ||
"github.com/grafana/grafana/pkg/api/response" | ||
"github.com/grafana/grafana/pkg/models" // LOGZ.IO GRAFANA CHANGE :: DEV-17927 - add LogzIoHeaders | ||
"github.com/grafana/grafana/pkg/services/alerting" | ||
alertmodels "github.com/grafana/grafana/pkg/services/alerting/models" | ||
contextmodel "github.com/grafana/grafana/pkg/services/contexthandler/model" | ||
|
@@ -179,7 +180,8 @@ func (hs *HTTPServer) AlertTest(c *contextmodel.ReqContext) response.Response { | |
return response.Error(400, "The dashboard needs to be saved at least once before you can test an alert rule", nil) | ||
} | ||
|
||
res, err := hs.AlertEngine.AlertTest(c.SignedInUser.GetOrgID(), dto.Dashboard, dto.PanelId, c.SignedInUser) | ||
// LOGZ.IO GRAFANA CHANGE :: DEV-17927 - add LogzIoHeaders | ||
res, err := hs.AlertEngine.AlertTest(c.SignedInUser.GetOrgID(), dto.Dashboard, dto.PanelId, c.SignedInUser, &models.LogzIoHeaders{RequestHeaders: c.Req.Header}) | ||
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. why are 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. it is technically part of the logzio datasources support, but because a lot of the implementation for the logz evaluation includes this i put it in this PR. i don't know if at that point it's worth the time of taking out of this PR for the next , because the next PR anyway will be based on that and making these changes can add up a day of work i don't see any value for it 😅 |
||
if err != nil { | ||
var validationErr alerting.ValidationError | ||
if errors.As(err, &validationErr) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// LOGZ.IO GRAFANA CHANGE :: DEV-17927 use LogzIoHeaders obj to pass on headers | ||
package models | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
const LogzioHeadersCtxKey string = "logzioHeaders" | ||
const LogzioRequestIdHeaderName string = "x-request-id" | ||
|
||
type LogzIoHeaders struct { | ||
RequestHeaders http.Header | ||
} | ||
|
||
var logzioHeadersWhitelist = []string{ | ||
"x-auth-token", | ||
"x-api-token", | ||
"user-context", | ||
LogzioRequestIdHeaderName, | ||
"cookie", | ||
"x-logz-csrf-token", | ||
"x-logz-csrf-token-v2", | ||
} | ||
|
||
func (logzioHeaders *LogzIoHeaders) GetDatasourceQueryHeaders(grafanaGeneratedHeaders http.Header) http.Header { | ||
datasourceRequestHeaders := grafanaGeneratedHeaders.Clone() | ||
logzioGrafanaRequestHeaders := logzioHeaders.RequestHeaders | ||
|
||
for _, whitelistedHeader := range logzioHeadersWhitelist { | ||
if requestHeader := logzioGrafanaRequestHeaders.Get(whitelistedHeader); requestHeader != "" { | ||
datasourceRequestHeaders.Set(whitelistedHeader, requestHeader) | ||
} | ||
} | ||
|
||
return datasourceRequestHeaders | ||
} | ||
|
||
// LOGZ.IO GRAFANA CHANGE :: end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package api | ||
|
||
// LOGZ.IO GRAFANA CHANGE :: DEV-30169,DEV-30170: add endpoints to evaluate and process alerts | ||
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 | ||
} | ||
|
||
type RunAlertMigrationForOrg struct { | ||
OrgId int64 `json:"orgId"` | ||
EmailNotifications []AlertMigrationEmailNotification `json:"emailNotifications"` | ||
} | ||
|
||
type ClearOrgAlertMigration struct { | ||
OrgId int64 `json:"orgId"` | ||
} | ||
|
||
type AlertMigrationEmailNotification struct { | ||
EmailAddress string `json:"address"` | ||
ChannelUid string `json:"channelUid"` | ||
} | ||
|
||
// NewLogzioAlertingApi creates a new LogzioAlertingApi instance | ||
func NewLogzioAlertingApi(service *LogzioAlertingService) *LogzioAlertingApi { | ||
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. Didn't we say its redundant? (leave only the service) 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. this is the structure of all APIs in this project. there is a class of "XXXApi" that has the route to the endpoint, and the function to call the service class that contains the main logic. |
||
return &LogzioAlertingApi{ | ||
service: service, | ||
} | ||
} | ||
|
||
func (api *LogzioAlertingApi) RouteEvaluateAlert(ctx *contextmodel.ReqContext) response.Response { | ||
body := apimodels.AlertEvaluationRequest{} | ||
if err := web.Bind(ctx.Req, &body); err != nil { | ||
return response.Error(http.StatusBadRequest, "bad request data", err) | ||
} | ||
|
||
return api.service.RouteEvaluateAlert(ctx, body) | ||
} | ||
|
||
//func (api *LogzioAlertingApi) RouteProcessAlert(ctx *contextmodel.ReqContext) response.Response { | ||
// body := apimodels.AlertProcessRequest{} | ||
// if err := web.Bind(ctx.Req, &body); err != nil { | ||
// return response.Error(http.StatusBadRequest, "bad request data", err) | ||
// } | ||
// | ||
// return api.service.RouteProcessAlert(*ctx.Req, body) | ||
//} | ||
|
||
func (api *API) RegisterLogzioAlertingApiEndpoints(srv *LogzioAlertingApi, m *metrics.API) { | ||
api.RouteRegister.Group("", func(group routing.RouteRegister) { | ||
group.Post( | ||
toMacaronPath("/internal/alert/api/v1/eval"), | ||
metrics.Instrument( | ||
http.MethodPost, | ||
"/internal/alert/api/v1/eval", | ||
srv.RouteEvaluateAlert, | ||
m, | ||
), | ||
) | ||
//group.Post( | ||
// toMacaronPath("/internal/alert/api/v1/process"), | ||
// metrics.Instrument( | ||
// http.MethodPost, | ||
// "/internal/alert/api/v1/process", | ||
// srv.RouteProcessAlert, | ||
// m, | ||
// ), | ||
//) | ||
}) | ||
} | ||
|
||
// LOGZ.IO GRAFANA CHANGE :: end |
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.
In the comment, I think better to use the reference to the new tickets. Also note in my PR I used a slightly different comment, so better be consistent (you can change mine if you wish).
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.
done