devskin-agent 1.0.0
DevSkin Monitor Agent - Python Instrumentation SDK
Requires Python: >=3.8
Keywords: monitoring, apm, agent, instrumentation, observability, tracing, logging, metrics

# devskin-agent

DevSkin APM Agent for Python - Automatic instrumentation for Flask, Django, and FastAPI.

## Installation

```bash
pip install devskin-agent
```

## Quick Start

### Flask

```python
from flask import Flask
from devskin_agent import init, start_agent
from devskin_agent.instrumentation import flask_middleware

# Initialize agent
agent = init(
    server_url='http://localhost:3000',
    api_key='your-api-key',
    service_name='my-flask-app',
    service_version='1.0.0',
    environment='production',
)

start_agent()

# Create Flask app
app = Flask(__name__)

# Add middleware
flask_middleware(app, agent)

@app.route('/api/users')
def get_users():
    # Automatically traced!
    return {'users': []}

if __name__ == '__main__':
    app.run()
```

### Django

Add to your `settings.py`:

```python
MIDDLEWARE = [
    'devskin_agent.instrumentation.DjangoMiddleware',
    # ... other middleware
]

# At the end of settings.py
from devskin_agent import init, start_agent

DEVSKIN_AGENT = init(
    server_url='http://localhost:3000',
    api_key='your-api-key',
    service_name='my-django-app',
    environment='production',
)

start_agent()
```

### FastAPI

```python
from fastapi import FastAPI
from devskin_agent import init, start_agent
from devskin_agent.instrumentation import fastapi_middleware

# Initialize agent
agent = init(
    server_url='http://localhost:3000',
    api_key='your-api-key',
    service_name='my-fastapi-app',
)

start_agent()

# Create FastAPI app
app = FastAPI()

# Add middleware
fastapi_middleware(app, agent)

@app.get('/api/users')
async def get_users():
    # Automatically traced!
    return {'users': []}
```

### Manual Span Creation

```python
from devskin_agent import get_agent, SpanBuilder, SpanKind

agent = get_agent()

def process_order(order_id):
    span = SpanBuilder(
        name='process_order',
        kind=SpanKind.INTERNAL,
        service_name=agent.get_config().service_name,
        agent=agent
    )

    span.set_attribute('order.id', order_id)

    try:
        # Your business logic
        order = fetch_order(order_id)
        span.set_attribute('order.amount', order['amount'])
        return order
    except Exception as e:
        span.record_error(e)
        raise
    finally:
        span.end()
```

## Configuration

| Parameter | Type | Default | Description |
|-----------|------|---------|-------------|
| `server_url` | str | *required* | DevSkin backend URL |
| `api_key` | str | *required* | API key for authentication |
| `service_name` | str | *required* | Name of your service |
| `service_version` | str | None | Version of your service |
| `environment` | str | None | Environment (production, staging, etc) |
| `enabled` | bool | True | Enable/disable agent |
| `sample_rate` | float | 1.0 | Sample rate (0.0 to 1.0) |
| `batch_size` | int | 100 | Batch size before flushing |
| `flush_interval` | float | 10.0 | Flush interval in seconds |
| `debug` | bool | False | Enable debug logging |

## Environment Variables

```bash
export DEVSKIN_SERVER_URL=http://localhost:3000
export DEVSKIN_API_KEY=your-api-key
export DEVSKIN_SERVICE_NAME=my-service
export DEVSKIN_ENVIRONMENT=production
export DEVSKIN_SAMPLE_RATE=1.0
```

```python
import os
from devskin_agent import init

agent = init(
    server_url=os.getenv('DEVSKIN_SERVER_URL'),
    api_key=os.getenv('DEVSKIN_API_KEY'),
    service_name=os.getenv('DEVSKIN_SERVICE_NAME'),
    environment=os.getenv('DEVSKIN_ENVIRONMENT'),
    sample_rate=float(os.getenv('DEVSKIN_SAMPLE_RATE', '1.0')),
)
```

## Features

- ✅ Automatic Flask instrumentation
- ✅ Automatic Django instrumentation
- ✅ Automatic FastAPI instrumentation
- ✅ Manual span creation
- ✅ Distributed tracing with trace ID propagation
- ✅ Error tracking and reporting
- ✅ Sampling support
- ✅ Context propagation across async operations
- ✅ Service discovery

## License

MIT