2021-03-08 14:19:21 -06:00
|
|
|
package api
|
2020-11-12 07:11:30 -06:00
|
|
|
|
|
|
|
import (
|
2021-08-17 07:49:05 -05:00
|
|
|
"context"
|
2021-08-13 07:14:36 -05:00
|
|
|
"net/url"
|
2021-03-08 14:19:21 -06:00
|
|
|
"time"
|
|
|
|
|
2020-11-12 07:11:30 -06:00
|
|
|
"github.com/grafana/grafana/pkg/api/routing"
|
2021-11-10 04:52:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/expr"
|
2021-03-24 09:20:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-03-08 08:22:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/accesscontrol"
|
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-04-19 13:26:04 -05:00
|
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
2022-04-01 19:00:23 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/eval"
|
2021-08-06 07:06:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/metrics"
|
2021-12-27 17:01:17 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/models"
|
2021-08-17 07:49:05 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/notifier"
|
2022-04-05 16:48:51 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/provisioning"
|
2021-03-24 09:20:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/schedule"
|
2022-07-12 14:13:04 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/sender"
|
2021-08-06 07:06:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/state"
|
2021-03-24 09:20:44 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/ngalert/store"
|
2021-08-06 07:06:56 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/quota"
|
2021-11-04 11:47:21 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/secrets"
|
2021-03-03 09:52:19 -06:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2020-11-12 07:11:30 -06:00
|
|
|
)
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// timeNow makes it possible to test usage of time
|
|
|
|
var timeNow = time.Now
|
|
|
|
|
2022-07-12 14:13:04 -05:00
|
|
|
type ExternalAlertmanagerProvider interface {
|
2021-08-13 07:14:36 -05:00
|
|
|
AlertmanagersFor(orgID int64) []*url.URL
|
|
|
|
DroppedAlertmanagersFor(orgID int64) []*url.URL
|
|
|
|
}
|
|
|
|
|
2021-03-24 09:20:44 -05:00
|
|
|
type Alertmanager interface {
|
2021-04-08 06:27:59 -05:00
|
|
|
// Configuration
|
2022-02-09 03:22:09 -06:00
|
|
|
SaveAndApplyConfig(ctx context.Context, config *apimodels.PostableUserConfig) error
|
|
|
|
SaveAndApplyDefaultConfig(ctx context.Context) error
|
2021-06-15 11:14:02 -05:00
|
|
|
GetStatus() apimodels.GettableStatus
|
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-08-17 07:49:05 -05:00
|
|
|
|
|
|
|
// Testing
|
2021-11-30 13:55:54 -06:00
|
|
|
TestReceivers(ctx context.Context, c apimodels.TestReceiversConfigBodyParams) (*notifier.TestReceiversResult, error)
|
2021-03-24 09:20:44 -05:00
|
|
|
}
|
|
|
|
|
2021-12-27 17:01:17 -06:00
|
|
|
type AlertingStore interface {
|
2022-02-09 03:22:09 -06:00
|
|
|
GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error
|
2021-12-27 17:01:17 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// API handlers.
|
|
|
|
type API struct {
|
2021-08-24 05:28:09 -05:00
|
|
|
Cfg *setting.Cfg
|
|
|
|
DatasourceCache datasources.CacheService
|
2022-07-20 09:50:49 -05:00
|
|
|
DatasourceService datasources.DataSourceService
|
2021-08-24 05:28:09 -05:00
|
|
|
RouteRegister routing.RouteRegister
|
2021-11-10 04:52:16 -06:00
|
|
|
ExpressionService *expr.Service
|
2022-07-15 11:06:44 -05:00
|
|
|
QuotaService quota.Service
|
2021-08-24 05:28:09 -05:00
|
|
|
Schedule schedule.ScheduleService
|
2022-04-05 16:48:51 -05:00
|
|
|
TransactionManager provisioning.TransactionManager
|
2022-04-28 14:27:34 -05:00
|
|
|
ProvenanceStore provisioning.ProvisioningStore
|
2021-08-24 05:28:09 -05:00
|
|
|
RuleStore store.RuleStore
|
|
|
|
InstanceStore store.InstanceStore
|
2021-12-27 17:01:17 -06:00
|
|
|
AlertingStore AlertingStore
|
2021-08-24 05:28:09 -05:00
|
|
|
AdminConfigStore store.AdminConfigurationStore
|
2021-08-25 08:11:22 -05:00
|
|
|
DataProxy *datasourceproxy.DataSourceProxyService
|
2021-08-24 05:28:09 -05:00
|
|
|
MultiOrgAlertmanager *notifier.MultiOrgAlertmanager
|
|
|
|
StateManager *state.Manager
|
2021-11-04 11:47:21 -05:00
|
|
|
SecretsService secrets.Service
|
2022-03-08 08:22:16 -06:00
|
|
|
AccessControl accesscontrol.AccessControl
|
2022-04-05 16:48:51 -05:00
|
|
|
Policies *provisioning.NotificationPolicyService
|
2022-04-13 15:15:55 -05:00
|
|
|
ContactPointService *provisioning.ContactPointService
|
2022-04-28 13:51:57 -05:00
|
|
|
Templates *provisioning.TemplateService
|
2022-05-17 13:42:48 -05:00
|
|
|
MuteTimings *provisioning.MuteTimingService
|
2022-06-02 07:48:53 -05:00
|
|
|
AlertRules *provisioning.AlertRuleService
|
2022-07-12 14:13:04 -05:00
|
|
|
AlertsRouter *sender.AlertsRouter
|
2021-03-03 09:52:19 -06:00
|
|
|
}
|
|
|
|
|
2021-03-08 14:19:21 -06:00
|
|
|
// RegisterAPIEndpoints registers API handlers
|
2021-09-14 06:55:01 -05:00
|
|
|
func (api *API) RegisterAPIEndpoints(m *metrics.API) {
|
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-27 07:25:32 -05:00
|
|
|
|
2021-08-13 07:14:36 -05:00
|
|
|
// Register endpoints for proxying to Alertmanager-compatible backends.
|
2022-07-18 02:08:08 -05:00
|
|
|
api.RegisterAlertmanagerApiEndpoints(NewForkingAM(
|
2021-03-29 10:18:25 -05:00
|
|
|
api.DatasourceCache,
|
|
|
|
NewLotexAM(proxy, logger),
|
2022-04-14 13:06:21 -05:00
|
|
|
&AlertmanagerSrv{crypto: api.MultiOrgAlertmanager.Crypto, log: logger, ac: api.AccessControl, mam: api.MultiOrgAlertmanager},
|
2021-04-30 11:28:06 -05:00
|
|
|
), m)
|
2021-08-13 07:14:36 -05:00
|
|
|
// Register endpoints for proxying to Prometheus-compatible backends.
|
2022-07-18 02:08:08 -05:00
|
|
|
api.RegisterPrometheusApiEndpoints(NewForkingProm(
|
2021-03-24 06:43:25 -05:00
|
|
|
api.DatasourceCache,
|
|
|
|
NewLotexProm(proxy, logger),
|
2022-04-11 16:37:44 -05:00
|
|
|
&PrometheusSrv{log: logger, manager: api.StateManager, store: api.RuleStore, ac: api.AccessControl},
|
2021-04-30 11:28:06 -05:00
|
|
|
), m)
|
2021-08-13 07:14:36 -05:00
|
|
|
// Register endpoints for proxying to Cortex Ruler-compatible backends.
|
2022-07-18 02:08:08 -05:00
|
|
|
api.RegisterRulerApiEndpoints(NewForkingRuler(
|
2021-03-23 11:08:57 -05:00
|
|
|
api.DatasourceCache,
|
2021-03-24 06:43:25 -05:00
|
|
|
NewLotexRuler(proxy, logger),
|
2022-03-15 11:48:42 -05:00
|
|
|
&RulerSrv{
|
|
|
|
DatasourceCache: api.DatasourceCache,
|
|
|
|
QuotaService: api.QuotaService,
|
|
|
|
scheduleService: api.Schedule,
|
|
|
|
store: api.RuleStore,
|
2022-04-28 14:27:34 -05:00
|
|
|
provenanceStore: api.ProvenanceStore,
|
2022-03-15 11:48:42 -05:00
|
|
|
xactManager: api.TransactionManager,
|
2022-03-21 18:20:35 -05:00
|
|
|
log: logger,
|
|
|
|
cfg: &api.Cfg.UnifiedAlerting,
|
|
|
|
ac: api.AccessControl,
|
|
|
|
},
|
2021-04-30 11:28:06 -05:00
|
|
|
), m)
|
2022-07-18 02:08:08 -05:00
|
|
|
api.RegisterTestingApiEndpoints(NewTestingApi(
|
2022-02-04 11:42:04 -06:00
|
|
|
&TestingApiSrv{
|
2022-06-27 16:40:44 -05:00
|
|
|
AlertingProxy: proxy,
|
|
|
|
DatasourceCache: api.DatasourceCache,
|
|
|
|
log: logger,
|
|
|
|
accessControl: api.AccessControl,
|
|
|
|
evaluator: eval.NewEvaluator(api.Cfg, log.New("ngalert.eval"), api.DatasourceCache, api.SecretsService, api.ExpressionService),
|
2021-12-13 02:22:57 -06:00
|
|
|
}), m)
|
2022-07-18 02:08:08 -05:00
|
|
|
api.RegisterConfigurationApiEndpoints(NewConfiguration(
|
|
|
|
&ConfigSrv{
|
2022-07-20 09:50:49 -05:00
|
|
|
datasourceService: api.DatasourceService,
|
2022-07-12 14:13:04 -05:00
|
|
|
store: api.AdminConfigStore,
|
|
|
|
log: logger,
|
|
|
|
alertmanagerProvider: api.AlertsRouter,
|
2021-12-13 02:22:57 -06:00
|
|
|
},
|
|
|
|
), m)
|
2022-04-05 16:48:51 -05:00
|
|
|
|
2022-07-18 02:08:08 -05:00
|
|
|
api.RegisterProvisioningApiEndpoints(NewProvisioningApi(&ProvisioningSrv{
|
2022-06-05 00:45:36 -05:00
|
|
|
log: logger,
|
|
|
|
policies: api.Policies,
|
|
|
|
contactPointService: api.ContactPointService,
|
|
|
|
templates: api.Templates,
|
|
|
|
muteTimings: api.MuteTimings,
|
|
|
|
alertRules: api.AlertRules,
|
|
|
|
}), m)
|
2020-12-17 08:00:09 -06:00
|
|
|
}
|