Skip to main content

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:
This installs:
  • opentelemetry-api>=1.25
  • opentelemetry-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/off
str
default:"iii-python-sdk"
Service name. Uses OTEL_SERVICE_NAME env var if set
str
default:"unknown"
Service version. Uses SERVICE_VERSION env var if set
str
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 set
bool
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 when fetch_instrumentation_enabled=True:
Spans include attributes:
  • http.request.method
  • url.full
  • server.address
  • url.scheme
  • url.path
  • server.port
  • http.response.status_code
  • http.request.body.size
  • http.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

Log records include:
  • 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 to false, 0, no, or off to disable OTel
  • OTEL_SERVICE_NAME: Default service name
  • SERVICE_VERSION: Default service version
  • III_BRIDGE_URL: III Engine WebSocket URL (default: ws://localhost:49134)

Best Practices

  1. Initialize early: Call init_otel() before connecting to the III Engine
  2. Use context: Access logger via get_context() for automatic tracing
  3. Meaningful names: Use descriptive span names and metric names
  4. Attributes: Add relevant attributes to spans for filtering and analysis
  5. Error handling: Always set span status and record exceptions
  6. Cleanup: Call shutdown_otel_async() on graceful shutdown
  7. 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 EngineMetricsExporter every 60 seconds
  • Logs: Exported via EngineLogExporter
The engine aggregates telemetry from all workers and provides a unified observability view.