Prometheus源码学习(7) targetgroup

news/2024/5/18 22:21:52 标签: golang, prometheus

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


http://www.niftyadmin.cn/n/1531796.html

相关文章

goroutine生命周期管理-errgroup和run

Go 语言有提供了多个包来将多个 goroutine 的生命周期编组管理。最简单的是标准库的 sync .WaitGroup&#xff0c;应用比较普遍的是 google 的 errgroup&#xff0c;Prometheus 用的是 oklog 的 run。下面学习后两个包的用法。 errgroup errgroup 为作为一个任务中的多个子任…

Prometheus源码学习(8) scrape总体流程

1. main 函数中初始化 scrapeManager 实例 // 初始化 scrapeManager&#xff0c;fanout Storage 是一个读写多个底层存储的代理 scrapeManager scrape.NewManager(log.With(logger, "component", "scrape manager"), fanoutStorage)fanoutStorage 是读写…

Prometheus源码学习(9) scrape-target

主要作用 scrape.Target 是一次抓取的具体对象&#xff0c;包含了抓取和抓取后存储所需要的全部信息。从 targetGroup.Group 到 scrape.Target 的转换过程如下&#xff1a; targetsFromGroup函数遍历每个targetGroup.Group中的Target&#xff0c;合并targetGroup.Group的公共…

Golang channel 注意事项

channel 是引用类型&#xff0c;仅仅 var 声明而没有 make 初始化的 channel 值是nil&#xff0c;channel 值可以用 比较&#xff0c;可以判断 nil。读、写 nil 的 channel 都会阻塞&#xff0c;可以通过将 channel 置为 nil 来屏蔽 select 中的某个 channel。关闭 nil 或 已…

Kubernetes 集群DNS选择:CoreDNS vs Kube-DNS

在二进制部署 Kubernetes 集群时&#xff0c;最后一步是部署 DNS&#xff0c;有两个选项&#xff1a;CoreDNS 和 Kube-DNS&#xff0c;二者主要有什么区别&#xff0c;如何选择呢&#xff1f; CoreDNS 和 Kube-DNS 作为 Kubernetes 集群的 DNS 服务提供者&#xff0c;在做用和…

Golang 设置操作超时的一种方法

读 etcd 源码看到一种超时的设置方法,控制一个循环的总时长&#xff0c;每轮迭代检查一下是否超时&#xff0c;适用于对超时时间要求不是非常精细&#xff0c;并且操作不会阻塞的场景 func main() {ctx, cancel : context.WithTimeout(context.Background(), 6*time.Second)de…

Prometheus源码学习(11)-common_model

文章目录lables.golabelset.gometric.govalue.goalert.gofnv.gosignature.go收获github.com/prometheus/commonv0.35.0/modellables.go 首先声明一系列标签常量&#xff0c;其中 __meta_ 和 __tmp_ 前缀用于标签的中间处理标签名 LabelName 是字符串&#xff0c;命名规范是“可…

sql between and 复制表

1 -between and 包括边界值&#xff0c;如果是日期 截止日期到00点0分 如果截止日期&#xff1a;2019-7-26 则为&#xff1a;2019-7-26 00&#xff1a;00&#xff1a;00 not between adn 不包括边界值 2 复制表 select * into newtable from oldtable where 12 insert into new…