自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

手把手教你為開源項(xiàng)目貢獻(xiàn)代碼

開源
以往的每一個(gè) Exporter 都需要單獨(dú)部署運(yùn)維。同時(shí)又完全兼容 Prometheus 生態(tài),也可以復(fù)用現(xiàn)有的監(jiān)控面板。恰好這段時(shí)間我也在公司從事可觀測(cè)性相關(guān)的業(yè)務(wù),發(fā)現(xiàn)這確實(shí)是一個(gè)痛點(diǎn)。于是便一直在關(guān)注這個(gè)項(xiàng)目,同時(shí)也做了些貢獻(xiàn);因?yàn)樵擁?xiàng)目的核心是用于整合 exporter,所以為其編寫插件也是非常重要的貢獻(xiàn)了。

背景

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

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

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

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

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

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

編寫插件

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

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

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

這里  cprobe 已經(jīng)抽象出了兩個(gè)接口,我們只需要做對(duì)應(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

這里每個(gè)插件的配置都不相同,所以我們需要將配置解析到具體的結(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)即可,可能會(huì)配置一些默認(rèn)值而已。

下面是核心的抓取邏輯,本質(zhì)上就是使用對(duì)應(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ù)都是通過對(duì)應(yīng)的客戶端獲取。

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

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

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

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

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

為了方便測(cè)試,可以在啟動(dòng) cprobe 時(shí)添加 -no-writer 讓指標(biāo)打印在控制臺(tái),從而方便調(diào)試。

總結(jié)

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

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

項(xiàng)目地址:

https://github.com/cprobe/cprobe

責(zé)任編輯:姜華 來源: crossoverJie
相關(guān)推薦

2021-05-27 11:10:42

Python開源包代碼

2021-09-26 16:08:23

CC++clang_forma

2021-07-14 09:00:00

JavaFX開發(fā)應(yīng)用

2011-01-10 14:41:26

2011-05-03 15:59:00

黑盒打印機(jī)

2017-09-05 13:01:11

CocoaPods開源庫GitHub

2022-01-08 20:04:20

攔截系統(tǒng)調(diào)用

2022-03-14 14:47:21

HarmonyOS操作系統(tǒng)鴻蒙

2023-04-26 12:46:43

DockerSpringKubernetes

2022-12-07 08:42:35

2022-07-27 08:16:22

搜索引擎Lucene

2020-08-12 09:07:53

Python開發(fā)爬蟲

2011-02-22 13:46:27

微軟SQL.NET

2021-12-28 08:38:26

Linux 中斷喚醒系統(tǒng)Linux 系統(tǒng)

2021-02-26 11:54:38

MyBatis 插件接口

2021-11-24 16:02:57

鴻蒙HarmonyOS應(yīng)用

2010-04-29 09:49:26

代碼提示SQL Server

2017-07-07 11:01:04

Spark性能調(diào)優(yōu)

2020-08-12 07:41:39

SQL 優(yōu)化語句

2021-07-01 09:31:50

MySQL SQL 語句數(shù)據(jù)庫
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)