Skip to content

Commit 67157dc

Browse files
authored
feat: urllib3 instrumentation (#30)
1 parent d541f01 commit 67157dc

File tree

13 files changed

+1893
-0
lines changed

13 files changed

+1893
-0
lines changed

drift/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from .instrumentation.fastapi import FastAPIInstrumentation
3333
from .instrumentation.flask import FlaskInstrumentation
3434
from .instrumentation.requests import RequestsInstrumentation
35+
from .instrumentation.urllib3 import Urllib3Instrumentation
3536

3637
__version__ = "0.1.0"
3738

@@ -61,6 +62,7 @@
6162
"FlaskInstrumentation",
6263
"FastAPIInstrumentation",
6364
"RequestsInstrumentation",
65+
"Urllib3Instrumentation",
6466
# Adapters
6567
"SpanExportAdapter",
6668
"ExportResult",

drift/core/drift_sdk.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,16 @@ def _init_auto_instrumentations(self) -> None:
402402
except ImportError:
403403
pass
404404

405+
try:
406+
import urllib3 # type: ignore[unresolved-import]
407+
408+
from ..instrumentation.urllib3 import Urllib3Instrumentation
409+
410+
_ = Urllib3Instrumentation()
411+
logger.debug("urllib3 instrumentation initialized")
412+
except ImportError:
413+
pass
414+
405415
# Initialize PostgreSQL instrumentation before Django
406416
# Instrument BOTH psycopg2 and psycopg if available
407417
# This allows apps to use either or both
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"""urllib3 instrumentation module."""
2+
3+
from .instrumentation import RequestDroppedByTransform, Urllib3Instrumentation
4+
5+
__all__ = ["Urllib3Instrumentation", "RequestDroppedByTransform"]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: 1
2+
3+
service:
4+
id: "urllib3-e2e-test-id"
5+
name: "urllib3-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
28+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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/urllib3/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"]
22+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
services:
2+
app:
3+
build:
4+
context: ../../../..
5+
dockerfile: drift/instrumentation/urllib3/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 SDK source for hot reload (no rebuild needed for SDK changes)
15+
- ../../../..:/sdk
16+
# Mount app source for development
17+
- ./src:/app/src
18+
# Mount .tusk folder to persist traces
19+
- ./.tusk:/app/.tusk
20+
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 urllib3 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 Urllib3E2ETestRunner(E2ETestRunnerBase):
22+
"""E2E test runner for urllib3 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 = Urllib3E2ETestRunner()
33+
exit_code = runner.run()
34+
sys.exit(exit_code)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-e /sdk
2+
Flask>=3.1.2
3+
urllib3>=2.0.0
4+
requests>=2.32.5
5+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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
65+

0 commit comments

Comments
 (0)