Skip to main content

Minimalist SRE metrics for FastAPI and Flask

Project description

SRE Metrics

PyPI

Minimalist SRE metrics for Python web frameworks.

Features

  • Default metrics on port 9090
  • Tracks individual status codes + optional grouping
  • FastAPI and Flask support
  • <5ms overhead

FastAPI Installation

pip install sre_metrics[fastapi]

FastAPI Usage

from fastapi import FastAPI
from sre_metrics import instrument_fastapi

app = FastAPI()
instrument_fastapi(app)  # Metrics on :9090 by default

# Custom port
instrument_fastapi(app, metrics_port=9091, excluded_paths=["/healthz"])

# All options
instrument_fastapi(
    app,
    metrics_port=9091,               # Metrics server port (default: 9090)
    prefix="app_",                   # Metric name prefix (default: "http_")
    buckets=[0.1, 0.5, 1, 5],       # Latency histogram buckets in seconds
    excluded_paths=["/healthz"],     # Path patterns to exclude from metrics
    group_status_codes=False,        # Disable 2xx/3xx/4xx/5xx grouping
    inprogress_labels=True,          # Add method/path labels to in-progress gauge
    custom_labels={"env": "prod"},   # Additional labels for all metrics
    skip_untemplated=True            # Ignore routes without path templates
)

Flask Installation

pip install sre_metrics[flask]
from flask import Flask
from sre_metrics import instrument_flask

app = Flask(__name__)
instrument_flask(app)  # Metrics on :9090

# Custom options:
instrument_flask(app, metrics_port=9091, excluded_paths=["/healthz"])

# All options
instrument_flask(
    app,
    metrics_port=9091,               # Metrics server port (default: 9090)
    prefix="app_",                   # Metric name prefix (default: "http_")
    buckets=[0.1, 0.5, 1, 5],       # Latency histogram buckets in seconds
    excluded_paths=["/static/.*"],   # Regex patterns of paths to exclude
    group_status_codes=True,         | Enable status code grouping (default)
    normalize_path=lambda p: re.sub(r'/user/\d+', '/user/{id}', p), # Path normalization
    enable_by_envvar="ENABLE_METRICS" # Only enable if env var is set
)

Custom Status Code Classification

def custom_classifier(status_code: int) -> str:
    if status_code == 418: return "teapot"
    return f"{status_code//100}xx"

instrument_fastapi(
    app,
    status_classifier=custom_classifier
)

Kubernetes Example

instrument_fastapi(
    app,
    custom_labels={
        "service": os.getenv("SERVICE_NAME"),
        "pod": os.getenv("POD_NAME"),
        "namespace": os.getenv("NAMESPACE")
    }
)

Rebuild locally

uv sync
uv pip install twine fastapi flask httpx uvicorn requests
uv run pytest
rm -rf build/ dist/ sre_metrics.egg-info/
uv run python -m build
pip install --force-reinstall dist/sre_metrics-*-py3-none-any.whl
python -c "import sre_metrics;"

## twine
uv add twine
rm -rf build/ dist/ sre_metrics.egg-info/
uv run python -m build
pip install --force-reinstall dist/sre_metrics-*-py3-none-any.whl
twine upload dist/*

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sre_metrics-1.0.5.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sre_metrics-1.0.5-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file sre_metrics-1.0.5.tar.gz.

File metadata

  • Download URL: sre_metrics-1.0.5.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for sre_metrics-1.0.5.tar.gz
Algorithm Hash digest
SHA256 dc48ab7d41f370239554178b1f3cb66be235f037d5a59956174226d1e89e0b59
MD5 10a1100fe6d032ea1aae191c5f9f9850
BLAKE2b-256 bdf42b7d0982c9031bfa6dd5d300083a75d7cc152f12e8d66de5d5282ea75610

See more details on using hashes here.

File details

Details for the file sre_metrics-1.0.5-py3-none-any.whl.

File metadata

  • Download URL: sre_metrics-1.0.5-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.8

File hashes

Hashes for sre_metrics-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 93a37e1d1c0a75fd35eff51cf79b707e42ecb0e61514b11118ad3fa042a85aa1
MD5 d1de74ce4dc464f3465c3d7d1c19de05
BLAKE2b-256 9a78d0a85e4ed645735d698ca23708a9a20d4cf289feb582c23df1584a140ec0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page