An asynchronous uptime and latency system designed to monitor API endpoints
Project description
🖥️ Clockwerk (Uptime Monitor)
Clockwerk is a Python application designed to monitor the availability and performance (latency or status) of various API Endpoints (URLs). It tracks the health of the monitored endpoint and stores the results in a database for future analysis and reporting. The application uses SQLalchemy's ORM-based database interaction to store results, and Asyncio's async sessions for asynchronous operations.
🚀 Key Features
- 🔍 Monitors multiple endpoints
- ⏱️ Tracks endpoint health and status with real-time status updates
- 📊 Logs the latency and status code for each request
- 🗃️ Stores monitoring results in a database of your own choice
- ⚡ Supports asynchronous database operations to ensure high performance
- 🚨 Sends alert message to report endpoint failure or latency
- 🧩 Supports MySQL, PostgreSQL and SQLite
📦 Installation
pip install clockwerk
poetry add clockwerk
🌎 Code Usage
Start by creating the necessary config models
MonitorConfig,EmailConfig,DatabaseConfig.
# Import the necessary config-models and scheduling loop
from clockwerk.scheduler import scheduling_loop
from clockwerk.config import MonitorConfig, EmailConfig, DatabaseConfig
from clockwerk.models import Endpoint
# Create an instance of the MonitorConfig to specify monitored endpoints.
monitor_config = MonitorConfig(
endpoints = [
Endpoint(url: "https://test-exmaple.com", timeout=2, alert_threshold=3),
Endpoint(url: "https://api_testpoint.org", timeout=3, alert_threshold=2)
],
check_interval = 60, # Checks the specified endpoints every 60 seconds.
latency_threshold = 1.5 # Raises an alert if the endpoint latency exceeds the specified number (seconds)
)
# Model for e-mail configuration used to raise and send alert messages.
email_config = EmailConfig(
smtp_host="smtp.mailtrap.io", # SMTP Host
smtp_port=587, # Port to run your email alerts from
email_from="randommail@yourdomain.com", # Email address sending the alerts
email_to="alerts@yourdomain.com" # Email address receiving the alerts.
)
# Database configuration model (MySQL, PostgreSQL, SQLite)
db_config = DatabaseConfig(
db_activation=True, # Writes results to DB if True.
db_driver="sqlite", # Specify the desired DB platform.
db_name="clockwerk.db", # Identifying name for DB.
db_user="root", # Identifying user for DB.
db_password="SuperSecret123", # Individual password for DB access.
db_host="localhost", # Host associated with current DB.
db_port=1200 # Available port to run DB connection on.
)
Utilise the config models to create an instance of
scheduling_loopto run. As Clockwerk's monitor is an async function it can be run with eitherasyncio.run()orawait.
# Import the necessary config-models and scheduling loop
import asyncio
from clockwerk.scheduler import scheduling_loop
from clockwerk.config import MonitorConfig, EmailConfig, DatabaseConfig
from clockwerk.models import Endpoint
# Run the scheduling loop (if running the loop from a synchronous context)
asyncio.run(scheduling_loop(
monitor_config=monitor_config,
email_config=email_config,
db_config=db_config
))
# Run the scheduling loop (if running the loop from an asynchronous context)
await scheduling_loop(
monitor_config=monitor_config,
email_config=email_config,
db_config=db_config
)
📋 Monitor & Endpoints
In order to properly configure the main scheduling_loop, Clockwerk requires a list of specified endpoints (URLs) scheduled for monitoring. These endpoints are passed in to the main configuration object (MonitorConfig), along with specifications detailing the interval between checks and maximum latency allowed:
check_interval,latency_threshold.
| Parameters | Description |
|---|---|
| endpoints | A list of endpoint-objects to monitor. |
| check_interval | Time in seconds between each health check (Default: 60) |
| latency_threshold | Max latency in seconds before triggering an alert (Default: 1.0) |
✉️ E-mail Alerts
Additionally, Clockwerk supports asynchronous email alerts (aiohttp) sent to individual e-mails if monitored Endpoints fail their respective health check.
| Parameters | Description |
|---|---|
| smtp_host | SMTP server for sending email alerts. |
| smtp_port | SMTP port (Default: 587) |
| email_from | Sender's e-mail address. |
| email_to | Recipient's e-mail address. |
♾️ Database Setup
Clockwerk supports asynchronous database operations, allowing users to dynamically store results in one of three currently supported database platforms:
MySQL,PostgreSQL,SQLiteBuilt on SQLalchemy's asynchronous sessions and ORM framework, allowing for quick and seamless database connection and storage. If the individual user does not require results stored in a database, simply set the DatabaseConfig's activation variable to False,db_activation=False. This disables any form of database connection used by the scheduling loop.
# To initialise database connectivity use a DatabaseConfig object to specify desired parameters.
from clockwerk.config import DatabaseConfig
| Parameter | Description |
|---|---|
| db_activation | Enable/Disable database connection and storage (Default: False) |
| db_driver | One of available drivers MySQL, PostgreSQL, SQLite |
| db_host | Database host (e.g. localhost) |
| db_name | Identifying name of the target database |
| db_user | Username for database connection |
| db_password | Password for database access |
| db_port | Port used to connect to database |
📈 Future Roadmap
Roadmap over future improvements and application extensions.
- Slack / Webhook integration for alerts.
- Docker support for deployment
- Dashboard with data analysis and visualisation (Prometheus)
🤝 Contributing
Contributions to the application's future improvements are more than welcome. Feel free to open an issue or submit a pull request.
- Fork the repo
- Create a new branch (
git checkout -b feature-name) - Commit your changes (
git commit -am "Add new feature") - Push to the branch (
git push origin feature-name) - Open a Pull request
📝 Licensing
This project is licensed under MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file clockwerk-0.1.2.tar.gz.
File metadata
- Download URL: clockwerk-0.1.2.tar.gz
- Upload date:
- Size: 18.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1bd14cdd8efbf9cfb75cf8e8117d7276b5f503a3b06870e80d0386430c2f45a3
|
|
| MD5 |
03e9ebdc4515365462f54c6917818ac1
|
|
| BLAKE2b-256 |
e498da12049ce935ca0f57eaca2e571ae3d62717b1c7fdeaa8d2b50263acc34a
|
File details
Details for the file clockwerk-0.1.2-py3-none-any.whl.
File metadata
- Download URL: clockwerk-0.1.2-py3-none-any.whl
- Upload date:
- Size: 18.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c2f7cd4897512cdaa59b3e12768c37a69688e6a71bf6da2871d52d2efbd9464c
|
|
| MD5 |
85f7be31531298ed560d3aea8a441f88
|
|
| BLAKE2b-256 |
aced9d131cdab15501086d0bd9e11dcef2aeb94e7aefe21b9f91a9f28fa6169b
|