Helpers
provide.foundation.logger.otlp.helpers
¶
Generic OTLP helper functions for trace context, endpoints, and log formatting.
Provides utility functions for working with OTLP/OpenTelemetry including: - Trace context extraction - Endpoint URL building - Header construction - Attribute normalization
Functions¶
add_trace_context_to_attributes
¶
Add trace context to attributes dict (modifies in place).
Extracts trace context and adds trace_id/span_id to attributes. Safe to call even if no trace context is available (no-op).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attributes
|
dict[str, Any]
|
Dictionary to add trace context to (modified in place) |
required |
Examples:
>>> attrs = {"key": "value"}
>>> add_trace_context_to_attributes(attrs)
>>> # attrs may now include 'trace_id' and 'span_id' if context available
Source code in provide/foundation/logger/otlp/helpers.py
build_otlp_endpoint
¶
Build OTLP endpoint URL for specific signal type.
Constructs the full OTLP endpoint URL for the given signal type. Handles trailing slashes and is idempotent (won't double-add paths).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_endpoint
|
str
|
Base OTLP endpoint (e.g., "https://api.example.com") |
required |
signal_type
|
str
|
"logs", "traces", or "metrics" |
'logs'
|
Returns:
| Type | Description |
|---|---|
str
|
Full endpoint URL (e.g., "https://api.example.com/v1/logs") |
Examples:
Source code in provide/foundation/logger/otlp/helpers.py
build_otlp_headers
¶
build_otlp_headers(
base_headers: dict[str, str] | None = None,
auth_token: str | None = None,
) -> dict[str, str]
Build OTLP headers with optional authentication.
Creates headers dictionary with OTLP-required headers and optional auth.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_headers
|
dict[str, str] | None
|
Base headers to include |
None
|
auth_token
|
str | None
|
Optional bearer token for authentication |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Complete headers dict with Content-Type and auth |
Examples:
>>> build_otlp_headers(auth_token="secret123")
{'Content-Type': 'application/x-protobuf', 'Authorization': 'Bearer secret123'}
>>> build_otlp_headers(base_headers={"X-Custom": "value"})
{'X-Custom': 'value', 'Content-Type': 'application/x-protobuf'}
Source code in provide/foundation/logger/otlp/helpers.py
extract_trace_context
¶
Extract current trace context from OpenTelemetry.
Extracts trace context from OpenTelemetry if SDK is available and a valid span is recording.
Returns:
| Type | Description |
|---|---|
dict[str, str] | None
|
Dict with 'trace_id' and 'span_id', or None if not available |
Examples:
Source code in provide/foundation/logger/otlp/helpers.py
normalize_attributes
¶
Normalize attribute values for OTLP compatibility.
Converts non-serializable types to OTLP-compatible values: - Non-serializable types → strings - Nested dicts → JSON strings - Lists → JSON strings - None values → empty strings
Returns new dict (doesn't modify input).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
attributes
|
dict[str, Any]
|
Dictionary of attributes to normalize |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
New dictionary with normalized values |
Examples: