Skip to main content

A simple, efficient timer for monitoring execution time and logging threshold violations

Project description

Timer Sentinel

PyPI version Python Support License: MIT

A dead-simple, fast, and lightweight Python timer for catching slow code. Zero dependencies, optimized performance, just pure Python doing one thing well.

Why?

I built this after spending too much time debugging inconsistent latency in complex systems. Sometimes a function that should take 100ms randomly takes 5 seconds, and you need to know when and where.

This timer helped me catch those issues. Works great with Grafana + Loki for analyzing logs and creating alerts, or use the callback for whatever custom logic you need.

What it does

  • ⏱️ Times your code with high precision
  • 📊 Logs when it's too slow
  • 🔔 Runs callbacks when thresholds are exceeded
  • 🎯 Works as decorator, context manager, or manual timer
  • 🔀 Supports async functions
  • 🚀 Fast - optimized with __slots__, minimal overhead
  • 🪶 Lightweight - zero dependencies, tiny footprint
  • Efficient - uses time.perf_counter() for microsecond precision

Install

pip install timer-sentinel

Usage

from timer_sentinel import TimerSentinel

# Context manager
with TimerSentinel(threshold=1.0, name="db_query"):
    slow_database_call()

# Decorator
@TimerSentinel(threshold=0.5)
def api_call():
    return requests.get(url)

# Async
@TimerSentinel(threshold=0.5)
async def async_operation():
    await some_io()

Real-world Examples

Catch slow database queries

@TimerSentinel(threshold=0.1, name="db_query")
def get_user(user_id):
    return db.query(f"SELECT * FROM users WHERE id={user_id}")

Integration with Grafana + Loki

Just log to your standard logger and ship logs to Loki:

import logging

logger = logging.getLogger("production")

with TimerSentinel(threshold=1.0, logger=logger, name="api_call"):
    call_external_api()

Create alerts in Grafana based on the "OVERTIME" keyword or parse execution times. Good for catching performance regressions in production.

API

TimerSentinel(
    threshold: float,              # Max time in seconds
    name: str = "",                # Timer name (uses function name if decorator)
    logger: Logger | None = None,  # Custom logger
    on_exceed_keyword: str = "OVERTIME",     # Log keyword
    on_exceed_level: int = WARNING,          # Log level
    on_exceed_callback: Callable = None,     # Callback function
    callback_args: dict = None,              # Args for callback
)

Methods:

  • start() - Start timing
  • end() - Stop timing
  • report() - Log if threshold exceeded

Use as:

  • Decorator: @TimerSentinel(...)
  • Context manager: with TimerSentinel(...)
  • Manual: timer.start()timer.end()timer.report()

Requirements

  • Python 3.10+
  • No external dependencies

Contributing

Found a bug? Want a feature? Open an issue or PR on GitHub.

License

MIT License - do whatever you want with it.

Author

Aaron Ojeda · GitHub

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

timer_sentinel-1.0.2.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

timer_sentinel-1.0.2-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file timer_sentinel-1.0.2.tar.gz.

File metadata

  • Download URL: timer_sentinel-1.0.2.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for timer_sentinel-1.0.2.tar.gz
Algorithm Hash digest
SHA256 03847727183544d50aa63e9310190ba3257fe4231dfbf3f7fb6a7102203d0062
MD5 d5d74c49ef1ea06e9039c5ceb3b7e6cd
BLAKE2b-256 8d47857d39a256a18efff19d54649a4449e5691a9a73cbb9b551273058417c97

See more details on using hashes here.

Provenance

The following attestation bundles were made for timer_sentinel-1.0.2.tar.gz:

Publisher: publish-pypi.yml on aaronojedait/timer-sentinel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file timer_sentinel-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: timer_sentinel-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for timer_sentinel-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 eace417665317e9c3d29a9438f8d31a9b3930771fb8bcc448a870efeac646b4c
MD5 4317d76feb3aaa77e85ab229b536d35a
BLAKE2b-256 d6928fe6b5fb834b4e76ee89e92478b1661b04c7facdf4e180c4ad3f645a3df4

See more details on using hashes here.

Provenance

The following attestation bundles were made for timer_sentinel-1.0.2-py3-none-any.whl:

Publisher: publish-pypi.yml on aaronojedait/timer-sentinel

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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