Overview
The III SDK provides execution context that includes a logger and optional OpenTelemetry span. Context is automatically injected into function handlers and can be accessed viaget_context().
Context
Execution context containing logger and tracing utilities.Logger
Context-aware logger that emits OpenTelemetry LogRecords
Any | None
The active OpenTelemetry span for adding custom attributes, events, etc.
Accessing Context
get_context
Get the current execution context within a function handler.Context
The current execution context, or a default context if none is set
Default Context
If no context is active,get_context() returns a default context with a basic logger:
Logger
Context-aware logger that emits OpenTelemetry LogRecords when OTel is active.Log Methods
str
required
Log message
Any
Additional structured data to include in the log record
Logger Behavior
When OpenTelemetry is initialized:- Logs are emitted as OpenTelemetry LogRecords
- Includes trace context (span ID, trace ID)
- Exported to the III Engine
- Function name is automatically included
- Falls back to Python’s standard logging module
- Logs to console/file handlers as configured
- Still includes function name and data
Function Name
The logger automatically includes the function name:with_context
Execute a function within a specific context (internal use).Callable[[Context], Awaitable[T]]
required
Async function to execute with the context
Context
required
The context to use
T
The function’s return value
Example: Structured Logging
Example: Logging with Trace Context
Example: Error Tracking
Example: Request/Response Logging
Logger Implementation Details
TheLogger class automatically:
- Detects OTel initialization: Uses
is_initialized()to check if OTel is active - Emits LogRecords: When OTel is active, creates OpenTelemetry LogRecords with:
- Timestamp (nanoseconds)
- Severity level and text
- Message body
- Function name attribute
- Custom data attribute
- Trace context (span ID, trace ID, trace flags)
- Falls back to Python logging: When OTel is not active, uses Python’s logging module
- Severity mapping:
debug→DEBUG(5)info→INFO(9)warn→WARN(13)error→ERROR(17)
Best Practices
- Use structured data: Pass dictionaries to the
dataparameter instead of formatting strings - Include context: Add relevant identifiers (user_id, order_id, etc.) to logs
- Appropriate levels: Use info for business logic, debug for diagnostics, error for failures
- Avoid sensitive data: Don’t log passwords, tokens, or PII
- Measure performance: Log durations for slow operations
- Trace correlation: Logs automatically include trace context when OTel is active