@@ -25,6 +25,7 @@ import (
2525 "github.com/thanos-io/thanos/pkg/store"
2626 "github.com/thanos-io/thanos/pkg/store/labelpb"
2727 "github.com/thanos-io/thanos/pkg/store/storepb"
28+ "github.com/thanos-io/thanos/pkg/targets/targetspb"
2829)
2930
3031const (
@@ -50,6 +51,11 @@ type RuleSpec interface {
5051 Addr () string
5152}
5253
54+ type TargetSpec interface {
55+ // Addr returns TargetsAPI Address for the targets spec. It is used as its ID.
56+ Addr () string
57+ }
58+
5359type MetadataSpec interface {
5460 // Addr returns MetadataAPI Address for the metadata spec. It is used as its ID.
5561 Addr () string
@@ -187,6 +193,7 @@ type StoreSet struct {
187193 // accessible and we close gRPC client for it.
188194 storeSpecs func () []StoreSpec
189195 ruleSpecs func () []RuleSpec
196+ targetSpecs func () []TargetSpec
190197 metadataSpecs func () []MetadataSpec
191198 dialOpts []grpc.DialOption
192199 gRPCInfoCallTimeout time.Duration
@@ -210,6 +217,7 @@ func NewStoreSet(
210217 reg * prometheus.Registry ,
211218 storeSpecs func () []StoreSpec ,
212219 ruleSpecs func () []RuleSpec ,
220+ targetSpecs func () []TargetSpec ,
213221 metadataSpecs func () []MetadataSpec ,
214222 dialOpts []grpc.DialOption ,
215223 unhealthyStoreTimeout time.Duration ,
@@ -228,6 +236,9 @@ func NewStoreSet(
228236 if ruleSpecs == nil {
229237 ruleSpecs = func () []RuleSpec { return nil }
230238 }
239+ if targetSpecs == nil {
240+ targetSpecs = func () []TargetSpec { return nil }
241+ }
231242 if metadataSpecs == nil {
232243 metadataSpecs = func () []MetadataSpec { return nil }
233244 }
@@ -236,6 +247,7 @@ func NewStoreSet(
236247 logger : log .With (logger , "component" , "storeset" ),
237248 storeSpecs : storeSpecs ,
238249 ruleSpecs : ruleSpecs ,
250+ targetSpecs : targetSpecs ,
239251 metadataSpecs : metadataSpecs ,
240252 dialOpts : dialOpts ,
241253 storesMetric : storesMetric ,
@@ -258,6 +270,9 @@ type storeRef struct {
258270 rule rulespb.RulesClient
259271 metadata metadatapb.MetadataClient
260272
273+ // If target is not nil, then this store also supports targets API.
274+ target targetspb.TargetsClient
275+
261276 // Meta (can change during runtime).
262277 labelSets []labels.Labels
263278 storeType component.StoreAPI
@@ -267,7 +282,7 @@ type storeRef struct {
267282 logger log.Logger
268283}
269284
270- func (s * storeRef ) Update (labelSets []labels.Labels , minTime int64 , maxTime int64 , storeType component.StoreAPI , rule rulespb.RulesClient , metadata metadatapb.MetadataClient ) {
285+ func (s * storeRef ) Update (labelSets []labels.Labels , minTime int64 , maxTime int64 , storeType component.StoreAPI , rule rulespb.RulesClient , target targetspb. TargetsClient , metadata metadatapb.MetadataClient ) {
271286 s .mtx .Lock ()
272287 defer s .mtx .Unlock ()
273288
@@ -276,6 +291,7 @@ func (s *storeRef) Update(labelSets []labels.Labels, minTime int64, maxTime int6
276291 s .minTime = minTime
277292 s .maxTime = maxTime
278293 s .rule = rule
294+ s .target = target
279295 s .metadata = metadata
280296}
281297
@@ -293,6 +309,13 @@ func (s *storeRef) HasRulesAPI() bool {
293309 return s .rule != nil
294310}
295311
312+ func (s * storeRef ) HasTargetsAPI () bool {
313+ s .mtx .RLock ()
314+ defer s .mtx .RUnlock ()
315+
316+ return s .target != nil
317+ }
318+
296319func (s * storeRef ) HasMetadataAPI () bool {
297320 s .mtx .RLock ()
298321 defer s .mtx .RUnlock ()
@@ -405,6 +428,10 @@ func (s *StoreSet) Update(ctx context.Context) {
405428 level .Info (s .logger ).Log ("msg" , "adding new rulesAPI to query storeset" , "address" , addr )
406429 }
407430
431+ if st .HasTargetsAPI () {
432+ level .Info (s .logger ).Log ("msg" , "adding new targetsAPI to query storeset" , "address" , addr )
433+ }
434+
408435 level .Info (s .logger ).Log ("msg" , "adding new storeAPI to query storeset" , "address" , addr , "extLset" , extLset )
409436 }
410437
@@ -425,6 +452,7 @@ func (s *StoreSet) getActiveStores(ctx context.Context, stores map[string]*store
425452
426453 storeAddrSet = make (map [string ]struct {})
427454 ruleAddrSet = make (map [string ]struct {})
455+ targetAddrSet = make (map [string ]struct {})
428456 metadataAddrSet = make (map [string ]struct {})
429457 )
430458
@@ -433,6 +461,11 @@ func (s *StoreSet) getActiveStores(ctx context.Context, stores map[string]*store
433461 ruleAddrSet [ruleSpec .Addr ()] = struct {}{}
434462 }
435463
464+ // Gather active targets map concurrently. Add a new target if it does not exist already.
465+ for _ , targetSpec := range s .targetSpecs () {
466+ targetAddrSet [targetSpec .Addr ()] = struct {}{}
467+ }
468+
436469 // Gather active stores map concurrently. Build new store if does not exist already.
437470 for _ , metadataSpec := range s .metadataSpecs () {
438471 metadataAddrSet [metadataSpec .Addr ()] = struct {}{}
@@ -473,6 +506,11 @@ func (s *StoreSet) getActiveStores(ctx context.Context, stores map[string]*store
473506 rule = rulespb .NewRulesClient (st .cc )
474507 }
475508
509+ var target targetspb.TargetsClient
510+ if _ , ok := targetAddrSet [addr ]; ok {
511+ target = targetspb .NewTargetsClient (st .cc )
512+ }
513+
476514 var metadata metadatapb.MetadataClient
477515 if _ , ok := metadataAddrSet [addr ]; ok {
478516 metadata = metadatapb .NewMetadataClient (st .cc )
@@ -502,7 +540,7 @@ func (s *StoreSet) getActiveStores(ctx context.Context, stores map[string]*store
502540 }
503541
504542 s .updateStoreStatus (st , nil )
505- st .Update (labelSets , minTime , maxTime , storeType , rule , metadata )
543+ st .Update (labelSets , minTime , maxTime , storeType , rule , target , metadata )
506544
507545 mtx .Lock ()
508546 defer mtx .Unlock ()
@@ -586,6 +624,20 @@ func (s *StoreSet) GetRulesClients() []rulespb.RulesClient {
586624 return rules
587625}
588626
627+ // GetTargetsClients returns a list of all active targets clients.
628+ func (s * StoreSet ) GetTargetsClients () []targetspb.TargetsClient {
629+ s .storesMtx .RLock ()
630+ defer s .storesMtx .RUnlock ()
631+
632+ targets := make ([]targetspb.TargetsClient , 0 , len (s .stores ))
633+ for _ , st := range s .stores {
634+ if st .HasTargetsAPI () {
635+ targets = append (targets , st .target )
636+ }
637+ }
638+ return targets
639+ }
640+
589641// GetMetadataClients returns a list of all active metadata clients.
590642func (s * StoreSet ) GetMetadataClients () []metadatapb.MetadataClient {
591643 s .storesMtx .RLock ()
0 commit comments