API: Allow AlertmanagerRetriever and RulesRetriever to receive a Context (#7256)

* API: Allow AlertmanagerRetriever and RulesRetriever to receive a
Context

Signed-off-by: gotjosh <josue@grafana.com>

* Use single line functions where possible

Signed-off-by: gotjosh <josue@grafana.com>
This commit is contained in:
gotjosh 2020-05-18 19:02:32 +01:00 committed by GitHub
parent 44cdd3e768
commit bfcd9282a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 22 deletions

View File

@ -105,12 +105,14 @@ type TargetRetriever interface {
TargetsDropped() map[string][]*scrape.Target TargetsDropped() map[string][]*scrape.Target
} }
type alertmanagerRetriever interface { // AlertmanagerRetriever provides a list of all/dropped AlertManager URLs.
type AlertmanagerRetriever interface {
Alertmanagers() []*url.URL Alertmanagers() []*url.URL
DroppedAlertmanagers() []*url.URL DroppedAlertmanagers() []*url.URL
} }
type rulesRetriever interface { // RulesRetriever provides a list of active rules and alerts.
type RulesRetriever interface {
RuleGroups() []*rules.Group RuleGroups() []*rules.Group
AlertingRules() []*rules.AlertingRule AlertingRules() []*rules.AlertingRule
} }
@ -174,8 +176,8 @@ type API struct {
QueryEngine *promql.Engine QueryEngine *promql.Engine
targetRetriever func(context.Context) TargetRetriever targetRetriever func(context.Context) TargetRetriever
alertmanagerRetriever alertmanagerRetriever alertmanagerRetriever func(context.Context) AlertmanagerRetriever
rulesRetriever rulesRetriever rulesRetriever func(context.Context) RulesRetriever
now func() time.Time now func() time.Time
config func() config.Config config func() config.Config
flagsMap map[string]string flagsMap map[string]string
@ -204,7 +206,7 @@ func NewAPI(
qe *promql.Engine, qe *promql.Engine,
q storage.Queryable, q storage.Queryable,
tr func(context.Context) TargetRetriever, tr func(context.Context) TargetRetriever,
ar alertmanagerRetriever, ar func(context.Context) AlertmanagerRetriever,
configFunc func() config.Config, configFunc func() config.Config,
flagsMap map[string]string, flagsMap map[string]string,
globalURLOptions GlobalURLOptions, globalURLOptions GlobalURLOptions,
@ -213,7 +215,7 @@ func NewAPI(
dbDir string, dbDir string,
enableAdmin bool, enableAdmin bool,
logger log.Logger, logger log.Logger,
rr rulesRetriever, rr func(context.Context) RulesRetriever,
remoteReadSampleLimit int, remoteReadSampleLimit int,
remoteReadConcurrencyLimit int, remoteReadConcurrencyLimit int,
remoteReadMaxBytesInFrame int, remoteReadMaxBytesInFrame int,
@ -847,8 +849,8 @@ type AlertmanagerTarget struct {
} }
func (api *API) alertmanagers(r *http.Request) apiFuncResult { func (api *API) alertmanagers(r *http.Request) apiFuncResult {
urls := api.alertmanagerRetriever.Alertmanagers() urls := api.alertmanagerRetriever(r.Context()).Alertmanagers()
droppedURLS := api.alertmanagerRetriever.DroppedAlertmanagers() droppedURLS := api.alertmanagerRetriever(r.Context()).DroppedAlertmanagers()
ams := &AlertmanagerDiscovery{ActiveAlertmanagers: make([]*AlertmanagerTarget, len(urls)), DroppedAlertmanagers: make([]*AlertmanagerTarget, len(droppedURLS))} ams := &AlertmanagerDiscovery{ActiveAlertmanagers: make([]*AlertmanagerTarget, len(urls)), DroppedAlertmanagers: make([]*AlertmanagerTarget, len(droppedURLS))}
for i, url := range urls { for i, url := range urls {
ams.ActiveAlertmanagers[i] = &AlertmanagerTarget{URL: url.String()} ams.ActiveAlertmanagers[i] = &AlertmanagerTarget{URL: url.String()}
@ -874,7 +876,7 @@ type Alert struct {
} }
func (api *API) alerts(r *http.Request) apiFuncResult { func (api *API) alerts(r *http.Request) apiFuncResult {
alertingRules := api.rulesRetriever.AlertingRules() alertingRules := api.rulesRetriever(r.Context()).AlertingRules()
alerts := []*Alert{} alerts := []*Alert{}
for _, alertingRule := range alertingRules { for _, alertingRule := range alertingRules {
@ -1021,7 +1023,7 @@ type recordingRule struct {
} }
func (api *API) rules(r *http.Request) apiFuncResult { func (api *API) rules(r *http.Request) apiFuncResult {
ruleGroups := api.rulesRetriever.RuleGroups() ruleGroups := api.rulesRetriever(r.Context()).RuleGroups()
res := &RuleDiscovery{RuleGroups: make([]*RuleGroup, len(ruleGroups))} res := &RuleDiscovery{RuleGroups: make([]*RuleGroup, len(ruleGroups))}
typeParam := strings.ToLower(r.URL.Query().Get("type")) typeParam := strings.ToLower(r.URL.Query().Get("type"))

View File

@ -164,9 +164,7 @@ func (t *testTargetRetriever) ResetMetadataStore() {
} }
func (t *testTargetRetriever) toFactory() func(context.Context) TargetRetriever { func (t *testTargetRetriever) toFactory() func(context.Context) TargetRetriever {
return func(context.Context) TargetRetriever { return func(context.Context) TargetRetriever { return t }
return t
}
} }
type testAlertmanagerRetriever struct{} type testAlertmanagerRetriever struct{}
@ -191,6 +189,10 @@ func (t testAlertmanagerRetriever) DroppedAlertmanagers() []*url.URL {
} }
} }
func (t testAlertmanagerRetriever) toFactory() func(context.Context) AlertmanagerRetriever {
return func(context.Context) AlertmanagerRetriever { return t }
}
type rulesRetrieverMock struct { type rulesRetrieverMock struct {
testing *testing.T testing *testing.T
} }
@ -276,6 +278,10 @@ func (m rulesRetrieverMock) RuleGroups() []*rules.Group {
return []*rules.Group{group} return []*rules.Group{group}
} }
func (m rulesRetrieverMock) toFactory() func(context.Context) RulesRetriever {
return func(context.Context) RulesRetriever { return m }
}
var samplePrometheusCfg = config.Config{ var samplePrometheusCfg = config.Config{
GlobalConfig: config.GlobalConfig{}, GlobalConfig: config.GlobalConfig{},
AlertingConfig: config.AlertingConfig{}, AlertingConfig: config.AlertingConfig{},
@ -318,12 +324,12 @@ func TestEndpoints(t *testing.T) {
Queryable: suite.Storage(), Queryable: suite.Storage(),
QueryEngine: suite.QueryEngine(), QueryEngine: suite.QueryEngine(),
targetRetriever: testTargetRetriever.toFactory(), targetRetriever: testTargetRetriever.toFactory(),
alertmanagerRetriever: testAlertmanagerRetriever{}, alertmanagerRetriever: testAlertmanagerRetriever{}.toFactory(),
flagsMap: sampleFlagMap, flagsMap: sampleFlagMap,
now: func() time.Time { return now }, now: func() time.Time { return now },
config: func() config.Config { return samplePrometheusCfg }, config: func() config.Config { return samplePrometheusCfg },
ready: func(f http.HandlerFunc) http.HandlerFunc { return f }, ready: func(f http.HandlerFunc) http.HandlerFunc { return f },
rulesRetriever: algr, rulesRetriever: algr.toFactory(),
} }
testEndpoints(t, api, testTargetRetriever, true) testEndpoints(t, api, testTargetRetriever, true)
@ -382,12 +388,12 @@ func TestEndpoints(t *testing.T) {
Queryable: remote, Queryable: remote,
QueryEngine: suite.QueryEngine(), QueryEngine: suite.QueryEngine(),
targetRetriever: testTargetRetriever.toFactory(), targetRetriever: testTargetRetriever.toFactory(),
alertmanagerRetriever: testAlertmanagerRetriever{}, alertmanagerRetriever: testAlertmanagerRetriever{}.toFactory(),
flagsMap: sampleFlagMap, flagsMap: sampleFlagMap,
now: func() time.Time { return now }, now: func() time.Time { return now },
config: func() config.Config { return samplePrometheusCfg }, config: func() config.Config { return samplePrometheusCfg },
ready: func(f http.HandlerFunc) http.HandlerFunc { return f }, ready: func(f http.HandlerFunc) http.HandlerFunc { return f },
rulesRetriever: algr, rulesRetriever: algr.toFactory(),
} }
testEndpoints(t, api, testTargetRetriever, false) testEndpoints(t, api, testTargetRetriever, false)

View File

@ -297,10 +297,11 @@ func New(logger log.Logger, o *Options) *Handler {
ready: 0, ready: 0,
} }
factoryTr := func(_ context.Context) api_v1.TargetRetriever { factoryTr := func(_ context.Context) api_v1.TargetRetriever { return h.scrapeManager }
return h.scrapeManager factoryAr := func(_ context.Context) api_v1.AlertmanagerRetriever { return h.notifier }
} FactoryRr := func(_ context.Context) api_v1.RulesRetriever { return h.ruleManager }
h.apiV1 = api_v1.NewAPI(h.queryEngine, h.storage, factoryTr, h.notifier,
h.apiV1 = api_v1.NewAPI(h.queryEngine, h.storage, factoryTr, factoryAr,
func() config.Config { func() config.Config {
h.mtx.RLock() h.mtx.RLock()
defer h.mtx.RUnlock() defer h.mtx.RUnlock()
@ -317,7 +318,7 @@ func New(logger log.Logger, o *Options) *Handler {
h.options.TSDBDir, h.options.TSDBDir,
h.options.EnableAdminAPI, h.options.EnableAdminAPI,
logger, logger,
h.ruleManager, FactoryRr,
h.options.RemoteReadSampleLimit, h.options.RemoteReadSampleLimit,
h.options.RemoteReadConcurrencyLimit, h.options.RemoteReadConcurrencyLimit,
h.options.RemoteReadBytesInFrame, h.options.RemoteReadBytesInFrame,