![observability](/marcossilvestrini/learning-observability/raw/main/images/observability.png)
Project for learning about Learning Observability.
Explore the docs »
Project Page
-
Report Bug
-
Request Feature
Table of Contents
這個項目是為了學習可觀察性。
該計畫旨在開始學習可觀察性工具和最佳實踐。
一些學習工具:
- 普羅米修斯
- 警報管理器
- 格拉法納
- 格拉法娜·洛基
- 格拉法納時間
- Linux系統啟動
- 叢集 Kubernetes 啟動
- git
克隆儲存庫
git clone https://github.com/marcossilvestrini/learning-observability.git
cd learning-observability || exit
我發布了一些在此存儲庫中使用的範例。
- 建立儲存庫
- 普羅米修斯
- 警報管理器
- 格拉法納
- 格拉法娜·洛基
- 格拉法納時間
- 格拉法納合金
- 其他工具
請參閱開放式問題取得建議功能(和已知問題)的完整清單。
Prometheus 是一個開源系統監控和警報工具包,最初是在 SoundCloud 建置的。
自2012年推出以來,許多公司和組織都採用了Prometheus,該專案擁有非常活躍的開發者和用戶社群。
Prometheus 生態系統由多個組件組成,其中許多組件是可選的:
- 主要的 Prometheus 伺服器,用於抓取和儲存時間序列數據
- 用於檢測應用程式程式碼的客戶端庫
- 支援短期工作的推送網關
- HAProxy、StatsD、Graphite 等服務的特殊用途導出器。
- 處理警報的警報管理器
- 各種支援工具
有關 Prometheus 的更多資訊請訪問官方文件:
https://prometheus.io/docs/introduction/overview/
指標名稱範例:
<metric name>{<label name>=<label value>, ...}
帶標籤的指標名稱範例:
api_http_requests_total{method="POST", handler="/messages"}
櫃檯– 僅接受並儲存那些隨時間增加的值。
測量– 儲存可以取不同值的值,這些值既可以增加也可以減少。
直方圖– 對觀察結果進行取樣(通常是請求持續時間或回應大小等)並將其計數到可設定的儲存桶中。它還提供所有觀察值的總和,使您可以計算平均值。
概括– 使用附加統計資料(分位數)提供更詳細資料表示的直方圖。
在 Prometheus 術語中,您可以抓取的端點稱為實例,通常對應於單一進程。
具有相同目的的實例的集合,例如為了可擴展性或可靠性而複製的進程,稱為作業。
遠端寫入協定旨在能夠將樣本從發送方即時可靠地傳播到接收方,而不會遺失。
- 「發送者」是發送 Prometheus 遠端寫入資料的東西。
- 「接收器」是接收 Prometheus 遠端寫入資料的東西。
- 「樣本」是一對(時間戳,值)。
- 「標籤」是一對(鍵,值)。
- 「系列」是樣本列表,由一組唯一的標籤標識。
該規範旨在描述以下元件如何交互作用:
- 普羅米修斯(作為“發送者”和“接收者”)
- Avalanche(作為“發送者”)- 負載測試工具 Prometheus Metrics。
- 皮質(作為“接收器”)
- 彈性代理(作為「接收者」)
- Grafana Agent(既作為「發送者」又作為「接收者」)
- GreptimeDB(作為「接收者」)
- InfluxData 的 Telegraf 代理程式。 (作為發送者和接收者)
- M3(作為「接收器」)
- 密米爾(作為「接收者」)
- OpenTelemetry Collector(作為「發送者」並最終作為「接收者」)
- 薩諾斯(作為「接收者」)
- 向量(作為“發送者”和“接收者”)
- VictoriaMetrics(作為「接收者」)
Prometheus 提供了一種名為 PromQL(Prometheus Query Language)的功能查詢語言,可讓使用者即時選擇和聚合時間序列資料。
表達式的結果可以顯示為圖表,在 Prometheus 表達式瀏覽器中以表格資料形式查看,或由外部系統透過 HTTP API 使用。
聯合允許 Prometheus 伺服器從另一個 Prometheus 伺服器抓取選定的時間序列。
分層聯合允許 Prometheus 擴展到具有數十個資料中心和數百萬個節點的環境。
在此用例中,聯合拓撲類似於樹,較高層級的 Prometheus 伺服器從大量從屬伺服器收集聚合時間序列資料。
這意味著我們擁有更大的 Prometheus 伺服器,可以從較小的伺服器收集時間序列資料。我們採用自上而下的方法,從不同層級收集資料。
此方法涉及一個 Prometheus 伺服器監視特定服務或一組服務,從監視另一組不同服務的另一台伺服器收集特定的時間序列資料。
例如,執行多個服務的叢集調度程序可能會公開有關叢集上執行的服務執行個體的資源使用資訊(例如記憶體和 CPU 使用情況)。
另一方面,在該叢集上運行的服務將僅公開特定於應用程式的服務指標。
通常,這兩組指標是由單獨的 Prometheus 伺服器抓取的。使用聯合,包含服務等級指標的 Prometheus 伺服器可以從叢集 Prometheus 中提取有關其特定服務的叢集資源使用指標,以便兩組指標都可以在該伺服器內使用。
透過這樣做,我們可以對來自兩台伺服器的合併資料執行查詢和警報。
Prometheus 提供了通用的 HTTP 服務發現,使其能夠透過 HTTP 端點發現目標。
HTTP 服務發現是支援的服務發現機制的補充,並且是基於檔案的服務發現的替代方案。
- static_configs 無法擴展到頻繁新增/刪除實例的更動態的環境
- Prometheus 可以與服務發現機制集成,以自動更新其正在運行的實例的視圖
- 當新增實例時,Prometheus 將開始抓取,當發現遺失時,時間序列也將被刪除
- 與 Consul、Azure、AWS 或基於文件的內建整合(如果需要自訂機制)
- JSON/YAML 檔案可以由平台發布,指定要從中抓取的所有目標。 Prometheus 使用它來自動更新目標
prometheus.yaml 用於廢棄目標 http_sd.json 中的服務
global:
scrape_interval: 15s
evaluation_interval: 15s
scrape_configs:
# Service Discovery with file_sd
- job_name: 'http_sd'
basic_auth:
username: "skynet"
password: "prometheus"
file_sd_configs:
- files:
- /home/vagrant/prometheus-server/http_sd.json
http_sd.json
[
{
"targets": ["192.168.0.130:9100", "192.168.0.131:9100"],
"labels": {
"__meta_prometheus_job": "node"
}
},
{
"targets": ["192.168.0.130:9091"],
"labels": {
"__meta_prometheus_job": "pushgateway"
}
}
]
# Download files - https://prometheus.io/download/
wget https://github.com/prometheus/prometheus/releases/download/v2.51.2/prometheus-2.51.2.linux-amd64.tar.gz
# Extract files
tar xvfz prometheus-*.tar.gz
rm prometheus-*.tar.gz
cd prometheus-*
# Check version
./prometheus --version
看我的設定檔普羅米修斯.yaml
vim prometheus.yaml
# my global config
global:
scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
# scrape_timeout is set to the global default (10s).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# Start
./prometheus --config.file=prometheus.yml
# Start with PM2 - npm install pm2@latest -g
pm2 start prometheus --name prometheus-server -- --config.file=prometheus.yml
http://localhost:9090 # all endpoints
http://localhost:9090/graph # PromQL expressions
http://localhost:9090/metrics # metrics
http://localhost:9090/targets # scrape_configs jobs
您可以在表格或圖表模式下使用表達式。
開啟頁面http://localhost:9090
# Check all http metrics
promhttp_metric_handler_requests_total
# Check http metrics with http status code 200
promhttp_metric_handler_requests_total{code="200"}
# Count http metrics
count(promhttp_metric_handler_requests_total)
# Rate function
rate(promhttp_metric_handler_requests_total{code="200"}[1m])
導出器是與您要從中獲取指標的應用程式一起運行的二進位檔案。
導出器通常會透過將以非 Prometheus 格式公開的指標轉換為 Prometheus 支援的格式來公開 Prometheus 指標。
Prometheus Node Exporter 公開了各種與硬體和核心相關的指標。
# Download - https://prometheus.io/download#node_exporter
wget https://github.com/prometheus/node_exporter/releases/download/v1.7.0/node_exporter-1.7.0.linux-amd64.tar.gz
# Extract
tar xvfz node_exporter-*.*-amd64.tar.gz
cd node_exporter-*.*-amd64
# Start
./node_exporter
# Start with PM2 - npm install pm2@latest -g
pm2 start node_exporter --name node_exporter
# Access metrics
http://localhost:9100/metrics
若要為節點匯出器啟用廢料,您可以設定 prometheus。
# Edit prometheus file and add job node
vim prometheus.yaml
...
scrape_configs:
- job_name: node
static_configs:
- targets: ['localhost:9100']
...
重新啟動普羅米修斯服務以申請新工作。
Prometheus Pushgateway 是一項中間服務,允許臨時作業和批次作業將其指標公開給 Prometheus。
由於此類工作可能存在的時間不夠長而無法刪除,因此他們可以將其指標推送到 Pushgateway。
然後,Pushgateway 充當 Prometheus 抓取的臨時指標儲存。
此設定對於擷取不連續執行的作業的結果特別有用,例如 CI 系統中的批次作業或在排程時間執行的備份腳本。
它簡化了對此類作業的監控,而無需運行可能比作業本身壽命長的長期 Prometheus 實例。
# Download
wget -q https://github.com/prometheus/pushgateway/releases/download/v1.8.0/pushgateway-1.8.0.linux-amd64.tar.gz
# Extract
tar xvfz pushgateway-*.*-amd64.tar.gz
cd pushgateway-*.*-amd64
# Start
# Start with PM2 - npm install pm2@latest -g
pm2 start pushgateway --name pushgateway -- --web.listen-address "192.168.0.130:9091"
# Edit prometheus file and add job pushgateway
vim prometheus.yaml
scrape_configs:
- job_name: 'pushgateway'
honor_labels: true
static_configs:
- targets: ['192.168.0.130:9091'] # prometheus server for scraping
# Restart prometheus
# restart with pm2
pm2 restart prometheus-server
echo 'training_completion{course="CKA", status="complete"} 1' > metrics.txt
echo 'training_completion{course="CKS", status="in_progress"} 0.5' >> metrics.txt
echo 'training_completion{course="LPIC2", status="not_started"} 0' >> metrics.txt
curl --data-binary @metrics.txt http://192.168.0.130:9091/metrics/job/training_metrics
# Access metrics
http://localhost:9091
僅在沒有基本身份驗證的情況下工作
echo "Downloading Promlens..."
wget -q https://github.com/prometheus/promlens/releases/download/v0.3.0/promlens-0.3.0.linux-amd64.tar.gz
echo "Extracting Promlens..."
tar xvfz promlens-*.*-amd64.tar.gz
rm promlens-*.*-amd64.tar.gz
cd promlens-*.*-amd64 || exit
echo "Starting Promlens..."
pm2 start promlens --name promlens -- --web.listen-address "192.168.0.130:8081"
cd || exit
# Access query builder
http://192.168.0.130:8081
關於Alertmanager的更多資訊請參閱官方文件:
https://github.com/prometheus/alertmanager
貢獻使開源社群成為學習、啟發和創造的絕佳場所。您所做的任何貢獻都是不勝感激.
如果您有更好的建議,請分叉該儲存庫並建立拉取請求。您也可以簡單地使用標籤“增強”來開啟問題。 不要忘記給該項目一顆星!再次感謝!
- 分叉項目
- 建立您的功能分支(
git checkout -b feature/AmazingFeature
) - 提交您的更改(
git commit -m 'Add some AmazingFeature'
) - 推送到分支(
git push origin feature/AmazingFeature
) - 打開拉取請求
根據 MIT 許可證分發。看LICENSE
了解更多。
- 馬科斯·西爾維斯特里尼 -@西爾維斯特里尼先生
- [email protected]
項目連結:https://github.com/marcossilvestrini/learning-observability