Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 77 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,109 @@
<img src="https://github.com/Use-Tusk/drift-python-sdk/raw/main/images/tusk-banner.png" alt="Tusk Drift Banner">
</p>

<p align="center">
<a href="https://pypi.org/project/tusk-drift-python-sdk/"><img src="https://img.shields.io/pypi/v/tusk-drift-python-sdk" alt="PyPI version"></a>
<a href="https://opensource.org/licenses/Apache-2.0"><img src="https://img.shields.io/badge/License-Apache_2.0-blue.svg" alt="License: Apache 2.0"></a>
<a href="https://github.com/Use-Tusk/drift-python-sdk/commits/main/"><img src="https://img.shields.io/github/last-commit/Use-Tusk/drift-python-sdk" alt="GitHub last commit"></a>
<a href="https://x.com/usetusk"><img src="https://img.shields.io/twitter/url?url=https%3A%2F%2Fx.com%2Fusetusk&style=flat&logo=x&label=Tusk&color=BF40BF" alt="Tusk X account"></a>
<a href="https://join.slack.com/t/tusk-community/shared_invite/zt-3fve1s7ie-NAAUn~UpHsf1m_2tdoGjsQ"><img src="https://img.shields.io/badge/slack-badge?style=flat&logo=slack&label=Tusk&color=BF40BF" alt="Tusk Community Slack"></a>
</p>

