Lightweight TPS monitoring agent for distributed systems
Project description
TPS Agent
A lightweight Python library for measuring and throttling TPS (Transactions Per Second) in distributed applications. TPS Agent collects metrics locally and sends them to a centralized TPS Collector server.
Features
- Decorators: Simple
@measure_tpsand@throttledecorators - Local Storage: Temporary local storage with automatic cleanup
- Reliable Transmission: Automatic retry and acknowledgment mechanism
- Agent Health: Heartbeat monitoring with the collector
- Async Support: Works with both sync and async functions
- Zero Configuration: Works out of the box with sensible defaults
Installation
pip install tps-agent
Quick Start
1. Configure the Agent
from tps_agent import configure_agent
# Configure once at application startup
configure_agent(
collector_url="http://tps-collector:8080",
server_id="web-server-1"
)
2. Use Decorators
from tps_agent import measure_tps, throttle
@measure_tps(gateway="payment_api")
def process_payment(amount):
# Your payment processing logic
return {"status": "success", "amount": amount}
@throttle(gateway="external_api", max_tps=100)
@measure_tps(gateway="external_api")
def call_external_service():
# This will be throttled to max 100 TPS
# and metrics will be collected
return requests.get("https://api.example.com/data")
# Async functions are supported too
@measure_tps(gateway="async_api")
async def async_operation():
await asyncio.sleep(0.1)
return "done"
Configuration
Environment Variables
# Required
TPS_COLLECTOR_URL=http://localhost:8080
TPS_SERVER_ID=my-server-1
# Optional
TPS_BATCH_SIZE=100
TPS_TRANSMISSION_INTERVAL=60
TPS_MAX_RETRIES=3
TPS_LOCAL_RETENTION_MINUTES=10
TPS_ENABLE_METRICS=true
TPS_ENABLE_THROTTLING=true
Programmatic Configuration
from tps_agent import configure_agent, AgentConfig
config = AgentConfig(
server_id="web-server-1",
collector_url="http://tps-collector:8080",
batch_size=50,
transmission_interval=30,
max_retries=5
)
configure_agent(config=config)
Advanced Usage
Custom Tags
@measure_tps(gateway="user_api", tags={"version": "v2", "region": "us-east"})
def get_user_profile(user_id):
return {"user_id": user_id, "name": "John Doe"}
Error Tracking
@measure_tps(gateway="risky_operation", track_errors=True)
def risky_operation():
if random.random() < 0.1:
raise ValueError("Something went wrong")
return "success"
Agent Statistics
from tps_agent import get_agent
agent = get_agent()
stats = agent.get_stats()
print(f"Local metrics: {stats['local_metrics_count']}")
print(f"Total stored: {stats['total_metrics_stored']}")
How It Works
- Local Collection: Decorators collect metrics (duration, success/failure, errors) locally
- Batching: Metrics are batched in memory for efficient transmission
- Transmission: Batches are sent to the collector at regular intervals
- Acknowledgment: Collector confirms receipt, agent cleans up local data
- Retry Logic: Failed transmissions are retried with exponential backoff
- Heartbeat: Agent sends periodic health updates to collector
Integration Examples
Flask Application
from flask import Flask
from tps_agent import configure_agent, measure_tps
app = Flask(__name__)
# Configure at startup
configure_agent(
collector_url="http://tps-collector:8080",
server_id="flask-app-1"
)
@app.route('/api/users/<user_id>')
@measure_tps(gateway="user_api", endpoint="get_user")
def get_user(user_id):
# Your logic here
return {"user_id": user_id}
if __name__ == '__main__':
app.run()
FastAPI Application
from fastapi import FastAPI
from tps_agent import configure_agent, measure_tps
app = FastAPI()
# Configure at startup
@app.on_event("startup")
async def startup():
configure_agent(
collector_url="http://tps-collector:8080",
server_id="fastapi-app-1"
)
@app.get("/api/users/{user_id}")
@measure_tps(gateway="user_api", endpoint="get_user")
async def get_user(user_id: str):
# Your async logic here
return {"user_id": user_id}
License
MIT License
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
tps_agent-2.0.0.tar.gz
(23.9 kB
view details)
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
tps_agent-2.0.0-py3-none-any.whl
(26.8 kB
view details)
File details
Details for the file tps_agent-2.0.0.tar.gz.
File metadata
- Download URL: tps_agent-2.0.0.tar.gz
- Upload date:
- Size: 23.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2767b57bc859e7d70613ae0224c62c7b5e25908d667e1ad8f8e668c0236a46dc
|
|
| MD5 |
1beeb263054f630cd0172e370da92010
|
|
| BLAKE2b-256 |
425165fea368e2c945d81725a70b6ccce25b4b8a80ace6bfa261161689004824
|
File details
Details for the file tps_agent-2.0.0-py3-none-any.whl.
File metadata
- Download URL: tps_agent-2.0.0-py3-none-any.whl
- Upload date:
- Size: 26.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6e86e45ba506fb4f444f5649f72a6d91ba8f534a2f4743cc40b8414b124018f5
|
|
| MD5 |
0d0d863f51d7377a431980115d48a285
|
|
| BLAKE2b-256 |
a7f7822e65051ef8a746c2d3a4baefdb7ab3ed360c35dc4a9d13ff4a29f19ed8
|