Skip to main content

A lightweight, decorator-based query performance tracking library for Python applications. Monitor and analyze database query performance with ease.

Project description

GitHub Release GitHub Issues or Pull Requests

PyPI - Downloads GitHub Actions Workflow Status GitHub forks

PyPI - License

🐍 pyquerytracker

pyquerytracker is a lightweight Python utility to track and analyze database query performance using simple decorators. It enables developers to gain visibility into SQL execution time, log metadata, and export insights in JSON format — with optional FastAPI integration and scheduled reporting.


🚀 Features

  • ✅ Easy-to-use decorator to track function execution (e.g., SQL queries)
  • ✅ Capture runtime, function name, args, return values, and more

TODO Features

  • ✅ Export logs to JSON or CSV
  • ✅ FastAPI integration to expose tracked metrics via REST API
  • ✅ Schedule periodic exports using APScheduler
  • ✅ Plug-and-play with any Python database client (SQLAlchemy, psycopg2, etc.)
  • ✅ Modular and extensible design

📦 Installation

pip install pyquerytracker

🔧 Configuration

import logging
from pyquerytracker.config import configure

configure(
    slow_log_threshold_ms=200,     # Log queries slower than 200ms
    slow_log_level=logging.DEBUG   # Use DEBUG level for slow logs
)

⚙️ Usage

Basic Usage

import time
from pyquerytracker import TrackQuery

@TrackQuery()
def run_query():
    time.sleep(0.3)  # Simulate SQL execution
    return "SELECT * FROM users;"

run_query()

Output:

2025-06-14 14:23:00,123 - pyquerytracker - INFO - Function run_query executed successfully in 305.12ms

🧩 Async Support

Use the same decorator with async functions or class methods:

import asyncio
from pyquerytracker import TrackQuery

@TrackQuery()
async def fetch_data():
    await asyncio.sleep(0.2)
    return "fetched"

class MyService:
    @TrackQuery()
    async def do_work(self, x, y):
        await asyncio.sleep(0.1)
        return x + y

asyncio.run(fetch_data())

🌐 Run the FastAPI Server

To view tracked query logs via REST, WebSocket, or a Web-based dashboard, start the built-in FastAPI server:

uvicorn pyquerytracker.api:app --reload
  • ⚠️ If your project or file structure is different, replace pyquerytracker.api with your own module path, like <your_project_name>.<your_server(file)_name>.

  • Open docs at http://localhost:8000/docs

  • Query Dashboard UI: http://localhost:8000/dashboard

  • REST endpoint: GET /queries

  • WebSocket stream: ws://localhost:8000/ws

Then run your tracked functions in another terminal or script:

@TrackQuery()
def insert_query():
    time.sleep(0.4)
    return "INSERT INTO users ..."

You’ll see logs live on the server via API/WebSocket.


📤 Export Logs

Enable exporting to CSV or JSON by setting config:

from pyquerytracker.config import configure

configure(
    export_type="json",
    export_path="./query_logs.json"
)

Let us know how you’re using pyquerytracker and feel free to contribute!

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

pyquerytracker-0.1.1.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

pyquerytracker-0.1.1-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file pyquerytracker-0.1.1.tar.gz.

File metadata

  • Download URL: pyquerytracker-0.1.1.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.4

File hashes

Hashes for pyquerytracker-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c3b5a49c7962f8c840b1c3ec49cbbaa48dfaf8890805c3cedeb6b7626b17dbd9
MD5 0567faca8f846979e9777a82eabc53fd
BLAKE2b-256 d7af2f4d0a11a79b02582e4d76b142e55ba76a0661ce49bfa1a21fe58c85d360

See more details on using hashes here.

File details

Details for the file pyquerytracker-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyquerytracker-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.4

File hashes

Hashes for pyquerytracker-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a49419aa5c890955f8845ac93a7be88e21c8d07499be3ea52367909c4e7be06f
MD5 a55158a5372e8e0b92d9dd0eb4b4be52
BLAKE2b-256 3e37fb77006cd9c2c084d728cab985039f562c1ab303004d183e2c1ddacbbaf5

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