prometheus學習系列十一: Prometheus 采集器的編寫


在前面的文章已經寫了官方的幾個exporter的使用了。 在實際使用環境中,我們可能需要收集一些自定義的數據, 這個時候我們一般是需要自己編寫采集器的。

快速入門編寫一個入門的demo

編寫代碼

from prometheus_client import Counter, Gauge, Summary, Histogram, start_http_server

# need install prometheus_client

if __name__ == '__main__':
    c = Counter('cc', 'A counter')
    c.inc()

    g = Gauge('gg', 'A gauge')
    g.set(17)

    s = Summary('ss', 'A summary', ['a', 'b'])
    s.labels('c', 'd').observe(17)

    h = Histogram('hh', 'A histogram')
    h.observe(.6)

    start_http_server(8000)
    import time

    while True:
        time.sleep(1)

只需要一個py文件, 運行起來, 會監聽在8000端口,訪問127.0.0.1:8000端口。

效果圖

 其實一個導出器就已經寫好了, 就是這么簡單的,我們只需要在prometheus配置來采集對應的導出器就可以了。 不過我們的導出的數據都是沒有實際意義了。

數據類型介紹

Counter 累加類型, 只能上升,比如記錄http請求的總數或者網絡的收發包累計值。

Gauge: 儀表盤類型, 適合有上升有下降的, 一般網絡流量,磁盤讀寫這些,會有波動和變化的采用這個數據類型。

Summary:  基於采樣的,在服務端完成統計。我們在統計平均值的時候,可能以為某個值異常導致計算平均值不能准確反映實際值, 就需要特定的點位置。

Histogram: 基於采樣的,在客戶端完成統計。我們在統計平均值的時候,可能以為某個值異常導致計算平均值不能准確反映實際值, 就需要特定的點位置。

 

采集內存使用數據

編寫采集類代碼

from prometheus_client.core import GaugeMetricFamily, REGISTRY
from prometheus_client import start_http_server
import psutil


class CustomMemoryUsaggeCollector():
    def format_metric_name(self):
        return 'custom_memory_'

    def collect(self):
        vm = psutil.virtual_memory()
        #sub_metric_list = ["free", "available", "buffers", "cached", "used", "total"]
        sub_metric_list = ["free", "available", "used", "total"]
        for sub_metric in sub_metric_list:
            gauge = GaugeMetricFamily(self.format_metric_name() + sub_metric, '')
            gauge.add_metric(labels=[], value=getattr(vm, sub_metric))
            yield gauge


if __name__ == "__main__":
    collector = CustomMemoryUsaggeCollector()
    REGISTRY.register(collector)
    start_http_server(8001)
    import time
    while True:
        time.sleep(1)

暴露數據情況

部署代碼和集成prometheus

# 准備python3 環境 參考: https://virtualenvwrapper.readthedocs.io/en/latest/
yum install python36 -y

 pip3 install virtualenvwrapper
vim /usr/local/bin/virtualenvwrapper.sh 
# 文件最前面添加如下行
# Locate the global Python where virtualenvwrapper is installed.
VIRTUALENVWRAPPER_PYTHON="/usr/bin/python3"

# 文件生效
source /usr/local/bin/virtualenvwrapper.sh
# 配置workon
[root@node01 ~]# echo "export WORKON_HOME=~/Envs" >>~/.bashrc 

[root@node01 ~]# mkvirtualenv  custom_memory_exporter
(custom_memory_exporter) [root@node01 ~]# pip install prometheus_client psutil
yum install python36-devel

(custom_memory_exporter) [root@node01 ~]# chmod a+x custom_memory_exporter.py 
(custom_memory_exporter) [root@node01 ~]# ./custom_memory_exporter.py 
# 測試是否有結果數據
[root@node00 ~]# curl http://192.168.100.11:8001/

prometheus.yml 加入如下片段
  - job_name: "custom-memory-exporter"
    static_configs:
    - targets: ["192.168.100.11:8001"]

[root@node00 prometheus]# systemctl restart prometheus
[root@node00 prometheus]# systemctl status prometheu

查詢效果圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM