偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

手把手教你為開源項目貢獻代碼

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

背景

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

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

以往的每一個 Exporter 都需要單獨部署運維。

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

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

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

編寫插件

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

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

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

這里  cprobe 已經(jīng)抽象出了兩個接口,我們只需要做對應的實現(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ù)自己的邏輯實現(xiàn)即可,可能會配置一些默認值而已。

下面是核心的抓取邏輯,本質(zhì)上就是使用對應插件的 Client 獲取一些核心指標封裝為 Prometheus 的 Metric,然后由 cprobe 寫入到遠端的 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)  
}

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

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

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

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

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

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

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

總結(jié)

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

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

項目地址:

https://github.com/cprobe/cprobe

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

2021-05-27 11:10:42

Python開源包代碼

2021-09-26 16:08:23

CC++clang_forma

2025-05-07 00:31:30

2011-01-10 14:41:26

2011-05-03 15:59:00

黑盒打印機

2021-07-14 09:00:00

JavaFX開發(fā)應用

2017-09-05 13:01:11

CocoaPods開源庫GitHub

2021-02-26 11:54:38

MyBatis 插件接口

2011-02-22 13:46:27

微軟SQL.NET

2021-12-28 08:38:26

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

2022-12-07 08:42:35

2023-04-26 12:46:43

DockerSpringKubernetes

2022-07-27 08:16:22

搜索引擎Lucene

2022-03-14 14:47:21

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

2022-01-08 20:04:20

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

2021-11-24 16:02:57

鴻蒙HarmonyOS應用

2020-08-12 09:07:53

Python開發(fā)爬蟲

2020-04-14 10:20:12

MySQL數(shù)據(jù)庫死鎖

2009-11-09 14:57:37

WCF上傳文件

2021-08-04 08:55:02

Socket Java開發(fā)
點贊
收藏

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