prometheus-client

Prometheus Python Client

https://prometheus.io/

普羅米修斯-客戶 ·PyPI

用于服務(wù)器和容器等生產(chǎn)信息監(jiān)控。

1. Install the client:

pip install prometheus-client

2. 運行該py,得到相關(guān)的信息

from prometheus_client import start_http_server, Summary

import random

import time

# Create a metric to track time spent and requests made.

REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')

# Decorate function with metric.

@REQUEST_TIME.time()

def process_request(t):

?? """A dummy function that takes some time."""

?? time.sleep(t)

if __name__ == '__main__':

?? # Start up the server to expose the metrics.

?? start_http_server(8000)

?? # Generate some requests.

?? while True:

? ? ?? process_request(random.random())


啟動服務(wù)IP

訪問http://localhost:8000/以查看指標(biāo)。


3. 提供4種類型指標(biāo)

Counter, Gauge, Summary and Histogram(計數(shù)器、儀表、摘要和直方圖)

counter

一直累加的計數(shù)器,不可以減少。counter適合用來記錄訪問總次數(shù),累加值

定義它需要2個參數(shù),第一個是metrics的名字,第二個是metrics的描述信息:

gauge

可增可減,可以任意設(shè)置,就代表了某個指標(biāo)當(dāng)前的值而已。

histogram

用來統(tǒng)計百分位的

summary

分桶和分桶計數(shù)

python客戶端沒有完整實現(xiàn)summary算法

from prometheus_client import Counter

c = Counter('my_failures', 'Description of counter')

c.inc() ? ? # Increment by 1

c.inc(1.6)? # Increment by given value

# -*- coding: utf-8 -*-

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

import time

import random

ProcessCollector().collect()


# 定義4種metrics例子

c = Counter("cc", 'A counter')

g = Gauge('gg', 'A gauge')

h = Histogram('hh', 'A histogram', buckets=(-5, 0, 5))

s = Summary('ss', 'A summary', ['label1', 'label2'])? # metrics名字, metrics說明, metrics支持的label

# 在線程中啟動http服務(wù), 供metrics抓取

start_http_server(8880)

while True:

?? # counter:? 只增不減

?? c.inc(1000)

?? # gauge: 任意值

?? g.set(random.random())

?? # histogram: 任意值, 會給符合條件的bucket增加1次計數(shù)

?? h.observe(random.randint(-10, 10))

?? # summary:任意值, python client不支持summary的百分位統(tǒng)計, 其他語言的client也許支持, 但一般不建議用, 性能和場景都有局限

?? s.labels('a', 'b').observe(17)

?? time.sleep(1)


4進程收集

1.如何使用

Python 客戶端會自動導(dǎo)出有關(guān)進程 CPU 使用率、RAM、文件描述符和開始時間的指標(biāo)。命名空間和 pid 構(gòu)造函數(shù)參數(shù)允許導(dǎo)出有關(guān)其他進程的指標(biāo)

ProcessCollector(namespace='system', pid=1)

eg:

ProcessCollector(namespace='supervisord')

2.返回進程的參數(shù)信息

supervisord_process_virtual_memory_bytes 3.38071552e+08虛擬內(nèi)存大?。ㄒ宰止?jié)為單位)

supervisord_process_resident_memory_bytes 1.4118912e+07駐留內(nèi)存大?。ㄒ宰止?jié)為單位)

supervisord_process_start_time_seconds 1.64171996791e+09進程的開始時間(以秒為單位)

supervisord_process_cpu_seconds_total 0.09999999999999999用戶和系統(tǒng) CPU 總時間(以秒為單位)。

supervisord_process_open_fds 7.0打開文件描述符的數(shù)量。

supervisord_process_max_fds 1024.0打開文件描述符的最大數(shù)量。

5.擴展,思考

from flask import Flask

from werkzeug.middleware.dispatcher import DispatcherMiddleware

from prometheus_client import make_wsgi_app

# Create my app

app = Flask(__name__)

# Add prometheus wsgi middleware to route /metrics requests

app.wsgi_app = DispatcherMiddleware(app.wsgi_app, {

?? '/metrics': make_wsgi_app()

})

文檔中有寫到flask和promethus的集成,

在利用fastapi的繼承者發(fā)展,發(fā)現(xiàn)目前暫無方法可利用

如利用,只能將數(shù)值傳入,并編寫相關(guān)的參數(shù)信息。

6流量

MultiProcessCollector

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容