playing with metrics
Introduction
pytheus is a modern python library for collecting prometheus metrics built with multiprocessing in mind.
Some of the features are:
- multiple multiprocess support:
- redis backend โ
- Rust powered backend ๐งช
- bring your own โ
- support for default labels value โ
- partial labels value (built in an incremental way) โ
- customizable registry support โ
- registry prefix support โ
Philosophy
Simply put is to let you work with metrics the way you want.
Be extremely flexible, allow for customization from the user for anything they might want to do without having to resort to hacks and most importantly offer the same api for single & multi process scenarios, the switch should be as easy as loading a different backend without having to change anything in the code.
- What you see is what you get.
- No differences between
singleprocess
&multiprocess
, the only change is loading a different backend and everything will work out of the box. - High flexibility with an high degree of
labels
control and customization.
Requirements
- Python 3.8+
- redis >= 4.0.0 (optional: for multiprocessing)
- pytheus-backend-rs (optional: for Rust powered multiprocessing ๐ฆ)
Installation
Optionally if you want to use the Redis backend (for multiprocess support) you will need the redis library:
If you want to try the Rust based backend (for multiprocess support):
Example
import time
from flask import Flask, Response
from pytheus.metrics import Histogram
from pytheus.exposition import generate_metrics, PROMETHEUS_CONTENT_TYPE
app = Flask(__name__)
http_request_duration_seconds = Histogram(
'http_request_duration_seconds', 'documenting the metric..'
)
@app.route('/metrics')
def metrics():
data = generate_metrics()
return Response(data, headers={'Content-Type': PROMETHEUS_CONTENT_TYPE})
# track time with the context manager
@app.route('/')
def home():
with http_request_duration_seconds.time():
return 'hello world!'
# alternatively you can also track time with the decorator shortcut
@app.route('/slow')
@http_request_duration_seconds
def slow():
time.sleep(3)
return 'hello world! from slow!'
app.run(host='0.0.0.0', port=8080)
Run the app with python example.py
and visit either localhost:8080
or localhost:8080/slow
and finally you will be able to see your metrics on localhost:8080/metrics
!
You can also point prometheus to scrape this endpoint and see directly the metrics in there.