2021-03-08 14:19:21 -06:00
|
|
|
package api
|
2020-11-12 07:11:30 -06:00
|
|
|
|
|
|
|
import (
|
2021-03-08 14:19:21 -06:00
|
|
|
"time"
|
|
|
|
|
2021-04-05 17:05:39 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
|
|
|
|
2020-11-12 07:11:30 -06:00
|
|
|
"github.com/go-macaron/binding"
|
2021-03-24 09:20:44 -05:00
|
|
|
|
|
|
|
apimodels "github.com/grafana/alerting-api/pkg/api"
|
2020-11-12 07:11:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2021-03-24 09:20:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2020-11-12 07:11:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/middleware"
|
2021-03-24 09:20:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasourceproxy"
|
2021-03-03 09:52:19 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
2021-03-24 09:20:44 -05:00
|
|
|
ngmodels "github.com/grafana/grafana/pkg/services/ngalert/models"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/schedule"
|
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
2021-03-03 09:52:19 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-11-12 07:11:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb"
|
|
|
|
)
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// timeNow makes it possible to test usage of time
|
|
|
|
var timeNow = time.Now
|
|
|
|
|
2021-03-24 09:20:44 -05:00
|
|
|
type Alertmanager interface {
|
2021-04-08 06:27:59 -05:00
|
|
|
// Configuration
|
2021-03-24 09:20:44 -05:00
|
|
|
ApplyConfig(config *apimodels.PostableUserConfig) error
|
2021-04-08 06:27:59 -05:00
|
|
|
|
|
|
|
// Silences
|
2021-03-31 15:00:56 -05:00
|
|
|
CreateSilence(ps *apimodels.PostableSilence) (string, error)
|
|
|
|
DeleteSilence(silenceID string) error
|
|
|
|
GetSilence(silenceID string) (apimodels.GettableSilence, error)
|
2021-04-08 06:27:59 -05:00
|
|
|
ListSilences(filter []string) (apimodels.GettableSilences, error)
|
|
|
|
|
|
|
|
// Alerts
|
|
|
|
GetAlerts(active, silenced, inhibited bool, filter []string, receiver string) (apimodels.GettableAlerts, error)
|
|
|
|
GetAlertGroups(active, silenced, inhibited bool, filter []string, receiver string) (apimodels.AlertGroups, error)
|
2021-03-24 09:20:44 -05:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// API handlers.
|
|
|
|
type API struct {
|
|
|
|
Cfg *setting.Cfg
|
|
|
|
DatasourceCache datasources.CacheService
|
|
|
|
RouteRegister routing.RouteRegister
|
2021-03-08 00:02:49 -06:00
|
|
|
DataService *tsdb.Service
|
2021-03-08 14:19:21 -06:00
|
|
|
Schedule schedule.ScheduleService
|
|
|
|
Store store.Store
|
2021-04-01 03:11:45 -05:00
|
|
|
RuleStore store.RuleStore
|
2021-03-31 15:00:56 -05:00
|
|
|
AlertingStore store.AlertingStore
|
2021-03-19 09:32:13 -05:00
|
|
|
DataProxy *datasourceproxy.DatasourceProxyService
|
2021-03-24 09:20:44 -05:00
|
|
|
Alertmanager Alertmanager
|
2021-04-05 17:05:39 -05:00
|
|
|
StateTracker *state.StateTracker
|
2021-03-03 09:52:19 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// RegisterAPIEndpoints registers API handlers
|
|
|
|
func (api *API) RegisterAPIEndpoints() {
|
2021-03-11 13:28:00 -06:00
|
|
|
logger := log.New("ngalert.api")
|
2021-03-24 06:43:25 -05:00
|
|
|
proxy := &AlertingProxy{
|
|
|
|
DataProxy: api.DataProxy,
|
|
|
|
}
|
2021-04-09 04:55:41 -05:00
|
|
|
// Register endpoints for proxing to Alertmanager-compatible backends.
|
2021-03-29 10:18:25 -05:00
|
|
|
api.RegisterAlertmanagerApiEndpoints(NewForkedAM(
|
|
|
|
api.DatasourceCache,
|
|
|
|
NewLotexAM(proxy, logger),
|
2021-03-31 15:00:56 -05:00
|
|
|
AlertmanagerSrv{store: api.AlertingStore, am: api.Alertmanager, log: logger},
|
2021-03-29 10:18:25 -05:00
|
|
|
))
|
2021-04-09 04:55:41 -05:00
|
|
|
// Register endpoints for proxing to Prometheus-compatible backends.
|
2021-03-24 06:43:25 -05:00
|
|
|
api.RegisterPrometheusApiEndpoints(NewForkedProm(
|
|
|
|
api.DatasourceCache,
|
|
|
|
NewLotexProm(proxy, logger),
|
2021-04-05 17:05:39 -05:00
|
|
|
PrometheusSrv{log: logger, stateTracker: api.StateTracker},
|
2021-03-24 06:43:25 -05:00
|
|
|
))
|
2021-04-09 04:55:41 -05:00
|
|
|
// Register endpoints for proxing to Cortex Ruler-compatible backends.
|
2021-03-19 09:32:13 -05:00
|
|
|
api.RegisterRulerApiEndpoints(NewForkedRuler(
|
2021-03-23 11:08:57 -05:00
|
|
|
api.DatasourceCache,
|
2021-03-24 06:43:25 -05:00
|
|
|
NewLotexRuler(proxy, logger),
|
2021-04-01 03:11:45 -05:00
|
|
|
RulerSrv{store: api.RuleStore, log: logger},
|
2021-03-19 09:32:13 -05:00
|
|
|
))
|
2021-04-09 04:55:41 -05:00
|
|
|
// Register endpoints for testing evaluation of rules and notification channels.
|
2021-03-17 05:47:03 -05:00
|
|
|
api.RegisterTestingApiEndpoints(TestingApiMock{log: logger})
|
2021-03-11 13:28:00 -06:00
|
|
|
|
|
|
|
// Legacy routes; they will be removed in v8
|
2021-03-03 09:52:19 -06:00
|
|
|
api.RouteRegister.Group("/api/alert-definitions", func(alertDefinitions routing.RouteRegister) {
|
|
|
|
alertDefinitions.Get("", middleware.ReqSignedIn, routing.Wrap(api.listAlertDefinitions))
|
|
|
|
alertDefinitions.Get("/eval/:alertDefinitionUID", middleware.ReqSignedIn, api.validateOrgAlertDefinition, routing.Wrap(api.alertDefinitionEvalEndpoint))
|
2021-03-08 14:19:21 -06:00
|
|
|
alertDefinitions.Post("/eval", middleware.ReqSignedIn, binding.Bind(ngmodels.EvalAlertConditionCommand{}), routing.Wrap(api.conditionEvalEndpoint))
|
2021-03-03 09:52:19 -06:00
|
|
|
alertDefinitions.Get("/:alertDefinitionUID", middleware.ReqSignedIn, api.validateOrgAlertDefinition, routing.Wrap(api.getAlertDefinitionEndpoint))
|
|
|
|
alertDefinitions.Delete("/:alertDefinitionUID", middleware.ReqEditorRole, api.validateOrgAlertDefinition, routing.Wrap(api.deleteAlertDefinitionEndpoint))
|
2021-03-08 14:19:21 -06:00
|
|
|
alertDefinitions.Post("/", middleware.ReqEditorRole, binding.Bind(ngmodels.SaveAlertDefinitionCommand{}), routing.Wrap(api.createAlertDefinitionEndpoint))
|
|
|
|
alertDefinitions.Put("/:alertDefinitionUID", middleware.ReqEditorRole, api.validateOrgAlertDefinition, binding.Bind(ngmodels.UpdateAlertDefinitionCommand{}), routing.Wrap(api.updateAlertDefinitionEndpoint))
|
|
|
|
alertDefinitions.Post("/pause", middleware.ReqEditorRole, binding.Bind(ngmodels.UpdateAlertDefinitionPausedCommand{}), routing.Wrap(api.alertDefinitionPauseEndpoint))
|
|
|
|
alertDefinitions.Post("/unpause", middleware.ReqEditorRole, binding.Bind(ngmodels.UpdateAlertDefinitionPausedCommand{}), routing.Wrap(api.alertDefinitionUnpauseEndpoint))
|
2020-11-12 07:11:30 -06:00
|
|
|
})
|
|
|
|
|
2021-03-23 11:11:15 -05:00
|
|
|
if api.Cfg.Env == setting.Dev {
|
|
|
|
api.RouteRegister.Group("/api/alert-definitions", func(alertDefinitions routing.RouteRegister) {
|
|
|
|
alertDefinitions.Post("/evalOld", middleware.ReqSignedIn, routing.Wrap(api.conditionEvalOldEndpoint))
|
|
|
|
})
|
|
|
|
api.RouteRegister.Group("/api/alert-definitions", func(alertDefinitions routing.RouteRegister) {
|
|
|
|
alertDefinitions.Get("/evalOldByID/:id", middleware.ReqSignedIn, routing.Wrap(api.conditionEvalOldEndpointByID))
|
|
|
|
})
|
2021-03-24 11:12:34 -05:00
|
|
|
api.RouteRegister.Group("/api/alert-definitions", func(alertDefinitions routing.RouteRegister) {
|
|
|
|
alertDefinitions.Get("/oldByID/:id", middleware.ReqSignedIn, routing.Wrap(api.conditionOldEndpointByID))
|
|
|
|
})
|
2021-04-07 07:28:06 -05:00
|
|
|
api.RouteRegister.Group("/api/alert-definitions", func(alertDefinitions routing.RouteRegister) {
|
|
|
|
alertDefinitions.Get("/ruleGroupByOldID/:id", middleware.ReqSignedIn, routing.Wrap(api.ruleGroupByOldID))
|
|
|
|
})
|
2021-03-23 11:11:15 -05:00
|
|
|
}
|
|
|
|
|
2021-03-24 11:12:34 -05:00
|
|
|
api.RouteRegister.Group("/api/ngalert/", func(schedulerRouter routing.RouteRegister) {
|
|
|
|
schedulerRouter.Post("/pause", routing.Wrap(api.pauseScheduler))
|
|
|
|
schedulerRouter.Post("/unpause", routing.Wrap(api.unpauseScheduler))
|
|
|
|
}, middleware.ReqOrgAdmin)
|
2021-03-23 11:11:15 -05:00
|
|
|
|
2021-03-24 11:12:34 -05:00
|
|
|
api.RouteRegister.Group("/api/alert-instances", func(alertInstances routing.RouteRegister) {
|
|
|
|
alertInstances.Get("", middleware.ReqSignedIn, routing.Wrap(api.listAlertInstancesEndpoint))
|
2021-03-23 11:11:15 -05:00
|
|
|
})
|
2020-12-17 08:00:09 -06:00
|
|
|
}
|