func (sp *scrapePool) sync(targets []*Target) {标签:hash,target,sp,切片,timeout,时避,config,loop,指针 From: https://www.cnblogs.com/cheyunhua/p/18470333
// 加锁
sp.mtx.Lock()
defer sp.mtx.Unlock()
var (
// target 标记
uniqueTargets = map[uint64]struct{}{}
// 采集周期
interval = time.Duration(sp.config.ScrapeInterval)
// 采集超时时间
timeout = time.Duration(sp.config.ScrapeTimeout)
limit = int(sp.config.SampleLimit)
// 重复lable是否覆盖
honorLabels = sp.config.HonorLabels
honorTimestamps = sp.config.HonorTimestamps
mrc = sp.config.MetricRelabelConfigs
)
// 遍历all队列中的所有target
for _, t := range targets {
// 赋值,避免range的坑,带指针类型的切片
t := t
// 生成对应的hash(对该hash算法感兴趣可以看下这里的源码)
hash := t.hash()
// 标记
uniqueTargets[hash] = struct{}{}
// 判断该taget是否已经在运行了。如果没有则运行该target对应的loop,将该loop加入activeTargets中
if _, ok := sp.activeTargets[hash]; !ok {
s := &targetScraper{Target: t, client: sp.client, timeout: timeout}
l := sp.newLoop(scrapeLoopOptions{
target: t,
scraper: s,
limit: limit,
honorLabels: honorLabels,
honorTimestamps: honorTimestamps,
mrc: mrc,
})
sp.activeTargets[hash] = t
sp.loops[hash] = l
// 启动该loop
go l.run(interval, timeout, nil)
} else {
// 该target对应的loop已经运行,设置最新的标签信息
sp.activeTargets[hash].SetDiscoveredLabels(t.DiscoveredLabels())
}
}