日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
手把手教你為開源項(xiàng)目貢獻(xiàn)代碼

背景

前段時間無意間看到一篇公眾號 招賢令:一起來搞一個新開源項(xiàng)目,作者介紹他想要做一個開源項(xiàng)目:cprobe 用于整合目前市面上散落在各地的 Exporter,統(tǒng)一進(jìn)行管理。

成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司,提供網(wǎng)站制作、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);可快速的進(jìn)行網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛的網(wǎng)站,是專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來合作!

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

以往的每一個 Exporter 都需要單獨(dú)部署運(yùn)維。

同時又完全兼容 Prometheus 生態(tài),也可以復(fù)用現(xiàn)有的監(jiān)控面板。

恰好這段時間我也在公司從事可觀測性相關(guān)的業(yè)務(wù),發(fā)現(xiàn)這確實(shí)是一個痛點(diǎn)。

于是便一直在關(guān)注這個項(xiàng)目,同時也做了些貢獻(xiàn);因?yàn)樵擁?xiàng)目的核心是用于整合 exporter,所以為其編寫插件也是非常重要的貢獻(xiàn)了。

編寫插件

整個項(xiàng)目執(zhí)行流程圖如下:

可以看到編寫插件最核心的便是自定義插件解析自定義的配置文件、抓取指標(biāo)的邏輯。

比如我們需要在配置中指定抓取目標(biāo)的域名、抓取規(guī)則等。

這里  cprobe 已經(jīng)抽象出了兩個接口,我們只需要做對應(yīng)的實(shí)現(xiàn)即可。

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

這里每個插件的配置都不相同,所以我們需要將配置解析到具體的結(jié)構(gòu)體中。

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  
}

解析配置文件沒啥好說的,根據(jù)自己的邏輯實(shí)現(xiàn)即可,可能會配置一些默認(rèn)值而已。

下面是核心的抓取邏輯,本質(zhì)上就是使用對應(yīng)插件的 Client 獲取一些核心指標(biāo)封裝為 Prometheus 的 Metric,然后由 cprobe 寫入到遠(yuǎn)端的 Prometheus 中(或者是兼容 Prometheus 的數(shù)據(jù)庫中)。

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

所有的指標(biāo)數(shù)據(jù)都是通過對應(yīng)的客戶端獲取。

如果是遷移一個存在的  export 到 cprobe 中時,這些抓取代碼我們都可以直接復(fù)制對應(yīng) repo 中的代碼。

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

除非我們是重新寫一個插件,不然對于一些流行的庫或者是中間件都已經(jīng)有對應(yīng)的 exporter 了。

具體的列表可以參考這里:https://prometheus.io/docs/instrumenting/exporters/

之后便需要在對應(yīng)的插件目錄(./conf.d)創(chuàng)建我們的配置文件:

為了方便測試,可以在啟動 cprobe 時添加 -no-writer 讓指標(biāo)打印在控制臺,從而方便調(diào)試。

總結(jié)

之前就有人問我有沒有畢竟好上手的開源項(xiàng)目,這不就來了嗎?

正好目前項(xiàng)目創(chuàng)建時間不長,代碼和功能也比較簡單,同時還有可觀察系統(tǒng)大佬帶隊(duì),確實(shí)是一個非常適合新手參與的開源項(xiàng)目。

項(xiàng)目地址:

https://github.com/cprobe/cprobe


分享題目:手把手教你為開源項(xiàng)目貢獻(xiàn)代碼
分享網(wǎng)址:http://www.5511xx.com/article/dpjjihg.html