3232 registrationTimeout = kingpin .Flag ("registration.timeout" , "After how long a registration expires." ).Default ("5m" ).Duration ()
3333)
3434
35+ type ClientInfo struct {
36+ LastSeen time.Time
37+ Labels map [string ]string
38+ }
39+
3540// Coordinator metrics.
3641var (
3742 knownClients = promauto .NewGauge (
@@ -51,8 +56,8 @@ type Coordinator struct {
5156 waiting map [string ]chan * http.Request
5257 // Responses from clients.
5358 responses map [string ]chan * http.Response
54- // Clients we know about and when they last contacted us .
55- known map [string ]time. Time
59+ // Clients we know about with their labels and last contact time .
60+ known map [string ]ClientInfo
5661
5762 logger * slog.Logger
5863}
@@ -62,7 +67,7 @@ func NewCoordinator(logger *slog.Logger) (*Coordinator, error) {
6267 c := & Coordinator {
6368 waiting : map [string ]chan * http.Request {},
6469 responses : map [string ]chan * http.Response {},
65- known : map [string ]time. Time {},
70+ known : map [string ]ClientInfo {},
6671 logger : logger ,
6772 }
6873
@@ -131,10 +136,10 @@ func (c *Coordinator) DoScrape(ctx context.Context, r *http.Request) (*http.Resp
131136}
132137
133138// WaitForScrapeInstruction registers a client waiting for a scrape result
134- func (c * Coordinator ) WaitForScrapeInstruction (fqdn string ) (* http.Request , error ) {
135- c .logger .Info ("WaitForScrapeInstruction" , "fqdn" , fqdn )
139+ func (c * Coordinator ) WaitForScrapeInstruction (fqdn string , labels map [ string ] string ) (* http.Request , error ) {
140+ c .logger .Info ("WaitForScrapeInstruction" , "fqdn" , fqdn , "labels" , labels )
136141
137- c .addKnownClient (fqdn )
142+ c .addKnownClient (fqdn , labels )
138143 // TODO: What if the client times out?
139144 ch := c .getRequestChannel (fqdn )
140145
@@ -179,24 +184,31 @@ func (c *Coordinator) ScrapeResult(r *http.Response) error {
179184 }
180185}
181186
182- func (c * Coordinator ) addKnownClient (fqdn string ) {
187+ func (c * Coordinator ) addKnownClient (fqdn string , labels map [ string ] string ) {
183188 c .mu .Lock ()
184189 defer c .mu .Unlock ()
185190
186- c .known [fqdn ] = time .Now ()
191+ if labels == nil {
192+ labels = make (map [string ]string )
193+ }
194+
195+ c .known [fqdn ] = ClientInfo {
196+ LastSeen : time .Now (),
197+ Labels : labels ,
198+ }
187199 knownClients .Set (float64 (len (c .known )))
188200}
189201
190- // KnownClients returns a list of alive clients
191- func (c * Coordinator ) KnownClients () [] string {
202+ // KnownClients returns a map of alive clients with their info
203+ func (c * Coordinator ) KnownClients () map [ string ] ClientInfo {
192204 c .mu .Lock ()
193205 defer c .mu .Unlock ()
194206
195207 limit := time .Now ().Add (- * registrationTimeout )
196- known := make ([] string , 0 , len ( c . known ) )
197- for k , t := range c .known {
198- if limit .Before (t ) {
199- known = append ( known , k )
208+ known := make (map [ string ] ClientInfo )
209+ for fqdn , info := range c .known {
210+ if limit .Before (info . LastSeen ) {
211+ known [ fqdn ] = info
200212 }
201213 }
202214 return known
@@ -210,9 +222,9 @@ func (c *Coordinator) gc() {
210222 defer c .mu .Unlock ()
211223 limit := time .Now ().Add (- * registrationTimeout )
212224 deleted := 0
213- for k , ts := range c .known {
214- if ts .Before (limit ) {
215- delete (c .known , k )
225+ for fqdn , info := range c .known {
226+ if info . LastSeen .Before (limit ) {
227+ delete (c .known , fqdn )
216228 deleted ++
217229 }
218230 }
0 commit comments