Cài đặt
pip install prometheus-client
Ví dụ
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())
Truy cập http://localhost:8000/ để xem metrics.
Trong đó:
request_processing_seconds_count
: Số lần function đc gọirequest_processing_seconds_sum
: Tổng thời gian cho function
metrics type
counter:
- metric là bộ đêm tăng đơn điệu mà giá trị chỉ có thể tăng hoặc được đặt về 0 khi restart, thường được dùng để biểu thị thành công hoặc lỗi.
- method:
inc()
: tăng counter lên 1inc(double v)
: tăng counter lên v > 0
- counter phải bắt đầu từ 0
gauge:
- metric đại diện cho 1 giá trị số duy nhất có thể lên xuống tùy ý, thường đc sử dụng cho các giá trị đo được như nhiệt độ, bộ nhớ.
- method:
inc()
inc(double v)
dec()
dec(double v)
set(double v)
- gauge phải bắt đầu từ 0
Histogram Summary
Counter
Counters go up, and reset when the process restarts.
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
Gauge
Gauges can go up and down.
from prometheus_client import Gauge
g = Gauge('my_inprogress_requests', 'Description of gauge')
g.inc() # Increment by 1
g.dec(10) # Decrement by given value
g.set(4.2) # Set to a given value
exporting metrics
HTTP
start a HTTP server in a daemon thread:
from prometheus_client import start_http_server
start_http_server(8000)
Flask
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()
})
Run
# Install uwsgi if you do not have it
pip install uwsgi
uwsgi --http 127.0.0.1:8000 --wsgi-file myapp.py --callable app
Custom Collectors
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, REGISTRY
class CustomCollector(object):
def collect(self):
yield GaugeMetricFamily('my_gauge', 'Help text', value=7)
c = CounterMetricFamily('my_counter_total', 'Help text', labels=['foo'])
c.add_metric(['bar'], 1.7)
c.add_metric(['baz'], 3.8)
yield c
REGISTRY.register(CustomCollector())
Ref: https://github.com/prometheus/client_python https://stackoverflow.com/questions/69175661/configure-exporter-python-prometheus-client-with-multiple-targets