Prometheus源码学习(7) targetgroup

targroup 是抓取目标

// Group is a set of targets with a common label set(production , test, staging etc.).
// Group 是一组目标的集合,这组目标有一个共同的标签集。
type Group struct {
	// Targets is a list of targets identified by a label set. Each target is
	// uniquely identifiable in the group by its address label.
	// Targets 是一个目标的列表,由一个标签集标识。一个目标在组中由它的 address 标签标识。
	Targets []model.LabelSet
	// Labels is a set of labels that is common across all targets in the group.
	// Labels 是一个标签集合,这个标签集合是整个目标组共用的。
	Labels model.LabelSet

	// Source is an identifier that describes a group of targets.
	// Source 唯一描述一组目标的字符串
	Source string
}

LabelSet 是一组标签名和标签值的映射。LabelName 和 LabelValue 都是字符串。github.com/prometheus/common/model/labelset.go

type LabelSet map[LabelName]LabelValue

反序列化犯法实现 yaml.Unmarshaler 接口

// UnmarshalYAML implements the yaml.Unmarshaler interface.
func (tg *Group) UnmarshalYAML(unmarshal func(interface{}) error) error {
	g := struct {
		Targets []string       `yaml:"targets"`
		Labels  model.LabelSet `yaml:"labels"`
	}{}
	if err := unmarshal(&g); err != nil {
		return err
	}
	tg.Targets = make([]model.LabelSet, 0, len(g.Targets))
	for _, t := range g.Targets {
		tg.Targets = append(tg.Targets, model.LabelSet{
			model.AddressLabel: model.LabelValue(t),
		})
	}
	tg.Labels = g.Labels
	return nil
}

例如配置文件中 scrape_configs 段是这样做的

scrape_configs:
  # The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
  - job_name: 'prometheus'
    static_configs:
    - targets: ['localhost:9090', '192.168.1.2:9091']
      labels:
        cluster: es
        env: prod

那么其中的 static_configs 会解析为

targetgroup.Group{
	Targets: []model.LabelSet{
		model.LabelSet{"__address__": "localhost:9090"}, 
		model.LabelSet{"__address__": "192.168.1.2:9091"}
		}, 
	Labels: model.LabelSet{
		"cluster": "es", 
		"env": "prod"
		}, 
		Source: ""

包中还定义了 yaml 序列化和 json 反序列化的方法。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值