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.1.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.1-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tps_agent-2.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 39ac1a1797509b7bb831f16dae6d536eb42cbfac776f783229bf93ab349645d5
MD5 aa50b0b8e17db5a7d5d6385f3e4d7cf3
BLAKE2b-256 f1544626eb66cfbd47edab1ff5807221c71c3094677779fb3f7a1dd6861b8211

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tps_agent-2.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c20c6fe1b60b2af58d7004acdad5f1f63e56dcb0baf741b7dc1ae61d821500cc
MD5 e335c1aae00da7e4f0a50c724240aaba
BLAKE2b-256 b0dcfc8f2dd096710838accaa3c21239894b49ab441f063a98825ceec5a39f0d

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