| 
									
										
										
										
											2021-03-09 04:19:21 +08:00
										 |  |  | package api | 
					
						
							| 
									
										
										
										
											2020-11-12 21:11:30 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-08-17 20:49:05 +08:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | 	"net/url" | 
					
						
							| 
									
										
										
										
											2021-03-09 04:19:21 +08:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-12 21:11:30 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/api/routing" | 
					
						
							| 
									
										
										
										
											2021-11-10 18:52:16 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/expr" | 
					
						
							| 
									
										
										
										
											2021-03-24 22:20:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/infra/log" | 
					
						
							| 
									
										
										
										
											2022-03-08 22:22:16 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/accesscontrol" | 
					
						
							| 
									
										
										
										
											2021-03-24 22:20:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/datasourceproxy" | 
					
						
							| 
									
										
										
										
											2021-03-03 23:52:19 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/datasources" | 
					
						
							| 
									
										
										
										
											2021-04-20 02:26:04 +08:00
										 |  |  | 	apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions" | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert/metrics" | 
					
						
							| 
									
										
										
										
											2021-12-28 07:01:17 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert/models" | 
					
						
							| 
									
										
										
										
											2021-08-17 20:49:05 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert/notifier" | 
					
						
							| 
									
										
										
										
											2021-03-24 22:20:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert/schedule" | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert/state" | 
					
						
							| 
									
										
										
										
											2021-03-24 22:20:44 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/ngalert/store" | 
					
						
							| 
									
										
										
										
											2021-08-06 20:06:56 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/quota" | 
					
						
							| 
									
										
										
										
											2021-11-05 00:47:21 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/services/secrets" | 
					
						
							| 
									
										
										
										
											2021-03-03 23:52:19 +08:00
										 |  |  | 	"github.com/grafana/grafana/pkg/setting" | 
					
						
							| 
									
										
										
										
											2020-11-12 21:11:30 +08:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 04:19:21 +08:00
										 |  |  | // timeNow makes it possible to test usage of time
 | 
					
						
							|  |  |  | var timeNow = time.Now | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | type Scheduler interface { | 
					
						
							|  |  |  | 	AlertmanagersFor(orgID int64) []*url.URL | 
					
						
							|  |  |  | 	DroppedAlertmanagersFor(orgID int64) []*url.URL | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-24 22:20:44 +08:00
										 |  |  | type Alertmanager interface { | 
					
						
							| 
									
										
										
										
											2021-04-08 19:27:59 +08:00
										 |  |  | 	// Configuration
 | 
					
						
							| 
									
										
										
										
											2022-02-09 17:22:09 +08:00
										 |  |  | 	SaveAndApplyConfig(ctx context.Context, config *apimodels.PostableUserConfig) error | 
					
						
							|  |  |  | 	SaveAndApplyDefaultConfig(ctx context.Context) error | 
					
						
							| 
									
										
										
										
											2021-06-16 00:14:02 +08:00
										 |  |  | 	GetStatus() apimodels.GettableStatus | 
					
						
							| 
									
										
										
										
											2021-04-08 19:27:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Silences
 | 
					
						
							| 
									
										
										
										
											2021-04-01 04:00:56 +08:00
										 |  |  | 	CreateSilence(ps *apimodels.PostableSilence) (string, error) | 
					
						
							|  |  |  | 	DeleteSilence(silenceID string) error | 
					
						
							|  |  |  | 	GetSilence(silenceID string) (apimodels.GettableSilence, error) | 
					
						
							| 
									
										
										
										
											2021-04-08 19:27:59 +08: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 20:49:05 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// Testing
 | 
					
						
							| 
									
										
										
										
											2021-12-01 03:55:54 +08:00
										 |  |  | 	TestReceivers(ctx context.Context, c apimodels.TestReceiversConfigBodyParams) (*notifier.TestReceiversResult, error) | 
					
						
							| 
									
										
										
										
											2021-03-24 22:20:44 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-28 07:01:17 +08:00
										 |  |  | type AlertingStore interface { | 
					
						
							| 
									
										
										
										
											2022-02-09 17:22:09 +08:00
										 |  |  | 	GetLatestAlertmanagerConfiguration(ctx context.Context, query *models.GetLatestAlertmanagerConfigurationQuery) error | 
					
						
							| 
									
										
										
										
											2021-12-28 07:01:17 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 04:19:21 +08:00
										 |  |  | // API handlers.
 | 
					
						
							|  |  |  | type API struct { | 
					
						
							| 
									
										
										
										
											2021-08-24 18:28:09 +08:00
										 |  |  | 	Cfg                  *setting.Cfg | 
					
						
							|  |  |  | 	DatasourceCache      datasources.CacheService | 
					
						
							|  |  |  | 	RouteRegister        routing.RouteRegister | 
					
						
							| 
									
										
										
										
											2021-11-10 18:52:16 +08:00
										 |  |  | 	ExpressionService    *expr.Service | 
					
						
							| 
									
										
										
										
											2021-08-24 18:28:09 +08:00
										 |  |  | 	QuotaService         *quota.QuotaService | 
					
						
							|  |  |  | 	Schedule             schedule.ScheduleService | 
					
						
							| 
									
										
										
										
											2022-03-16 00:48:42 +08:00
										 |  |  | 	TransactionManager   store.TransactionManager | 
					
						
							| 
									
										
										
										
											2021-08-24 18:28:09 +08:00
										 |  |  | 	RuleStore            store.RuleStore | 
					
						
							|  |  |  | 	InstanceStore        store.InstanceStore | 
					
						
							| 
									
										
										
										
											2021-12-28 07:01:17 +08:00
										 |  |  | 	AlertingStore        AlertingStore | 
					
						
							| 
									
										
										
										
											2021-08-24 18:28:09 +08:00
										 |  |  | 	AdminConfigStore     store.AdminConfigurationStore | 
					
						
							| 
									
										
										
										
											2021-08-25 21:11:22 +08:00
										 |  |  | 	DataProxy            *datasourceproxy.DataSourceProxyService | 
					
						
							| 
									
										
										
										
											2021-08-24 18:28:09 +08:00
										 |  |  | 	MultiOrgAlertmanager *notifier.MultiOrgAlertmanager | 
					
						
							|  |  |  | 	StateManager         *state.Manager | 
					
						
							| 
									
										
										
										
											2021-11-05 00:47:21 +08:00
										 |  |  | 	SecretsService       secrets.Service | 
					
						
							| 
									
										
										
										
											2022-03-08 22:22:16 +08:00
										 |  |  | 	AccessControl        accesscontrol.AccessControl | 
					
						
							| 
									
										
										
										
											2021-03-03 23:52:19 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-09 04:19:21 +08:00
										 |  |  | // RegisterAPIEndpoints registers API handlers
 | 
					
						
							| 
									
										
										
										
											2021-09-14 19:55:01 +08:00
										 |  |  | func (api *API) RegisterAPIEndpoints(m *metrics.API) { | 
					
						
							| 
									
										
										
										
											2021-03-12 03:28:00 +08:00
										 |  |  | 	logger := log.New("ngalert.api") | 
					
						
							| 
									
										
										
										
											2021-03-24 19:43:25 +08:00
										 |  |  | 	proxy := &AlertingProxy{ | 
					
						
							|  |  |  | 		DataProxy: api.DataProxy, | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-04-27 20:25:32 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | 	// Register endpoints for proxying to Alertmanager-compatible backends.
 | 
					
						
							| 
									
										
										
										
											2021-03-29 23:18:25 +08:00
										 |  |  | 	api.RegisterAlertmanagerApiEndpoints(NewForkedAM( | 
					
						
							|  |  |  | 		api.DatasourceCache, | 
					
						
							|  |  |  | 		NewLotexAM(proxy, logger), | 
					
						
							| 
									
										
										
										
											2022-02-05 01:42:04 +08:00
										 |  |  | 		&AlertmanagerSrv{store: api.AlertingStore, mam: api.MultiOrgAlertmanager, secrets: api.SecretsService, log: logger}, | 
					
						
							| 
									
										
										
										
											2021-05-01 00:28:06 +08:00
										 |  |  | 	), m) | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | 	// Register endpoints for proxying to Prometheus-compatible backends.
 | 
					
						
							| 
									
										
										
										
											2021-03-24 19:43:25 +08:00
										 |  |  | 	api.RegisterPrometheusApiEndpoints(NewForkedProm( | 
					
						
							|  |  |  | 		api.DatasourceCache, | 
					
						
							|  |  |  | 		NewLotexProm(proxy, logger), | 
					
						
							| 
									
										
										
										
											2022-02-05 01:42:04 +08:00
										 |  |  | 		&PrometheusSrv{log: logger, manager: api.StateManager, store: api.RuleStore}, | 
					
						
							| 
									
										
										
										
											2021-05-01 00:28:06 +08:00
										 |  |  | 	), m) | 
					
						
							| 
									
										
										
										
											2021-08-13 20:14:36 +08:00
										 |  |  | 	// Register endpoints for proxying to Cortex Ruler-compatible backends.
 | 
					
						
							| 
									
										
										
										
											2021-03-19 22:32:13 +08:00
										 |  |  | 	api.RegisterRulerApiEndpoints(NewForkedRuler( | 
					
						
							| 
									
										
										
										
											2021-03-24 00:08:57 +08:00
										 |  |  | 		api.DatasourceCache, | 
					
						
							| 
									
										
										
										
											2021-03-24 19:43:25 +08:00
										 |  |  | 		NewLotexRuler(proxy, logger), | 
					
						
							| 
									
										
										
										
											2022-03-16 00:48:42 +08:00
										 |  |  | 		&RulerSrv{ | 
					
						
							|  |  |  | 			DatasourceCache: api.DatasourceCache, | 
					
						
							|  |  |  | 			QuotaService:    api.QuotaService, | 
					
						
							|  |  |  | 			scheduleService: api.Schedule, | 
					
						
							|  |  |  | 			store:           api.RuleStore, | 
					
						
							|  |  |  | 			xactManager:     api.TransactionManager, | 
					
						
							|  |  |  | 			log:             logger, cfg: &api.Cfg.UnifiedAlerting}, | 
					
						
							| 
									
										
										
										
											2021-05-01 00:28:06 +08:00
										 |  |  | 	), m) | 
					
						
							| 
									
										
										
										
											2021-12-13 16:22:57 +08:00
										 |  |  | 	api.RegisterTestingApiEndpoints(NewForkedTestingApi( | 
					
						
							| 
									
										
										
										
											2022-02-05 01:42:04 +08:00
										 |  |  | 		&TestingApiSrv{ | 
					
						
							| 
									
										
										
										
											2021-12-13 16:22:57 +08:00
										 |  |  | 			AlertingProxy:     proxy, | 
					
						
							|  |  |  | 			Cfg:               api.Cfg, | 
					
						
							|  |  |  | 			ExpressionService: api.ExpressionService, | 
					
						
							|  |  |  | 			DatasourceCache:   api.DatasourceCache, | 
					
						
							| 
									
										
										
										
											2022-02-04 21:56:37 +08:00
										 |  |  | 			secretsService:    api.SecretsService, | 
					
						
							| 
									
										
										
										
											2021-12-13 16:22:57 +08:00
										 |  |  | 			log:               logger, | 
					
						
							|  |  |  | 		}), m) | 
					
						
							|  |  |  | 	api.RegisterConfigurationApiEndpoints(NewForkedConfiguration( | 
					
						
							| 
									
										
										
										
											2022-02-05 01:42:04 +08:00
										 |  |  | 		&AdminSrv{ | 
					
						
							| 
									
										
										
										
											2021-12-13 16:22:57 +08:00
										 |  |  | 			store:     api.AdminConfigStore, | 
					
						
							|  |  |  | 			log:       logger, | 
					
						
							|  |  |  | 			scheduler: api.Schedule, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	), m) | 
					
						
							| 
									
										
										
										
											2020-12-17 22:00:09 +08:00
										 |  |  | } |