Metadata-Version: 2.4
Name: exewatch-sdk
Version: 0.2.0
Summary: ExeWatch SDK for Python: logs, errors, timings and metrics for your application. Standard library only.
Project-URL: Homepage, https://exewatch.com
Classifier: Programming Language :: Python :: 3
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: POSIX :: Linux
Classifier: Operating System :: MacOS
Requires-Python: >=3.7
Description-Content-Type: text/markdown

# ExeWatch Python SDK

Lightweight APM / observability SDK for Python applications.  
**Zero external dependencies** — uses only the Python 3.7+ standard library.

## Installation

```
pip install exewatch-sdk
```

Python 3.7 or later. In a project with a virtual environment, activate it first. To update: `pip install --upgrade exewatch-sdk`. The package is on [PyPI](https://pypi.org/project/exewatch-sdk/) and you import it as `exewatch`:

```python
from exewatch import initialize_exewatch, ew, finalize_exewatch
```

## Quick Start

```python
from exewatch import initialize_exewatch, ew, finalize_exewatch

# Initialize (once at startup)
initialize_exewatch("ew_win_YOUR_API_KEY", "your-customer-id", app_version="1.0.0")

# Log
ew.info("Application started", "startup")
ew.warning("Disk space low", "system")

# Shutdown (at exit — also registered via atexit)
finalize_exewatch()
```

## Features

### Logging (5 levels)

```python
ew.debug("verbose details", "module")
ew.info("normal operation", "module")
ew.warning("something unusual", "module")
ew.error("something failed", "module")       # includes breadcrumbs + stack trace
ew.fatal("critical failure", "module")        # includes breadcrumbs + stack trace
```

Extra data via keyword arguments:

```python
ew.info("Order placed", "orders", order_id="12345", total=99.99)
```

### Exception Capture

```python
try:
    risky_operation()
except Exception as e:
    ew.error_with_exception(e, "module", "Context message")
```

### Timing / Profiling

```python
ew.start_timing("db_query", "database")
result = db.execute("SELECT ...")
elapsed = ew.end_timing("db_query")  # returns ms, logs automatically

# Stack-based (LIFO)
ew.start_timing("outer")
ew.start_timing("inner")
ew.end_timing()  # ends "inner"
ew.end_timing()  # ends "outer"

# With success/failure
elapsed = ew.end_timing("op", success=False)

# Cancel without logging
ew.cancel_timing("op")
```

### Breadcrumbs

Automatically attached to ERROR/FATAL logs.

```python
from exewatch import BreadcrumbType

ew.add_breadcrumb(BreadcrumbType.NAVIGATION, "nav", "Opened settings")
ew.add_breadcrumb(BreadcrumbType.CLICK, "ui", "Clicked Save")
ew.add_breadcrumb(BreadcrumbType.QUERY, "db", "SELECT * FROM users")
# Next error() or fatal() will include these breadcrumbs
```

### Metrics

```python
# Counters (cumulative)
ew.increment_counter("orders.placed", 1.0, "shop")
ew.increment_counter("revenue", 49.99, "billing")

# Gauges (instantaneous)
ew.record_gauge("memory_mb", 256.0, "system")
ew.record_gauge("queue_size", 42.0, "worker")

# Periodic gauge (sampled automatically in background)
def get_memory():
    import resource
    return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024.0

ew.register_periodic_gauge("memory_mb", get_memory, "system")
```

### User Identity

```python
ew.set_user("user-42", "jane@example.com", "Jane Doe")
# All subsequent logs include user info
ew.info("User action")

ew.clear_user()
```

### Global Tags

```python
ew.set_tag("environment", "production")
ew.set_tag("feature_flag", "new_checkout")
# All subsequent logs include tag_environment, tag_feature_flag

ew.remove_tag("feature_flag")
ew.clear_tags()
```

### Device Info

```python
# Send hardware info (CPU, memory, disks, OS, etc.)
ew.send_device_info()

# Custom device metadata
ew.set_custom_device_info("license_type", "enterprise")
ew.set_custom_device_info("deployment", "on-premise")
ew.send_custom_device_info()
```

### Runtime Configuration

```python
# Change customer ID at runtime
ew.set_customer_id("new-customer")

# Enable/disable
ew.enabled = False  # suspend all logging
ew.enabled = True   # resume
```

## Configuration Options

```python
initialize_exewatch(
    api_key="ew_win_...",           # Required: your API key
    customer_id="acme-corp",         # Required: customer identifier
    app_version="1.0.0",            # Optional: application version
    endpoint="https://exewatch.com", # API endpoint
    storage_path="",                 # Custom storage path (default: platform-specific)
    buffer_size=100,                 # Events before auto-persist
    flush_interval_ms=5000,          # Persist interval (ms)
    retry_interval_ms=30000,         # Retry interval on failure (ms)
    sample_rate=1.0,                 # 0.0-1.0 (errors/fatals always sent)
    gauge_sampling_interval_sec=30,  # Periodic gauge interval
    max_pending_age_days=7,          # Auto-cleanup old files
    anonymize_device_id=False,       # GDPR: hash username in device ID
)
```

## Architecture

- **Single file** (`exewatch.py`): no package installation needed
- **File queue**: logs persisted to disk before sending (crash-safe)
- **Background shipper thread**: sends queued files to server
- **Background sampler thread**: samples registered periodic gauges
- **Thread-safe**: all shared state protected with locks
- **Thread-local breadcrumbs**: each thread has its own trail

## Platform Support

| Feature | Windows | Linux |
|---------|---------|-------|
| Logging | Yes | Yes |
| CPU info | Registry | /proc/cpuinfo |
| Memory info | GlobalMemoryStatusEx | /proc/meminfo |
| Disk info | Win32 API | /proc/mounts + statvfs |
| Monitor info | EnumDisplayMonitors | N/A |
| Boot time | GetTickCount64 | /proc/stat btime |

## Requirements

- Python 3.7+
- No external dependencies

## Running Tests

```bash
pip install pytest
cd SDK/Python
pytest tests/ -v
```
