Official Python SDK for Surfa Analytics - Ingest live traffic events
Project description
Surfa Ingest SDK
Official Python SDK for ingesting live traffic events to Surfa Analytics.
Installation
pip install surfa-ingest
Quick Start
from surfa_ingest import SurfaClient
# Initialize client with your ingest key
client = SurfaClient(ingest_key="sk_live_your_key_here")
# Track events
client.track({
"kind": "tool",
"subtype": "call_started",
"tool_name": "search_web",
"args": {"query": "AI news"}
})
client.track({
"kind": "tool",
"subtype": "call_completed",
"tool_name": "search_web",
"status": "success",
"latency_ms": 234
})
# Flush events to API
client.flush()
Context Manager (Recommended)
Use the context manager to automatically track session lifecycle:
from surfa_ingest import SurfaClient
with SurfaClient(ingest_key="sk_live_your_key_here") as client:
# Session automatically started
client.track({
"kind": "tool",
"subtype": "call_started",
"tool_name": "search_web"
})
# Session automatically ended and events flushed on exit
Configuration
client = SurfaClient(
ingest_key="sk_live_your_key_here",
api_url="https://api.surfa.dev", # Default: http://localhost:3000
flush_at=25, # Auto-flush after 25 events
timeout_s=10, # HTTP timeout in seconds
)
Set Runtime Metadata
Track which AI runtime is being used:
client = SurfaClient(ingest_key="sk_live_...")
client.set_runtime(
provider="anthropic",
model="claude-sonnet-4-5",
mode="messages"
)
Event Types
Tool Events
# Tool call started
client.track({
"kind": "tool",
"subtype": "call_started",
"tool_name": "search_web",
"direction": "request",
"args": {"query": "Python tutorials"}
})
# Tool call completed
client.track({
"kind": "tool",
"subtype": "call_completed",
"tool_name": "search_web",
"direction": "response",
"status": "success",
"latency_ms": 234,
"results": [{"title": "Learn Python", "url": "..."}]
})
Session Events
# Session started
client.session_started()
# Session ended
client.session_ended()
Runtime Events
# LLM request
client.track({
"kind": "runtime",
"subtype": "llm_request",
"direction": "outbound",
"messages": [{"role": "user", "content": "Hello"}],
"temperature": 0.7
})
Event Fields
Required Fields
kind(str): Event type (e.g., "tool", "session", "runtime")
Optional Fields
subtype(str): Event subtype (e.g., "call_started", "session_ended")tool_name(str): Name of the toolstatus(str): Status (e.g., "success", "error")direction(str): Direction (e.g., "request", "response")method(str): HTTP method or similarcorrelation_id(str): Correlation ID for pairing eventsspan_parent_id(str): Parent span ID for tracinglatency_ms(int): Latency in millisecondsts(str): Timestamp (ISO 8601 format, auto-generated if not provided)- Any additional fields will be included in the event payload
Auto-Flush
Events are automatically flushed when:
- Buffer reaches
flush_atevents (default: 25) - Context manager exits
flush()is called explicitly
Error Handling
from surfa_ingest import SurfaClient, SurfaConfigError, SurfaValidationError
try:
client = SurfaClient(ingest_key="invalid_key")
except SurfaConfigError as e:
print(f"Configuration error: {e}")
try:
client.track({"invalid": "event"}) # Missing 'kind'
except SurfaValidationError as e:
print(f"Validation error: {e}")
Logging
The SDK uses Python's standard logging module:
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger("surfa_ingest")
Development Status
Current Version: 0.1.0 (Alpha)
This SDK is in active development. The API may change in future versions.
Implemented
- ✅ Client initialization
- ✅ Event buffering
- ✅ Session management
- ✅ Context manager support
- ✅ Event validation
- ✅ Runtime metadata
Coming Soon
- 🔜 HTTP API integration
- 🔜 Automatic retry logic
- 🔜 Background flushing
- 🔜 Async support
License
MIT
Support
- Documentation: https://docs.surfa.dev
- Issues: https://github.com/yourusername/surfa/issues
- Email: support@surfa.dev
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file surfa_ingest-0.1.0.tar.gz.
File metadata
- Download URL: surfa_ingest-0.1.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b95f05f5af24d4590ab8868ad7b2ba6a54da3cfdf4231f154a4d7a463cab0d4
|
|
| MD5 |
59c8ebaa284572da590d3e9ee59c9681
|
|
| BLAKE2b-256 |
fe2f91d21c4eb3aeb42f86fb6c921724ae53bdb10d7111e06ddbf8ab92ba3577
|
File details
Details for the file surfa_ingest-0.1.0-py3-none-any.whl.
File metadata
- Download URL: surfa_ingest-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b97bc47742a1cc502461de58fbb818b089269d50a3ca71acc82f0edc6404fa06
|
|
| MD5 |
587b5b78378608980d0b0d916aaec53c
|
|
| BLAKE2b-256 |
76afee00fdfb27496e672933328cf36bb2060c198a26ee4c8e230e2fa0f8cdfb
|