一直觉得自己的 Golang 水平一般般,所以决定还是找个开源项目好好学一下,虽然说按我的水平不见得就能看出来哪个项目写的好,但至少开源的而且被广泛使用,包括我自己的项目,还是有点信心的,所以就挑 Prometheus 来学。
首先比较感兴趣的是 discovery 模块,因为这个模块相对比较独立,单独拉出来可能会比较容易理解吧,这里的功能简单来说就是当用户添加一些 targets
或者 endpoint
,反正就是你需要采集的数据的来源如果没增加一个,你就需要手动去 prometheus.yml 这个文件加一点东西,那也太低效了吧!而且还特别容易出纰漏,比如说加了一行,但不小心又删掉一行。所以这里 discovery 做的事情就是所谓的自动发现,用户可以少做点东西,但是又能保证不丢采集的来源数据。
结构还是很清晰的,在 discovery 包下面有不同的文件夹,也就是 discovery 的不同方式,先找重点,看看接口怎么定义的,然后挑个简单的再看看,比如 file 是怎么实现的。
// Discoverer provides information about target groups. It maintains a set
// of sources from which TargetGroups can originate. Whenever a discovery provider
// detects a potential change, it sends the TargetGroup through its channel.
//
// Discoverer does not know if an actual change happened.
// It does guarantee that it sends the new TargetGroup whenever a change happens.
//
// Discoverers should initially send a full set of all discoverable TargetGroups.
type Discoverer interface {
// Run hands a channel to the discovery provider (Consul, DNS, etc.) through which
// it can send updated target groups. It must return when the context is canceled.
// It should not close the update channel on returning.
Run(ctx context.Context, up chan<- []*targetgroup.Group)
}
Discoverer
接口就一个方法,就是 Run
,所以下面的文件夹,包括 file, zookeeper,这些只要实现这个 Run
方法就可以了。
NewManager
是在 Main.go 里执行的。
这里没什么特别的,就是 Prometheus 也要收集自己的指标。
Discovery
的构造方法。
SDConfig
的几个方法。
这么一看 Discover
的扩展性非常好,这种设计是值得学习的。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有