Circuit
provide.foundation.logger.otlp.circuit
¶
Circuit breaker pattern for OTLP connection failures.
This prevents log spam when OTLP endpoint is unreachable by: - Tracking failure counts and timestamps - Automatically disabling OTLP after threshold failures - Implementing exponential backoff before retry attempts - Auto-recovering after cooldown period
This generic implementation works with any OTLP-compatible backend.
Classes¶
OTLPCircuitBreaker
¶
OTLPCircuitBreaker(
failure_threshold: int = 5,
timeout: float = 60.0,
half_open_timeout: float = 10.0,
)
Circuit breaker for OTLP connections with exponential backoff.
States
- closed: Normal operation, requests allowed
- open: Too many failures, requests blocked
- half_open: Testing if service recovered
Examples:
>>> breaker = OTLPCircuitBreaker(failure_threshold=3, timeout=60.0)
>>> if breaker.can_attempt():
... success = send_otlp_log()
... if success:
... breaker.record_success()
... else:
... breaker.record_failure()
Initialize circuit breaker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
failure_threshold
|
int
|
Number of failures before opening circuit |
5
|
timeout
|
float
|
Seconds to wait before attempting half-open (doubles each time) |
60.0
|
half_open_timeout
|
float
|
Seconds to wait in half-open before trying again |
10.0
|
Source code in provide/foundation/logger/otlp/circuit.py
Attributes¶
Functions¶
can_attempt
¶
Check if we can attempt an OTLP operation.
Returns:
| Type | Description |
|---|---|
bool
|
True if operation should be attempted, False if circuit is open |
Source code in provide/foundation/logger/otlp/circuit.py
get_stats
¶
Get circuit breaker statistics.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with current state and statistics |
Source code in provide/foundation/logger/otlp/circuit.py
record_failure
¶
Record a failed operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception | None
|
Optional exception that caused the failure |
None
|
Source code in provide/foundation/logger/otlp/circuit.py
record_success
¶
Record a successful operation.
Source code in provide/foundation/logger/otlp/circuit.py
reset
¶
Manually reset the circuit breaker to closed state.
Source code in provide/foundation/logger/otlp/circuit.py
Functions¶
get_otlp_circuit_breaker
¶
Get the global OTLP circuit breaker instance.
Returns:
| Type | Description |
|---|---|
OTLPCircuitBreaker
|
Shared OTLPCircuitBreaker instance |
Source code in provide/foundation/logger/otlp/circuit.py
reset_otlp_circuit_breaker
¶
Reset the global circuit breaker (primarily for testing).