Skip to main content

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 via get_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
When OpenTelemetry is not initialized:
  • 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).
This is primarily used internally by the SDK to inject context into function handlers. You typically don’t need to call this directly.
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

The Logger class automatically:
  1. Detects OTel initialization: Uses is_initialized() to check if OTel is active
  2. 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)
  3. Falls back to Python logging: When OTel is not active, uses Python’s logging module
  4. Severity mapping:
    • debugDEBUG (5)
    • infoINFO (9)
    • warnWARN (13)
    • errorERROR (17)

Best Practices

  1. Use structured data: Pass dictionaries to the data parameter instead of formatting strings
  2. Include context: Add relevant identifiers (user_id, order_id, etc.) to logs
  3. Appropriate levels: Use info for business logic, debug for diagnostics, error for failures
  4. Avoid sensitive data: Don’t log passwords, tokens, or PII
  5. Measure performance: Log durations for slow operations
  6. Trace correlation: Logs automatically include trace context when OTel is active