Rust語言之Prometheus系統(tǒng)監(jiān)控工具包的使用詳解
1. Prometheus 簡介
Prometheus 是一個開源的系統(tǒng)監(jiān)控和警報工具包,最初是由 SoundCloud 構建的。Prometheus 的主要優(yōu)勢在于其多維數據模型和靈活的查詢語言,能夠實現(xiàn)高效的數據存儲和查詢。
隨著時間的發(fā)展,Prometheus已經具有適用于各種操作系統(tǒng)、各種使用場景的版本。為了開發(fā)者方便開發(fā),更是有各種語言版本的Prometheus的開發(fā)工具包(本文主要介紹Rust版本的Prometheus開發(fā)工具包)
本文章具體介紹兩個部分的使用:
Exporter:用于產生和發(fā)送信息
Prometheus:用于接收和展示信息
1.1 Exporter
在整個Prometheus 的使用中,Exporter是最基礎和重要的部分,Prometheus只是負責接收并展示你想知道的信息,但是信息究竟從何而來呢?是的,消息由Exporter產生,并發(fā)送給Prometheus保存和記錄。
廣義上講所有可以向Prometheus提供監(jiān)控樣本數據的程序都可以被稱為一個Exporter。而Exporter的一個實例稱為target,Prometheus通過輪詢的方式定期從這些target中獲取樣本數據。也就是說可以同時有多個Exporter給同一個Prometheus發(fā)送需要記錄的信息。
其實對于Prometheus工具包的使用具體也在這一部分,通過工具包創(chuàng)建一個又一個Exporter從而完整的記錄你想記錄的任何信息,那問題來了Exporter結構應該是怎樣的呢?究竟是使用工具包提供的api寫在自己的程序里面,還是完全隔離開,寫一個獨立的Exporter程序呢?
從Exporter的運行方式上來講,又可以分為:
- 獨立使用的
以Node Exporter為例,由于操作系統(tǒng)本身并不直接支持Prometheus,同時用戶也無法通過直接從操作系統(tǒng)層面上提供對Prometheus的支持。因此,用戶只能通過獨立運行一個程序的方式,通過操作系統(tǒng)提供的相關接口,將系統(tǒng)的運行狀態(tài)數據轉換為可供Prometheus讀取的監(jiān)控數據。 除了Node Exporter以外,比如MySQL Exporter、Redis Exporter等都是通過這種方式實現(xiàn)的。 這些Exporter程序扮演了一個中間代理人的角色。
- 集成到應用中的
為了能夠更好的監(jiān)控系統(tǒng)的內部運行狀態(tài),有些開源項目如Kubernetes,ETCD等直接在代碼中使用了Prometheus的Client Library,提供了對Prometheus的直接支持。這種方式打破的監(jiān)控的界限,讓應用程序可以直接將內部的運行狀態(tài)暴露給Prometheus,適合于一些需要更多自定義監(jiān)控指標需求的項目。
1.2 安裝 Prometheus
Prometheus主要用來顯示從Exporter發(fā)送的信息,并且Prometheus可以在本地運行,只需下載對應的版本。
https://prometheus.io/download/
按照安裝指南完成安裝與配置。
我下載的是Linux,64位系統(tǒng)的版本:prometheus-2.47.0.linux-amd64.tar.gz
輸入指令解壓文件
tar -zxvf prometheus-2.47.0.linux-amd64.tar.gz
進入 Prometheus 目錄
cd prometheus-2.47.0.linux-amd64
運行 Prometheus
./prometheus
現(xiàn)在 Prometheus 應該已經開始運行,并在默認的 9090 端口上監(jiān)聽。你可以在 Web 瀏覽器中通過訪問 http://<你的服務器IP>:9090 來訪問 Prometheus 的 Web 界面。
配置 Prometheus
Prometheus 通常還需要一個配置文件,例如 prometheus.yml,來定義如何執(zhí)行服務發(fā)現(xiàn)和數據采集。在 Prometheus 目錄中,你通常會找到一個示例配置文件,你可以根據自己的需要對其進行修改。
2.Prometheus 庫的基本使用
2.1 創(chuàng)建新的 Rust 項目
使用 Cargo 創(chuàng)建一個新項目:
cargo new prometheus_example cd prometheus_example
配置項目依賴,在 Cargo.toml 文件中加入 Prometheus 庫:
[dependencies] prometheus = "0.13.3"
0.13.3是寫這篇文章時的最新版本。也許現(xiàn)在版本已經更新,建議使用最新的版本哦
https://docs.rs/prometheus/0.13.3/prometheus/
2.2 創(chuàng)建和配置 Prometheus 計數器
簡單的 初始化 一個 計數器,并配置標簽和值:
use prometheus::{Counter, Registry};
let reg = Registry::new();
let counter = Counter::new("example_counter", "An example counter").unwrap();
reg.register(Box::new(counter.clone())).unwrap();也可以通過官方的的例子自定義Registry實例和counter實例
// 使用 `lazy_static!` 定義靜態(tài) `IntCounter`。
lazy_static! {
static ref CUSTOM_COUNTER: IntCounter = IntCounter::new("custom", "dedicated counter").unwrap();
}
// Register custom metrics to a custom registry.
let mut labels = HashMap::new();
//這部分代碼創(chuàng)建了一個帶有自定義前綴“myprefix”的 `Registry`,并為它分配了一個標簽,標簽的鍵是“mykey”,值是“myvalue”。
labels.insert("mykey".to_string(), "myvalue".to_string());
let custom_registry = Registry::new_custom(Some("myprefix".to_string()),Some(labels)).unwrap();
//這一行調用了下面定義的 `custom_metrics` 函數,并將先前創(chuàng)建的 `custom_registry` 作為參數傳遞給它。
custom_metrics(&custom_registry);
/// Custom metrics, to be collected by a dedicated registry.
fn custom_metrics(registry: &Registry) {
registry.register(Box::new(CUSTOM_COUNTER.clone())).unwrap();
CUSTOM_COUNTER.inc_by(42);
assert_eq!(CUSTOM_COUNTER.get(), 42);
}
//在這個函數中:
// `CUSTOM_COUNTER` 被注冊到了傳遞進來的 `Registry` 中。
// 然后,`CUSTOM_COUNTER` 的值增加了42。
// 最后,使用 `assert_eq!` 宏來驗證 `CUSTOM_COUNTER` 的值是否確實為42。通過上面的方式生成的metrics可以打印出來
// Print metrics for the custom registry.
let mut buffer = Vec::<u8>::new();
let encoder = prometheus::TextEncoder::new();
encoder
.encode(&custom_registry.gather(), &mut buffer)
.unwrap();
println!("## Custom registry");
println!("{}", String::from_utf8(buffer.clone()).unwrap());打印結果:
## Custom registry # HELP myprefix_custom dedicated counter # TYPE myprefix_custom counter myprefix_custom 42
3. 數據收集和展示
3.1通過web服務提供查詢數據的接口給 Prometheus
因為 Prometheus 主要是通過 HTTP 協(xié)議來抓取(scrape)應用暴露出的指標(metrics)的,但這并不意味著你的應用必須是一個完整的 web 服務。你只需要為你的應用添加一個簡單的 HTTP 服務器來暴露指標。這樣的 HTTP 服務器通常非常輕量,對你的應用幾乎沒有什么影響。
可以使用warp的輕量級web框架,也可以使用最近剛推出的評價還不錯的一個web框架,當然選用什么框架都看個人習慣。
我將用warp舉例 在Cargo.toml文件中添加依賴
[dependencies]
warp = "0.3"
tokio = { version = "1", features = ["full"] }接下來,用warp創(chuàng)建一個簡單的web服務:這個服務會在 http://[127:0:0:1]:8080/test 上暴露你的 Prometheus 指標:
use prometheus::{Encoder, TextEncoder};
use warp::Filter;
use std::collections::HashMap;
use prometheus::{Registry, IntCounter};
use std::sync::Arc;
use tokio::sync::RwLock;
lazy_static! {
static ref CUSTOM_COUNTER: IntCounter = IntCounter::new("custom", "dedicated counter").unwrap();
#[tokio::main]
async fn main() {
let mut labels = HashMap::new();
labels.insert("mykey".to_string(), "myvalue".to_string());
let custom_registry = Registry::new_custom(Some("myprefix".to_string()),Some(labels)).unwrap();
custom_metrics(&custom_registry);
fn custom_metrics(registry: &Registry) {
registry.register(Box::new(CUSTOM_COUNTER.clone())).unwrap();
CUSTOM_COUNTER.inc_by(42);
assert_eq!(CUSTOM_COUNTER.get(), 42);
}
let custom_registry = Arc::new(RwLock::new(custom_registry));
// 創(chuàng)建一個 warp filter,該 filter 會暴露 Prometheus 指標。
let consensus_route = warp::path!("test").map(move || {
GET_BLOCK_COUNTER.inc();
SEND_TX_COUNTER.inc();
let buffer = metrics_encode(Consensus_registry.clone());
warp::reply::with_header(buffer, "Content-Type", "text/plain")
});
let routes=rpc_route.or(consensus_route);
warp::serve(routes).run(([127, 0, 0, 1], 8080)).await;在配置文件中添加剛剛寫的web服務的網址,使打開prometheus可以定時訪問這個地址從而獲取信息。 文件編輯好后,保存,退出,重新運行prometheus就會去監(jiān)控這個地址了。
3.2. 監(jiān)控信息
打開瀏覽器,訪問localhost:9090地址,會出現(xiàn)prometheus的監(jiān)控畫面

在搜索欄中輸入想要監(jiān)控的信息:myprefix_custom,點擊Execute,便可以看到想要查詢的信息,數量等
以上就是Rust語言之Prometheus系統(tǒng)監(jiān)控工具包的使用詳解的詳細內容,更多關于Rust Prometheus工具包使用的資料請關注腳本之家其它相關文章!
相關文章
vscode搭建rust開發(fā)環(huán)境的圖文教程
本文主要介紹了vscode搭建rust開發(fā)環(huán)境的圖文教程,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-08-08
Rust語言從入門到精通系列之Iterator迭代器深入詳解
這篇文章主要為大家介紹了Rust語言從入門到精通系列之Iterator迭代器深入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04

