Client
provide.foundation.logger.otlp.client
¶
Generic OTLP client for any OpenTelemetry-compatible backend.
Provides OTLPLogClient for sending logs via OTLP to any compatible backend (OpenObserve, Datadog, Honeycomb, etc.).
Classes¶
OTLPLogClient
¶
OTLPLogClient(
endpoint: str,
headers: dict[str, str] | None = None,
service_name: str = "foundation",
service_version: str | None = None,
environment: str | None = None,
timeout: float = 30.0,
use_circuit_breaker: bool = True,
)
Generic OTLP client for any OpenTelemetry-compatible backend.
This client works with any OTLP-compatible backend and provides: - Single log sending with automatic flushing - Persistent LoggerProvider for continuous logging - Circuit breaker pattern for reliability - Automatic trace context extraction - Attribute normalization for OTLP compatibility
Examples:
>>> client = OTLPLogClient(
... endpoint="https://api.honeycomb.io/v1/logs",
... headers={"x-honeycomb-team": "YOUR_API_KEY"},
... service_name="my-service",
... )
>>> client.send_log("Hello OTLP!", level="INFO")
True
>>> # Use with persistent logger provider
>>> provider = client.create_logger_provider()
>>> # Configure structlog to use provider
Initialize OTLP client.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
OTLP endpoint URL (e.g., "https://api.example.com/v1/logs") |
required |
headers
|
dict[str, str] | None
|
Optional custom headers (auth, organization, etc.) |
None
|
service_name
|
str
|
Service name for resource attributes |
'foundation'
|
service_version
|
str | None
|
Optional service version |
None
|
environment
|
str | None
|
Optional environment (dev, staging, prod) |
None
|
timeout
|
float
|
Request timeout in seconds |
30.0
|
use_circuit_breaker
|
bool
|
Enable circuit breaker pattern |
True
|
Source code in provide/foundation/logger/otlp/client.py
Functions¶
create_logger_provider
¶
Create persistent LoggerProvider for continuous logging.
Returns:
| Type | Description |
|---|---|
Any | None
|
LoggerProvider if OpenTelemetry SDK available, None otherwise |
Use this for long-running applications that need persistent OTLP logging. The provider can be used with structlog processors for automatic OTLP export.
Circuit breaker: - Returns None if circuit is open - Records success if provider created - Records failure if exception occurs
Examples:
>>> provider = client.create_logger_provider()
>>> if provider:
... # Configure structlog with provider
... pass
Source code in provide/foundation/logger/otlp/client.py
from_config
classmethod
¶
Create client from TelemetryConfig.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
Any
|
TelemetryConfig instance |
required |
additional_headers
|
dict[str, str] | None
|
Additional headers to merge with config headers |
None
|
Returns:
| Type | Description |
|---|---|
OTLPLogClient
|
Configured OTLPLogClient instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If config.otlp_endpoint is not set |
Examples:
>>> from provide.foundation.logger.config.telemetry import TelemetryConfig
>>> config = TelemetryConfig.from_env()
>>> client = OTLPLogClient.from_config(config)
Source code in provide/foundation/logger/otlp/client.py
get_stats
¶
Get client statistics including circuit breaker state.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with client and circuit breaker statistics |
Examples:
Source code in provide/foundation/logger/otlp/client.py
is_available
¶
Check if OTLP is available (SDK installed and circuit not open).
Returns:
| Type | Description |
|---|---|
bool
|
True if OTLP is available and circuit is closed |
Examples:
Source code in provide/foundation/logger/otlp/client.py
send_log
¶
Send single log via OTLP.
Creates a temporary LoggerProvider, sends the log, and flushes immediately. This ensures delivery for single log sends but is less efficient for bulk logging.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Log message |
required |
level
|
str
|
Log level (DEBUG, INFO, WARN, ERROR, FATAL) |
'INFO'
|
attributes
|
dict[str, Any] | None
|
Optional log attributes |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if sent successfully, False otherwise |
Circuit breaker pattern: - Checks circuit before attempting - Records success/failure - Automatically disables after threshold failures - Auto-recovers with exponential backoff
Examples:
Source code in provide/foundation/logger/otlp/client.py
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |