Skip to main content

Lightweight TPS monitoring agent with PostgreSQL Direct Strategy

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_tps and @throttle decorators
  • 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

  1. Local Collection: Decorators collect metrics (duration, success/failure, errors) locally
  2. Batching: Metrics are batched in memory for efficient transmission
  3. Transmission: Batches are sent to the collector at regular intervals
  4. Acknowledgment: Collector confirms receipt, agent cleans up local data
  5. Retry Logic: Failed transmissions are retried with exponential backoff
  6. 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.1.0.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

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

tps_agent-2.1.0-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

Details for the file tps_agent-2.1.0.tar.gz.

File metadata

  • Download URL: tps_agent-2.1.0.tar.gz
  • Upload date:
  • Size: 28.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tps_agent-2.1.0.tar.gz
Algorithm Hash digest
SHA256 85d7bc25dd32dabf0de6f82442b135b4f2f4bddc36f6fab0b2a7f390f1156317
MD5 79b6d9b206a8d4dc264eb5c5fc5dc15a
BLAKE2b-256 08b177b9b21f960a49a17f56bc12a79ce5944f3d566be34cd492a9dc4537cdb2

See more details on using hashes here.

File details

Details for the file tps_agent-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: tps_agent-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tps_agent-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cf43b8e92d93aac02cbe7037ea10ac5dceed9ade6e5f1b0a2db9187c16ca1f33
MD5 51f3dc9825d66b87423ee370f97f7800
BLAKE2b-256 2a84008f7a53a80941ed1c9dcde92e6e6f7c6e6ac97fa7af1449416b50dc7c6f

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