Overview
The III SDK provides built-in OpenTelemetry support for distributed tracing, metrics, and logging. When enabled, telemetry data is automatically exported to the III Engine.Installation
Install the SDK with OpenTelemetry support:opentelemetry-api>=1.25opentelemetry-sdk>=1.25
Initialization
init_otel
Initialize OpenTelemetry with automatic engine integration.OtelConfig
OpenTelemetry configuration. If omitted, uses defaults.
asyncio.AbstractEventLoop
Running event loop. When provided, the connection starts immediately. When None, it starts lazily on first use.
OtelConfig
Configuration for OpenTelemetry initialization:bool
default:true
Enable OpenTelemetry. Defaults to True unless
OTEL_ENABLED=false/0/no/offstr
default:"iii-python-sdk"
Service name. Uses
OTEL_SERVICE_NAME env var if setstr
default:"unknown"
Service version. Uses
SERVICE_VERSION env var if setstr
Service namespace for grouping related services
str
Unique instance ID. Defaults to a random UUID
str
default:"ws://localhost:49134"
III Engine WebSocket URL. Uses
III_BRIDGE_URL env var if setbool
default:true
Auto-instrument urllib HTTP calls
bool
default:true
Enable OpenTelemetry log export
bool
default:true
Enable OpenTelemetry metrics export
int
default:60000
Metrics export interval in milliseconds (60 seconds)
Distributed Tracing
get_tracer
Get the active OpenTelemetry tracer.Tracer | None
The active tracer, or None if OTel is not initialized
Automatic Trace Propagation
Trace context is automatically propagated across function calls:Custom Span Attributes
HTTP Instrumentation
Urllib HTTP requests are automatically instrumented whenfetch_instrumentation_enabled=True:
http.request.methodurl.fullserver.addressurl.schemeurl.pathserver.porthttp.response.status_codehttp.request.body.sizehttp.response.body.size
Metrics
get_meter
Get the active OpenTelemetry meter.Meter | None
The active meter, or None if OTel metrics are not initialized
Counter
Histogram
Gauge
Logging
Logger
The SDK provides a context-aware logger that emits OpenTelemetry LogRecords:Log Levels
- Timestamp
- Severity level
- Message body
- Function name (if available)
- Trace context (span ID, trace ID)
- Custom attributes
Fallback to Python Logging
If OTel is not initialized, logs fallback to standard Python logging:Shutdown
shutdown_otel
Shut down OpenTelemetry synchronously (best-effort):shutdown_otel_async
Shut down OpenTelemetry and await WebSocket connection close:is_initialized
Check if OpenTelemetry has been initialized:bool
True if OTel has been successfully initialized
Example: Full Observability
Environment Variables
The SDK respects these environment variables:OTEL_ENABLED: Set tofalse,0,no, oroffto disable OTelOTEL_SERVICE_NAME: Default service nameSERVICE_VERSION: Default service versionIII_BRIDGE_URL: III Engine WebSocket URL (default:ws://localhost:49134)
Best Practices
- Initialize early: Call
init_otel()before connecting to the III Engine - Use context: Access logger via
get_context()for automatic tracing - Meaningful names: Use descriptive span names and metric names
- Attributes: Add relevant attributes to spans for filtering and analysis
- Error handling: Always set span status and record exceptions
- Cleanup: Call
shutdown_otel_async()on graceful shutdown - Sampling: Use OTel’s built-in sampling for high-volume services
Integration with III Engine
Telemetry data is automatically exported to the III Engine via WebSocket:- Traces: Exported via
EngineSpanExporter - Metrics: Exported via
EngineMetricsExporterevery 60 seconds - Logs: Exported via
EngineLogExporter