前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【Prometheus】看着Prometheus学Golang(1)

【Prometheus】看着Prometheus学Golang(1)

作者头像
runzhliu
发布于 2021-03-02 08:11:13
发布于 2021-03-02 08:11:13
75000
代码可运行
举报
文章被收录于专栏:容器计算容器计算
运行总次数:0
代码可运行

一直觉得自己的 Golang 水平一般般,所以决定还是找个开源项目好好学一下,虽然说按我的水平不见得就能看出来哪个项目写的好,但至少开源的而且被广泛使用,包括我自己的项目,还是有点信心的,所以就挑 Prometheus 来学。 首先比较感兴趣的是 discovery 模块,因为这个模块相对比较独立,单独拉出来可能会比较容易理解吧,这里的功能简单来说就是当用户添加一些 targets 或者 endpoint,反正就是你需要采集的数据的来源如果没增加一个,你就需要手动去 prometheus.yml 这个文件加一点东西,那也太低效了吧!而且还特别容易出纰漏,比如说加了一行,但不小心又删掉一行。所以这里 discovery 做的事情就是所谓的自动发现,用户可以少做点东西,但是又能保证不丢采集的来源数据。

结构还是很清晰的,在 discovery 包下面有不同的文件夹,也就是 discovery 的不同方式,先找重点,看看接口怎么定义的,然后挑个简单的再看看,比如 file 是怎么实现的。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// 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 的扩展性非常好,这种设计是值得学习的。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/02/13 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档