Skip to content

pytheus

playing with metrics

ci pypi versions license downloads

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

pip install pytheus

Optionally if you want to use the Redis backend (for multiprocess support) you will need the redis library:

pip install redis

# or everything in one command
pip install pytheus[redis]

If you want to try the Rust based backend (for multiprocess support):

pip install pytheus-backend-rs

Tip

Try the Rust powered backend! ๐Ÿฆ€

pip install pytheus-backend-rs


Example

example.py
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.