对带有指针类型的切片进行循环时避坑
func (sp *scrapePool) sync(targets []*Target) {
// 加锁
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的坑,带指针类型的切片,go sdk新版本已忽悠
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())
}
}
分类:
GO
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
2023-10-16 详解k8s 4种类型Service
2020-10-16 Linux系统只有root可以开始低于1023端口的服务
2020-10-16 nc测试udp端口是否成功
2020-10-16 jenkins解决构建完成后自动杀死进程的问题