@@ -138,12 +138,16 @@ func (m *Discoverer) Run() error {
138
138
return nil
139
139
}
140
140
141
+ // UpdateTsets updates the target sets to be scraped.
141
142
func (m * Discoverer ) UpdateTsets (tsets map [string ][]* targetgroup.Group ) {
142
143
m .mtxScrape .Lock ()
143
144
m .targetSets = tsets
144
145
m .mtxScrape .Unlock ()
145
146
}
146
147
148
+ // reloader triggers a reload of the scrape configs at regular intervals.
149
+ // The time between reloads is defined by reloadIntervalDuration to avoid overloading the system
150
+ // with too many reloads, because some service discovery mechanisms can be quite chatty.
147
151
func (m * Discoverer ) reloader () {
148
152
reloadIntervalDuration := model .Duration (5 * time .Second )
149
153
ticker := time .NewTicker (time .Duration (reloadIntervalDuration ))
@@ -165,6 +169,8 @@ func (m *Discoverer) reloader() {
165
169
}
166
170
}
167
171
172
+ // Reload triggers a reload of the scrape configs.
173
+ // This will process the target groups and update the targets concurrently.
168
174
func (m * Discoverer ) Reload () {
169
175
m .mtxScrape .Lock ()
170
176
var wg sync.WaitGroup
@@ -176,7 +182,12 @@ func (m *Discoverer) Reload() {
176
182
wg .Add (1 )
177
183
// Run the sync in parallel as these take a while and at high load can't catch up.
178
184
go func (jobName string , groups []* targetgroup.Group ) {
179
- m .processTargetGroups (jobName , groups , targets )
185
+ processedTargets := m .processTargetGroups (jobName , groups )
186
+ m .mtxTargets .Lock ()
187
+ for k , v := range processedTargets {
188
+ targets [k ] = v
189
+ }
190
+ m .mtxTargets .Unlock ()
180
191
wg .Done ()
181
192
}(jobName , groups )
182
193
}
@@ -185,9 +196,11 @@ func (m *Discoverer) Reload() {
185
196
m .processTargetsCallBack (targets )
186
197
}
187
198
188
- func (m * Discoverer ) processTargetGroups (jobName string , groups []* targetgroup.Group , targets map [string ]* Item ) {
199
+ // processTargetGroups processes the target groups and returns a map of targets.
200
+ func (m * Discoverer ) processTargetGroups (jobName string , groups []* targetgroup.Group ) map [string ]* Item {
189
201
builder := labels .NewBuilder (labels.Labels {})
190
202
timer := prometheus .NewTimer (processTargetGroupsDuration .WithLabelValues (jobName ))
203
+ targets := map [string ]* Item {}
191
204
defer timer .ObserveDuration ()
192
205
var count float64 = 0
193
206
for _ , tg := range groups {
@@ -203,12 +216,11 @@ func (m *Discoverer) processTargetGroups(jobName string, groups []*targetgroup.G
203
216
builder .Set (string (ln ), string (lv ))
204
217
}
205
218
item := NewItem (jobName , string (t [model .AddressLabel ]), builder .Labels (), "" )
206
- m .mtxTargets .Lock ()
207
219
targets [item .Hash ()] = item
208
- m .mtxTargets .Unlock ()
209
220
}
210
221
}
211
222
targetsDiscovered .WithLabelValues (jobName ).Set (count )
223
+ return targets
212
224
}
213
225
214
226
// Run receives and saves target set updates and triggers the scraping loops reloading.
0 commit comments