Prometheus指标数据同步机制

本文详细解析了Prometheus的指标数据同步机制,包括源码分析、配置同步、数据处理入口以及数据同步的具体步骤。每个抓取目标由单独的goroutine按固定间隔进行指标抓取,抓取后的数据经过处理后写入底层存储。
摘要由CSDN通过智能技术生成

源码地址:https://github.com/prometheus/prometheus

整体代码框架大致如下:

  • 由scrape.Manager管理所有的抓取对象;

  • 所有的抓取对象按group分组,每个group是一个job_name;

  • 每个group下含多个scrapeTarget,即具体的抓取目标endpoint;

  • 对每个目标endpoint,启动一个抓取goroutine,按照interval间隔循环的抓取对象的指标;

同步配置

假如prometheus.yaml中的抓取配置为:

scrape_configs:
  - job_name: "monitor"
    static_configs:
    - targets: ['192.168.101.9:11504']


  - job_name: 'node-exporter'
    static_configs:
    - targets: ['10.21.1.74:9100', '192.168.101.9:9100']

那么,抓取对象将按如下结构分组:

{
    "monitor": [
        {
            "Targets": [
                {
                    "__address__": "192.168.101.9:11504"
                }
            ],
            "Labels": null,
            "Source": "0"
        }
    ],
    "node-exporter": [
        {
            "Targets": [
                {
                    "__address__": "10.21.1.74:9100"
                },
                {
                    "__address__": "192.168.101.9:9100"
                }
            ],
            "Labels": null,
            "Source": "0"
        }
    ]
}

入口方法

下面来看下数据处理的入口方法:

func (m *Manager) reload() {
   m.mtxScrape.Lock()
   var wg sync.WaitGroup
   for setName, groups := range m.targetSets {
      if _, ok := m.scrapePools[setName]; !ok {
         scrapeConfig, ok := m.scrapeConfigs[setName]
         if !ok {
            level.Error(m.logger).Log("msg", "error reloading target set", "err", "invalid config id:"+setName)
            continue
         }
         sp, err := newScrapePool(scrapeConfig, m.append, m.jitterSeed, log.With(m.logger, "scrape_pool", setName), m.opts)
         if err != nil {
            level.Error(m.logger).Log("msg", "error creating new scrape pool", "err", err, "scrape_pool", setName)
            continue
         }
         m.scrapePools[setName] = sp
      }

      wg.Add(1)
      // Run the sync in parallel as these take a while and at high load can't catch up.
      go func(sp *scrapePool, groups []*targetgroup.Group) {
         sp.Sync(groups)
         wg.Done()
      }(m.scrapePools[s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值