Skip to content

Commit 63fa4cd

Browse files
feat: requests/httpx e2e tests + refactor (#19)
1 parent 9a275be commit 63fa4cd

22 files changed

Lines changed: 1587 additions & 190 deletions

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pip install tusk-drift-python-sdk[flask]
2727

2828
# FastAPI support
2929
pip install tusk-drift-python-sdk[fastapi]
30+
31+
# Django support
32+
pip install tusk-drift-python-sdk[django]
3033
```
3134

3235
## Requirements

drift/instrumentation/django/middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def _record_request(self, request: HttpRequest, sdk, is_pre_app_start: bool) ->
179179

180180
start_time_ns = time.time_ns()
181181

182-
method = request.method
182+
method = request.method or ""
183183
path = request.path
184184
span_name = f"{method} {path}"
185185

@@ -397,7 +397,7 @@ def dict_to_schema_merges(merges_dict):
397397
status = SpanStatus(code=StatusCode.OK, message="")
398398

399399
# Django-specific: use route template for span name to avoid cardinality explosion
400-
method = request.method
400+
method = request.method or ""
401401
route_template = getattr(request, "_drift_route_template", None)
402402
if route_template:
403403
# Use route template (e.g., "users/<int:id>/")
@@ -502,7 +502,7 @@ def dict_to_schema_merges(merges_dict):
502502
duration_seconds = duration_ns // 1_000_000_000
503503
duration_nanos = duration_ns % 1_000_000_000
504504

505-
method = request.method
505+
method = request.method or ""
506506
route_template = getattr(request, "_drift_route_template", None)
507507
span_name = f"{method} {route_template}" if route_template else f"{method} {request.path}"
508508

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: 1
2+
3+
service:
4+
id: "httpx-e2e-test-id"
5+
name: "httpx-e2e-test"
6+
port: 8000
7+
start:
8+
command: "python src/app.py"
9+
readiness_check:
10+
command: "curl -f http://localhost:8000/health"
11+
timeout: 45s
12+
interval: 5s
13+
14+
tusk_api:
15+
url: "http://localhost:8000"
16+
17+
test_execution:
18+
concurrent_limit: 10
19+
batch_size: 10
20+
timeout: 30s
21+
22+
recording:
23+
sampling_rate: 1.0
24+
export_spans: false
25+
26+
replay:
27+
enable_telemetry: false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM python-e2e-base:latest
2+
3+
# Copy SDK source for editable install
4+
COPY . /sdk
5+
6+
# Copy test files
7+
COPY drift/instrumentation/httpx/e2e-tests /app
8+
9+
WORKDIR /app
10+
11+
# Install dependencies (requirements.txt uses -e /sdk for SDK)
12+
RUN pip install -q -r requirements.txt
13+
14+
# Make entrypoint executable
15+
RUN chmod +x entrypoint.py
16+
17+
# Create .tusk directories
18+
RUN mkdir -p /app/.tusk/traces /app/.tusk/logs
19+
20+
# Run entrypoint
21+
ENTRYPOINT ["python", "entrypoint.py"]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
services:
2+
app:
3+
build:
4+
context: ../../../..
5+
dockerfile: drift/instrumentation/httpx/e2e-tests/Dockerfile
6+
args:
7+
- TUSK_CLI_VERSION=${TUSK_CLI_VERSION:-latest}
8+
environment:
9+
- PORT=8000
10+
- TUSK_ANALYTICS_DISABLED=1
11+
- PYTHONUNBUFFERED=1
12+
working_dir: /app
13+
volumes:
14+
# Mount app source for development
15+
- ./src:/app/src
16+
# Mount .tusk folder to persist traces
17+
- ./.tusk:/app/.tusk
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
"""
3+
E2E Test Entrypoint for HTTPX Instrumentation
4+
5+
This script orchestrates the full e2e test lifecycle:
6+
1. Setup: Install dependencies
7+
2. Record: Start app in RECORD mode, execute requests
8+
3. Test: Run Tusk CLI tests
9+
4. Teardown: Cleanup and return exit code
10+
"""
11+
12+
import sys
13+
from pathlib import Path
14+
15+
# Add SDK to path for imports
16+
sys.path.insert(0, "/sdk")
17+
18+
from drift.instrumentation.e2e_common.base_runner import E2ETestRunnerBase
19+
20+
21+
class HttpxE2ETestRunner(E2ETestRunnerBase):
22+
"""E2E test runner for HTTPX instrumentation."""
23+
24+
def __init__(self):
25+
import os
26+
27+
port = int(os.getenv("PORT", "8000"))
28+
super().__init__(app_port=port)
29+
30+
31+
if __name__ == "__main__":
32+
runner = HttpxE2ETestRunner()
33+
exit_code = runner.run()
34+
sys.exit(exit_code)
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-e /sdk
2+
Flask>=3.1.2
3+
httpx>=0.28.1
4+
requests>=2.32.5
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/bin/bash
2+
3+
# Exit on error
4+
set -e
5+
6+
# Accept optional port parameter (default: 8000)
7+
APP_PORT=${1:-8000}
8+
export APP_PORT
9+
10+
# Generate unique docker compose project name
11+
# Get the instrumentation name (parent directory of e2e-tests)
12+
TEST_NAME="$(basename "$(dirname "$(pwd)")")"
13+
PROJECT_NAME="python-${TEST_NAME}-${APP_PORT}"
14+
15+
# Colors for output
16+
GREEN='\033[0;32m'
17+
RED='\033[0;31m'
18+
YELLOW='\033[1;33m'
19+
BLUE='\033[0;34m'
20+
NC='\033[0m'
21+
22+
echo -e "${BLUE}========================================${NC}"
23+
echo -e "${BLUE}Running Python E2E Test: ${TEST_NAME}${NC}"
24+
echo -e "${BLUE}Port: ${APP_PORT}${NC}"
25+
echo -e "${BLUE}========================================${NC}"
26+
echo ""
27+
28+
# Cleanup function
29+
cleanup() {
30+
echo ""
31+
echo -e "${YELLOW}Cleaning up containers...${NC}"
32+
docker compose -p "$PROJECT_NAME" down -v 2>/dev/null || true
33+
}
34+
35+
# Register cleanup on exit
36+
trap cleanup EXIT
37+
38+
# Build containers
39+
echo -e "${BLUE}Building containers...${NC}"
40+
docker compose -p "$PROJECT_NAME" build --no-cache
41+
42+
# Run the test container
43+
echo -e "${BLUE}Starting test...${NC}"
44+
echo ""
45+
46+
# Run container and capture exit code (always use port 8000 inside container)
47+
# Disable set -e temporarily to capture exit code
48+
set +e
49+
docker compose -p "$PROJECT_NAME" run --rm app
50+
EXIT_CODE=$?
51+
set -e
52+
53+
echo ""
54+
if [ $EXIT_CODE -eq 0 ]; then
55+
echo -e "${GREEN}========================================${NC}"
56+
echo -e "${GREEN}Test passed!${NC}"
57+
echo -e "${GREEN}========================================${NC}"
58+
else
59+
echo -e "${RED}========================================${NC}"
60+
echo -e "${RED}Test failed with exit code ${EXIT_CODE}${NC}"
61+
echo -e "${RED}========================================${NC}"
62+
fi
63+
64+
exit $EXIT_CODE

0 commit comments

Comments
 (0)