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.1.tar.gz (5.0 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.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: timer_sentinel-1.0.1.tar.gz
  • Upload date:
  • Size: 5.0 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.1.tar.gz
Algorithm Hash digest
SHA256 0515e1712bd42d2a163f730a620e077b8dceb13206eee404297613aae5333e10
MD5 6fda63240e270a92f886950a5cc71f31
BLAKE2b-256 2fd14bc0dbf9f40bc9e6c8f6f8f09e1660f74c86d69ad048c54062369524e6ab

See more details on using hashes here.

Provenance

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

Publisher: publish.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.1-py3-none-any.whl.

File metadata

  • Download URL: timer_sentinel-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 45b0374a50789b1e8ef801959500dfb2e1859aef8e54f6f573c370e0c911e970
MD5 4bde48408b4f6391166115ddf0875e13
BLAKE2b-256 6d99441125525b66a3d8dd3fd6e757e44b44059822912adb501124c66b77a6b9

See more details on using hashes here.

Provenance

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

Publisher: publish.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