mirror of
https://github.com/prometheus/prometheus.git
synced 2025-03-05 20:59:13 -08:00
API: Allow TargetRetriever
to receive a Context (#7125)
Fixes #7103 Signed-off-by: gotjosh <josue@grafana.com>
This commit is contained in:
parent
5ab6b043c1
commit
24af5049bb
|
@ -100,7 +100,8 @@ func (e *apiError) Error() string {
|
||||||
return fmt.Sprintf("%s: %s", e.typ, e.err)
|
return fmt.Sprintf("%s: %s", e.typ, e.err)
|
||||||
}
|
}
|
||||||
|
|
||||||
type targetRetriever interface {
|
// TargetRetriever provides the list of active/dropped targets to scrape or not.
|
||||||
|
type TargetRetriever interface {
|
||||||
TargetsActive() map[string][]*scrape.Target
|
TargetsActive() map[string][]*scrape.Target
|
||||||
TargetsDropped() map[string][]*scrape.Target
|
TargetsDropped() map[string][]*scrape.Target
|
||||||
}
|
}
|
||||||
|
@ -173,7 +174,7 @@ type API struct {
|
||||||
Queryable storage.Queryable
|
Queryable storage.Queryable
|
||||||
QueryEngine *promql.Engine
|
QueryEngine *promql.Engine
|
||||||
|
|
||||||
targetRetriever targetRetriever
|
targetRetriever func(context.Context) TargetRetriever
|
||||||
alertmanagerRetriever alertmanagerRetriever
|
alertmanagerRetriever alertmanagerRetriever
|
||||||
rulesRetriever rulesRetriever
|
rulesRetriever rulesRetriever
|
||||||
now func() time.Time
|
now func() time.Time
|
||||||
|
@ -202,7 +203,7 @@ func init() {
|
||||||
func NewAPI(
|
func NewAPI(
|
||||||
qe *promql.Engine,
|
qe *promql.Engine,
|
||||||
q storage.Queryable,
|
q storage.Queryable,
|
||||||
tr targetRetriever,
|
tr func(context.Context) TargetRetriever,
|
||||||
ar alertmanagerRetriever,
|
ar alertmanagerRetriever,
|
||||||
configFunc func() config.Config,
|
configFunc func() config.Config,
|
||||||
flagsMap map[string]string,
|
flagsMap map[string]string,
|
||||||
|
@ -659,7 +660,7 @@ func (api *API) targets(r *http.Request) apiFuncResult {
|
||||||
res := &TargetDiscovery{}
|
res := &TargetDiscovery{}
|
||||||
|
|
||||||
if showActive {
|
if showActive {
|
||||||
targetsActive := api.targetRetriever.TargetsActive()
|
targetsActive := api.targetRetriever(r.Context()).TargetsActive()
|
||||||
activeKeys, numTargets := sortKeys(targetsActive)
|
activeKeys, numTargets := sortKeys(targetsActive)
|
||||||
res.ActiveTargets = make([]*Target, 0, numTargets)
|
res.ActiveTargets = make([]*Target, 0, numTargets)
|
||||||
|
|
||||||
|
@ -697,7 +698,7 @@ func (api *API) targets(r *http.Request) apiFuncResult {
|
||||||
res.ActiveTargets = []*Target{}
|
res.ActiveTargets = []*Target{}
|
||||||
}
|
}
|
||||||
if showDropped {
|
if showDropped {
|
||||||
tDropped := flatten(api.targetRetriever.TargetsDropped())
|
tDropped := flatten(api.targetRetriever(r.Context()).TargetsDropped())
|
||||||
res.DroppedTargets = make([]*DroppedTarget, 0, len(tDropped))
|
res.DroppedTargets = make([]*DroppedTarget, 0, len(tDropped))
|
||||||
for _, t := range tDropped {
|
for _, t := range tDropped {
|
||||||
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
|
res.DroppedTargets = append(res.DroppedTargets, &DroppedTarget{
|
||||||
|
@ -744,7 +745,7 @@ func (api *API) targetMetadata(r *http.Request) apiFuncResult {
|
||||||
metric := r.FormValue("metric")
|
metric := r.FormValue("metric")
|
||||||
|
|
||||||
res := []metricMetadata{}
|
res := []metricMetadata{}
|
||||||
for _, tt := range api.targetRetriever.TargetsActive() {
|
for _, tt := range api.targetRetriever(r.Context()).TargetsActive() {
|
||||||
for _, t := range tt {
|
for _, t := range tt {
|
||||||
if limit >= 0 && len(res) >= limit {
|
if limit >= 0 && len(res) >= limit {
|
||||||
break
|
break
|
||||||
|
@ -877,7 +878,7 @@ func (api *API) metricMetadata(r *http.Request) apiFuncResult {
|
||||||
|
|
||||||
metric := r.FormValue("metric")
|
metric := r.FormValue("metric")
|
||||||
|
|
||||||
for _, tt := range api.targetRetriever.TargetsActive() {
|
for _, tt := range api.targetRetriever(r.Context()).TargetsActive() {
|
||||||
for _, t := range tt {
|
for _, t := range tt {
|
||||||
|
|
||||||
if metric == "" {
|
if metric == "" {
|
||||||
|
|
|
@ -163,6 +163,12 @@ func (t *testTargetRetriever) ResetMetadataStore() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (t *testTargetRetriever) toFactory() func(context.Context) TargetRetriever {
|
||||||
|
return func(context.Context) TargetRetriever {
|
||||||
|
return t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type testAlertmanagerRetriever struct{}
|
type testAlertmanagerRetriever struct{}
|
||||||
|
|
||||||
func (t testAlertmanagerRetriever) Alertmanagers() []*url.URL {
|
func (t testAlertmanagerRetriever) Alertmanagers() []*url.URL {
|
||||||
|
@ -311,7 +317,7 @@ func TestEndpoints(t *testing.T) {
|
||||||
api := &API{
|
api := &API{
|
||||||
Queryable: suite.Storage(),
|
Queryable: suite.Storage(),
|
||||||
QueryEngine: suite.QueryEngine(),
|
QueryEngine: suite.QueryEngine(),
|
||||||
targetRetriever: testTargetRetriever,
|
targetRetriever: testTargetRetriever.toFactory(),
|
||||||
alertmanagerRetriever: testAlertmanagerRetriever{},
|
alertmanagerRetriever: testAlertmanagerRetriever{},
|
||||||
flagsMap: sampleFlagMap,
|
flagsMap: sampleFlagMap,
|
||||||
now: func() time.Time { return now },
|
now: func() time.Time { return now },
|
||||||
|
@ -375,7 +381,7 @@ func TestEndpoints(t *testing.T) {
|
||||||
api := &API{
|
api := &API{
|
||||||
Queryable: remote,
|
Queryable: remote,
|
||||||
QueryEngine: suite.QueryEngine(),
|
QueryEngine: suite.QueryEngine(),
|
||||||
targetRetriever: testTargetRetriever,
|
targetRetriever: testTargetRetriever.toFactory(),
|
||||||
alertmanagerRetriever: testAlertmanagerRetriever{},
|
alertmanagerRetriever: testAlertmanagerRetriever{},
|
||||||
flagsMap: sampleFlagMap,
|
flagsMap: sampleFlagMap,
|
||||||
now: func() time.Time { return now },
|
now: func() time.Time { return now },
|
||||||
|
|
|
@ -293,7 +293,10 @@ func New(logger log.Logger, o *Options) *Handler {
|
||||||
ready: 0,
|
ready: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
h.apiV1 = api_v1.NewAPI(h.queryEngine, h.storage, h.scrapeManager, h.notifier,
|
factoryTr := func(_ context.Context) api_v1.TargetRetriever {
|
||||||
|
return h.scrapeManager
|
||||||
|
}
|
||||||
|
h.apiV1 = api_v1.NewAPI(h.queryEngine, h.storage, factoryTr, h.notifier,
|
||||||
func() config.Config {
|
func() config.Config {
|
||||||
h.mtx.RLock()
|
h.mtx.RLock()
|
||||||
defer h.mtx.RUnlock()
|
defer h.mtx.RUnlock()
|
||||||
|
|
Loading…
Reference in a new issue