Alerting: refactor scheduler and separate notification logic (#48144)
* Introduce AlertsRouter in the sender package, and move all fields and methods related to notifications out of the scheduler to this router. * Introduce a new interface AlertsSender in the schedule package and replace calls of anonymous function `notify` inside the ruleRoutine to calling methods of that interface. * Rename interface Scheduler in api package to ExternalAlertmanagerProvider, and replace scheduler with AlertRouter as struct that implements the interface.
This commit is contained in:
@@ -18,6 +18,7 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/notifier"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/schedule"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/sender"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
||||
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
||||
"github.com/grafana/grafana/pkg/services/quota"
|
||||
@@ -28,7 +29,7 @@ import (
|
||||
// timeNow makes it possible to test usage of time
|
||||
var timeNow = time.Now
|
||||
|
||||
type Scheduler interface {
|
||||
type ExternalAlertmanagerProvider interface {
|
||||
AlertmanagersFor(orgID int64) []*url.URL
|
||||
DroppedAlertmanagersFor(orgID int64) []*url.URL
|
||||
}
|
||||
@@ -81,6 +82,7 @@ type API struct {
|
||||
Templates *provisioning.TemplateService
|
||||
MuteTimings *provisioning.MuteTimingService
|
||||
AlertRules *provisioning.AlertRuleService
|
||||
AlertsRouter *sender.AlertsRouter
|
||||
}
|
||||
|
||||
// RegisterAPIEndpoints registers API handlers
|
||||
@@ -128,9 +130,9 @@ func (api *API) RegisterAPIEndpoints(m *metrics.API) {
|
||||
}), m)
|
||||
api.RegisterConfigurationApiEndpoints(NewForkedConfiguration(
|
||||
&AdminSrv{
|
||||
store: api.AdminConfigStore,
|
||||
log: logger,
|
||||
scheduler: api.Schedule,
|
||||
store: api.AdminConfigStore,
|
||||
log: logger,
|
||||
alertmanagerProvider: api.AlertsRouter,
|
||||
},
|
||||
), m)
|
||||
|
||||
|
||||
@@ -16,14 +16,14 @@ import (
|
||||
)
|
||||
|
||||
type AdminSrv struct {
|
||||
scheduler Scheduler
|
||||
store store.AdminConfigurationStore
|
||||
log log.Logger
|
||||
alertmanagerProvider ExternalAlertmanagerProvider
|
||||
store store.AdminConfigurationStore
|
||||
log log.Logger
|
||||
}
|
||||
|
||||
func (srv AdminSrv) RouteGetAlertmanagers(c *models.ReqContext) response.Response {
|
||||
urls := srv.scheduler.AlertmanagersFor(c.OrgId)
|
||||
droppedURLs := srv.scheduler.DroppedAlertmanagersFor(c.OrgId)
|
||||
urls := srv.alertmanagerProvider.AlertmanagersFor(c.OrgId)
|
||||
droppedURLs := srv.alertmanagerProvider.DroppedAlertmanagersFor(c.OrgId)
|
||||
ams := v1.AlertManagersResult{Active: make([]v1.AlertManager, len(urls)), Dropped: make([]v1.AlertManager, len(droppedURLs))}
|
||||
for i, url := range urls {
|
||||
ams.Active[i].URL = url.String()
|
||||
|
||||
Reference in New Issue
Block a user