This API requires the
otel feature flag. Add features = ["otel"] to your Cargo.toml:Overview
The III SDK includes comprehensive OpenTelemetry support for distributed tracing, metrics collection, and structured logging. When theotel feature is enabled, telemetry data is exported to the III Engine over a shared WebSocket connection.
Initialization
init_otel
Initialize OpenTelemetry with the given configuration.OtelConfig
required
Configuration for OpenTelemetry including service name, metrics settings, etc.
When using
III::set_otel_config() and connect(), OpenTelemetry is automatically initialized. You only need to call init_otel() directly if you’re not using the III client.shutdown_otel
Shutdown OpenTelemetry and flush all pending data.flush_otel
Flush all pending telemetry data without shutting down.is_initialized
Check if OpenTelemetry has been initialized.Configuration
OtelConfig
Configuration structure for OpenTelemetry.enabled:true(can be overridden byOTEL_ENABLEDenv var)service_name:"iii-rust-sdk"(can be overridden byOTEL_SERVICE_NAMEenv var)service_version: SDK version fromCargo.tomlservice_instance_id: Random UUIDengine_ws_url: III client address orws://localhost:49134metrics_enabled:truemetrics_export_interval_ms:60000(1 minute)logs_enabled:trueshutdown_timeout_ms:10000(10 seconds)channel_capacity:10000fetch_instrumentation_enabled:true
ReconnectionConfig
Configuration for WebSocket reconnection behavior.initial_delay_ms:1000max_delay_ms:30000backoff_multiplier:2.0jitter_factor:0.3max_retries:None(infinite)max_pending_messages:1000
Distributed Tracing
get_tracer
Get a tracer for creating spans manually.with_span
Execute a function within a traced span with automatic error handling.&str
required
Name of the span
Option<&str>
W3C traceparent header to set parent context
Option<SpanKind>
Span kind (defaults to
Internal)F
required
Async function to execute within the span
Automatic Trace Propagation
Trace context is automatically propagated across function calls:Trace Context Functions
current_trace_id
Get the current trace ID.current_span_id
Get the current span ID.inject_traceparent
Inject current trace context into a W3C traceparent header.extract_traceparent
Extract trace context from a W3C traceparent header.Baggage
Baggage allows you to propagate key-value pairs across service boundaries.set_baggage_entry
Set a baggage entry.get_baggage_entry
Get a baggage entry.get_all_baggage
Get all baggage entries.Metrics
get_meter
Get a meter for creating metrics.Metric Types
OpenTelemetry provides several metric types: Counter:HTTP Instrumentation
execute_traced_request
Execute an HTTP request with automatic tracing.reqwest::Request
required
HTTP request to execute
HTTP instrumentation automatically injects
traceparent and baggage headers into outbound requests.Logging
When theotel feature is enabled, the Logger automatically emits OpenTelemetry LogRecords:
logger.debug(message, data)logger.info(message, data)logger.warn(message, data)logger.error(message, data)
Complete Example
Resource Attributes
The SDK automatically adds these resource attributes:service.name: Service name from config orOTEL_SERVICE_NAMEservice.version: Service version from config orSERVICE_VERSIONservice.instance.id: Service instance ID (UUID)service.namespace: Optional namespace from configtelemetry.sdk.name:"iii-rust-sdk"telemetry.sdk.language:"rust"telemetry.sdk.version: SDK version
See Also
- Context API - Access logger in function handlers
- Client API - Configure OTel when creating III client
- Invocation API - Automatic trace propagation in function calls