The Python Tusk Drift SDK enables fast and deterministic API testing by capturing and replaying API calls made to/from your service. Automatically record real-world API calls, then replay them as tests using the [Tusk CLI](https://github.com/Use-Tusk/tusk-drift-cli) to find regressions. During replay, all outbound requests are intercepted with recorded data to ensure consistent behavior without side-effects.

<div align="center">

[![X URL](https://img.shields.io/twitter/url?url=https%3A%2F%2Fx.com%2Fusetusk&style=flat&logo=x&label=Tusk&color=BF40BF)](https://x.com/usetusk)
[![Slack URL](https://img.shields.io/badge/slack-badge?style=flat&logo=slack&label=Tusk&color=BF40BF)](https://join.slack.com/t/tusk-community/shared_invite/zt-3fve1s7ie-NAAUn~UpHsf1m_2tdoGjsQ)
![Demo](images/demo.gif)

<p><a href="https://github.com/Use-Tusk/drift-python-demo">Try it on a demo repo →</a></p>

</div>

# Drift Python SDK
## Documentation

For comprehensive guides and API reference, visit our [full documentation](https://docs.usetusk.ai/api-tests/installation#setup).

### SDK Guides

- [Initialization Guide](docs/initialization.md) - Set up the SDK in your Python application
- [Environment Variables](docs/environment-variables.md) - Environment variables reference
- [Quick Start Guide](docs/quickstart.md) - Record and replay your first trace

<div align="center">

![Tusk Drift Animated Diagram](images/tusk-drift-animated-diagram-light.gif#gh-light-mode-only)
![Tusk Drift Animated Diagram](images/tusk-drift-animated-diagram-dark.gif#gh-dark-mode-only)

</div>

## Requirements

- Python 3.12+

Python SDK for Tusk Drift instrumentation and replay.
Tusk Drift currently supports the following packages and versions:

- **Flask**: `flask>=2.0.0`
- **FastAPI**: `fastapi>=0.68.0`
- **Django**: `django>=3.2.0`
- **Requests**: `requests` (all versions)
- **HTTPX**: `httpx` (all versions)
- **psycopg**: `psycopg>=3.0.0`, `psycopg2>=2.8.0`
- **Redis**: `redis` (all versions)

If you're using packages or versions not listed above, please create an issue with the package + version you'd like an instrumentation for.

## Installation

### Step 1: Install the CLI

First, install and configure the Tusk Drift CLI by following our [CLI installation guide](https://github.com/Use-Tusk/tusk-drift-cli?tab=readme-ov-file#install). The CLI helps set up your Tusk configuration file and replays tests.

The wizard will eventually direct you back here when it's time to set up the SDK.

### Step 2: Install the SDK

After completing the CLI wizard, install the SDK:

```bash
pip install tusk-drift-python-sdk
```

### With Framework Support
#### With Framework Support

```bash
# Flask support
# With Flask support
pip install tusk-drift-python-sdk[flask]

# FastAPI support
# With FastAPI support
pip install tusk-drift-python-sdk[fastapi]

# Django support
# With Django support
pip install tusk-drift-python-sdk[django]
```

## Requirements
### Step 3: Initialize the SDK for your service

- Python 3.12+
Refer to our [initialization guide](docs/initialization.md) to set up the SDK for your service.

### Step 4: Run Your First Test

Follow along our [quick start guide](docs/quickstart.md) to record and replay your first test!
Comment thread
sohankshirsagar marked this conversation as resolved.

## Troubleshooting

Having issues?

- Check our [initialization guide](docs/initialization.md) for common setup issues
- Create an issue or reach us at [support@usetusk.ai](mailto:support@usetusk.ai).

## Community

Join our open source community on [Slack](https://join.slack.com/t/tusk-community/shared_invite/zt-3fve1s7ie-NAAUn~UpHsf1m_2tdoGjsQ).

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md) for development setup and guidelines.
We appreciate feedback and contributions. See [CONTRIBUTING.md](/CONTRIBUTING.md).

## License

Apache License 2.0
This project is licensed under the Apache License 2.0 - see the [LICENSE](LICENSE) file for details.
150 changes: 150 additions & 0 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
# Environment Variables

This guide covers the environment variables that can be set when using the Tusk Drift SDK.

## TUSK_DRIFT_MODE

The `TUSK_DRIFT_MODE` environment variable controls how the SDK operates in your application.

### Available Modes

| Mode | Description | When to Use |
| ---------- | ---------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| `RECORD` | Records traces for all instrumented operations | Set this in environments where you want to capture API traces (e.g., staging, production) |
| `REPLAY` | Replays previously recorded traces | Automatically set by the Tusk CLI when running `tusk run` - you should NOT set this manually |
| `DISABLED` | Disables all instrumentation and recording | Use when you want to completely disable Tusk with no performance impact |
| Unset | Same as `DISABLED` - no instrumentation or recording | Default state when the variable is not set |

### Important Notes

**Recording Traces:**

- Set `TUSK_DRIFT_MODE=RECORD` in any environment where you want to record traces
- This is typically staging, production, or local development environments
- Traces will be saved according to your `recording` configuration in `.tusk/config.yaml`

**Replaying Traces:**

- `TUSK_DRIFT_MODE` is automatically set to `REPLAY` by the Tusk CLI when you run `tusk run`
- **Do NOT** manually set `TUSK_DRIFT_MODE=REPLAY` in your application startup commands
- The start command specified in your `.tusk/config.yaml` should NOT cause `TUSK_DRIFT_MODE` to be set to anything - the CLI handles this automatically

**Disabling Tusk:**

- If `TUSK_DRIFT_MODE` is unset or set to `DISABLED`, the SDK will not add any instrumentation
- No data will be recorded and there should be **no performance impact**
- This is useful for environments where you don't need Tusk functionality

### Examples

**Recording in development:**

```bash
TUSK_DRIFT_MODE=RECORD python app.py
```

**Recording in production (via environment variable):**

```bash
# In your .env file or deployment configuration
TUSK_DRIFT_MODE=RECORD
```

**Start command in config.yaml (correct):**

```yaml
# .tusk/config.yaml
start_command: "python app.py" # Do NOT include TUSK_DRIFT_MODE here
```

**Replaying traces (handled by CLI):**

```bash
# The CLI automatically sets TUSK_DRIFT_MODE=REPLAY
tusk run
```

**Disabling Tusk:**

```bash
# Either unset the variable or explicitly disable
TUSK_DRIFT_MODE=DISABLED python app.py

# Or simply don't set it at all
python app.py
```

## TUSK_API_KEY

Your Tusk Drift API key, required when using Tusk Cloud for storing and managing traces.

- **Required:** Only if using Tusk Cloud (not needed for local-only trace storage)
- **Where to get it:** [Tusk Drift Dashboard](https://usetusk.ai/app/settings/api-keys)

### How to Set

**For Recording:**

- Must be provided in the `TuskDrift.initialize()` call:

```python
sdk = TuskDrift.initialize(
api_key=os.environ.get("TUSK_API_KEY"), # or hard-coded for non-production
# ... other options
)
```

**For Replay:**

- Can be set as an environment variable:

```bash
TUSK_API_KEY=your-api-key-here tusk run
```

- Or use the Tusk CLI login command (recommended):

```bash
tusk auth login
```

This will securely store your auth key for future replay sessions.

## TUSK_SAMPLING_RATE

Controls what percentage of requests are recorded during trace collection.

- **Type:** Number between 0.0 and 1.0
- **Default:** 1.0 (100% of requests)
- **Precedence:** This environment variable is overridden by the `sampling_rate` parameter in `TuskDrift.initialize()`, but takes precedence over the `sampling_rate` setting in `.tusk/config.yaml`

**Examples:**

```bash
# Record all requests (100%)
TUSK_SAMPLING_RATE=1.0 python app.py

# Record 10% of requests
TUSK_SAMPLING_RATE=0.1 python app.py
```

For more details on sampling rate configuration methods and precedence, see the [Initialization Guide](./initialization.md#configure-sampling-rate).

## Connection Variables

These variables configure how the SDK connects to the Tusk CLI during replay:

| Variable | Description | Default |
| ------------------ | ----------------------------------- | ------------------------- |
| `TUSK_MOCK_HOST` | CLI host for TCP connection | - |
| `TUSK_MOCK_PORT` | CLI port for TCP connection | - |
| `TUSK_MOCK_SOCKET` | Unix socket path for CLI connection | `/tmp/tusk-connect.sock` |

These are typically set automatically by the Tusk CLI and do not need to be configured manually.

---

## Related Documentation

- [Initialization Guide](./initialization.md) - SDK initialization parameters and config file settings
- [Quick Start Guide](./quickstart.md) - Record and replay your first trace
Loading