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 反序列化的方法。