手把手教你为开源项目贡献代码

开源
以往的每一个 Exporter 都需要单独部署运维。同时又完全兼容 Prometheus 生态,也可以复用现有的监控面板。恰好这段时间我也在公司从事可观测性相关的业务,发现这确实是一个痛点。于是便一直在关注这个项目,同时也做了些贡献;因为该项目的核心是用于整合 exporter,所以为其编写插件也是非常重要的贡献了。

背景

前段时间无意间看到一篇公众号 招贤令:一起来搞一个新开源项目,作者介绍他想要做一个开源项目:cprobe 用于整合目前市面上散落在各地的 Exporter,统一进行管理。

比如我们常用的 blackbox_exporter/mysqld_exporter 等。

以往的每一个 Exporter 都需要单独部署运维。

同时又完全兼容 Prometheus 生态,也可以复用现有的监控面板。

恰好这段时间我也在公司从事可观测性相关的业务,发现这确实是一个痛点。

于是便一直在关注这个项目,同时也做了些贡献;因为该项目的核心是用于整合 exporter,所以为其编写插件也是非常重要的贡献了。

编写插件

整个项目执行流程图如下:

可以看到编写插件最核心的便是自定义插件解析自定义的配置文件、抓取指标的逻辑。

比如我们需要在配置中指定抓取目标的域名、抓取规则等。

这里  cprobe 已经抽象出了两个接口,我们只需要做对应的实现即可。

type Plugin interface {  
    // ParseConfig is used to parse config  
    ParseConfig(baseDir string, bs []byte) (any, error)  
    // Scrape is used to scrape metrics, cfg need to be cast specific cfg  
    Scrape(ctx context.Context, target string, cfg any, ss *types.Samples) error  
}

下面就以我之前编写的 Consul 为例。

# Allows any Consul server (non-leader) to service a read.  
allow_stale = true  
  
# === CA  
# File path to a PEM-encoded certificate authority used to validate the authenticity of a server certificate.  
ca_file = "/etc/consul.d/consul-agent-ca.pem"  
  
# File path to a PEM-encoded certificate used with the private key to verify the exporter's authenticity.  
cert_file = "/etc/consul.d/consul-agent.pem"  
  
# Generate a health summary for each service instance. Needs n+1 queries to collect all information.  
health_summary = true  
  
# File path to a PEM-encoded private key used with the certificate to verify the exporter's authenticity  
key_file = "/etc/consul.d/consul-agent-key.pem"  
  
# Disable TLS host verification.  
insecure = false

这里每个插件的配置都不相同,所以我们需要将配置解析到具体的结构体中。

func (*Consul) ParseConfig(baseDir string, bs []byte) (any, error) {  
    var c Config  
    err := toml.Unmarshal(bs, &c)  
    if err != nil {  
       return nil, err  
    }  
  
    if c.Timeout == 0 {  
       c.Timeout = time.Millisecond * 500  
    }  
    return &c, nil  
}

解析配置文件没啥好说的,根据自己的逻辑实现即可,可能会配置一些默认值而已。

下面是核心的抓取逻辑,本质上就是使用对应插件的 Client 获取一些核心指标封装为 Prometheus 的 Metric,然后由 cprobe 写入到远端的 Prometheus 中(或者是兼容 Prometheus 的数据库中)。

// Create client
config.HttpClient.Timeout = opts.Timeout  
config.HttpClient.Transport = transport  
  
client, err := consul_api.NewClient(config)  
if err != nil {  
    return nil, err  
}  
  
var requestLimitChan chan struct{}  
if opts.RequestLimit > 0 {  
    requestLimitChan = make(chan struct{}, opts.RequestLimit)  
}

所有的指标数据都是通过对应的客户端获取。

如果是迁移一个存在的  export 到 cprobe 中时,这些抓取代码我们都可以直接复制对应 repo 中的代码。

比如我就是参考的:https://github.com/prometheus/consul_exporter

除非我们是重新写一个插件,不然对于一些流行的库或者是中间件都已经有对应的 exporter 了。

具体的列表可以参考这里:https://prometheus.io/docs/instrumenting/exporters/

之后便需要在对应的插件目录(./conf.d)创建我们的配置文件:

为了方便测试,可以在启动 cprobe 时添加 -no-writer 让指标打印在控制台,从而方便调试。

总结

之前就有人问我有没有毕竟好上手的开源项目,这不就来了吗?

正好目前项目创建时间不长,代码和功能也比较简单,同时还有可观察系统大佬带队,确实是一个非常适合新手参与的开源项目。

项目地址:

https://github.com/cprobe/cprobe

责任编辑:姜华 来源: crossoverJie
相关推荐

2021-05-27 11:10:42

Python开源包代码

2021-09-26 16:08:23

CC++clang_forma

2021-07-14 09:00:00

JavaFX开发应用

2011-05-03 15:59:00

黑盒打印机

2011-01-10 14:41:26

2017-09-05 13:01:11

CocoaPods开源库GitHub

2020-08-12 09:07:53

Python开发爬虫

2023-04-26 12:46:43

DockerSpringKubernetes

2022-03-14 14:47:21

HarmonyOS操作系统鸿蒙

2022-07-27 08:16:22

搜索引擎Lucene

2022-01-08 20:04:20

拦截系统调用

2022-12-07 08:42:35

2021-11-24 16:02:57

鸿蒙HarmonyOS应用

2021-02-26 11:54:38

MyBatis 插件接口

2011-02-22 13:46:27

微软SQL.NET

2021-12-28 08:38:26

Linux 中断唤醒系统Linux 系统

2010-04-29 09:49:26

代码提示SQL Server

2023-09-16 18:48:28

代码逻辑

2018-05-14 16:34:08

Python网络爬虫Scrapy

2010-09-16 14:08:13

无线双网
点赞
收藏

51CTO技术栈公众号