1. 程式人生 > 其它 >Prometheus自研採集器(python)

Prometheus自研採集器(python)

1 背景

當Prometheus自帶的exporter無法滿足實際需求時,需要我們自定義開發採集器,本篇文章主要介紹通過python開發自定義的exporter

2 環境準備

本文介紹的採集器用python語言編寫,利用docker部署

  • python版本

    版本要求為3.x,本文指令碼例子python為3.8

  • python依賴包

    # prometheus所需依賴包
    from prometheus_client import Gauge, start_http_server, Counter
    from prometheus_client.core import CollectorRegistry
    import prometheus_client
    
    # api介面所需依賴包
    import uvicorn
    from fastapi import FastAPI
    from fastapi.responses import PlainTextResponse
    
  • docker版本

    20.10.8

3 採集器開發部署

3.1 exporter開發

定義Prometheus的metrics,這裡主要介紹最常用的兩種metrics

  • Gauge

    第一種是最常用的metrics——Gauge,Gauge可任意賦值,代表是當前採集的值

    example_G = Gauge("this is a metric name", "this is a metric describe", ["label1", "label2", "label3"], registry=REGISTRY) # 指標名稱,描述,標籤名稱

  • Counter

    第二種Counter是一直累計增加的計數器,不能減少。一般用來記錄訪問總次數等

    example_C = Gauge("this is a metric name", "this is a metric describe", ["label1", "label2", "label3"], registry=REGISTRY) # 指標名稱,描述,標籤名稱

本文例子exporter檔名為test_exporter.py,具體指令碼例子如下:

#!/usr/bin/python3
# coding:utf-8
from prometheus_client import Gauge, start_http_server, Counter
from prometheus_client.core import CollectorRegistry
import prometheus_client
import uvicorn
from fastapi import FastAPI
from fastapi.responses import PlainTextResponse

# 定義api物件
app = FastAPI()

# 設定介面訪問路徑/metrics
@app.get('/metrics', response_class=PlainTextResponse)
def get_data():
    '''
    該函式為採集資料函式,該例子模擬採集到資料標籤label1、label2和label3,資料data1
    '''
    # 定義client_python裡提供的prometheus Gauge資料型別
    REGISTRY = CollectorRegistry(auto_describe=False)
    example_G = Gauge("this_is_a_metric_name", "this is a metric describe", ["label1", "label2", "label3"], registry=REGISTRY)
    
    label1 = '111'
    label2 = '222'
    label3 = '333'
    data1 = '444'
    # 呼叫Gauge資料型別,把採集的資料放入到設定的example_G
    example_G.labels(label1,label2,label3).set(data1)
    return prometheus_client.generate_latest(REGISTRY)
 
# 用uvicorn呼叫介面,啟用埠為9330
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=9330, log_level="info")

在終端執行pip freeze > requirements.txt生成依賴包清單

3.2 docker部署

把寫好的指令碼用docker打包
在./test目錄建立一個app目錄存放指令碼test_exporter.py
把生成的requirements.txt放到./test目錄下
在./test目錄編寫Dockerfile

FROM python:3.9-alpine

RUN mkdir -p /test/app
COPY ./requirements.txt /app/requirements.txt
COPY ./app /test/app

WORKDIR /app
RUN pip install -r requirements.txt

CMD ["python", "/test/app/test_exporter.py"]

建立映象 docker image build -t test_exporter:v0.1 .
建立後可通過 docker images 檢視生成的docker映象

執行exporter docker run -it -d --name test_exporter -p 9330:9330 test_exporter:v0.1
執行後可以通過 docker ps 檢視容器啟動情況,docker logs (容器ID) 檢視容器日誌

採集器啟用後可以通過呼叫介面檢視採集資料 http://localhost:9330/metrics

4 配置採集任務

修改prometheus.yml配置檔案,新增採集任務(新增後需要重啟Prometheus)

scrape_configs:
  - job_name: "test_exporter"
    scrape_interval: 30s  # 採集頻率為30s抓取一次
    static_configs:
    - targets:  # 配置採集任務
      - 127.0.0.1:9330

在Prometheus官方Graph中通過採集metric名檢視採集資料

原創宣告
本文原創自作者:liu_kx,轉載請註明原文連結

本文來自部落格園,作者:liu_kx,轉載請註明原文連結:https://www.cnblogs.com/liukx/p/15549688.html