IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

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

    crossoverJie发表于 2024-01-25 10:22:13
    love 0

    背景

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

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

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

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

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

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

    编写插件

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

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

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

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

    1
    2
    3
    4
    5
    6
    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 为例。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    # 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

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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 的数据库中)。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    // 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

    #Blog



沪ICP备19023445号-2号
友情链接