PyBreaker’s listener system lets you hook into circuit breaker lifecycle events without modifyingDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/danielfm/pybreaker/llms.txt
Use this file to discover all available pages before exploring further.
CircuitBreaker itself. Attach one or more CircuitBreakerListener subclasses to receive callbacks whenever a call is attempted, succeeds, fails, or causes a state transition. This is the recommended way to integrate circuit breaker telemetry with logging frameworks, metrics systems, and alerting pipelines.
The CircuitBreakerListener Interface
SubclassCircuitBreakerListener and override only the methods you need. The base class provides no-op implementations for all four callbacks, so partial implementations are fully supported.
| Callback | When it fires | Key parameters |
|---|---|---|
before_call | Immediately before each protected call | cb — the breaker; func — the callable; forwarded args/kwargs |
failure | When a call raises a system error (not an excluded exception) | cb — the breaker; exc — the exception instance |
success | When a protected call returns without raising | cb — the breaker |
state_change | When the circuit transitions between states | cb — the breaker; old_state — previous state object; new_state — new state object |
Example: Logging Listener
A logging listener is the simplest useful implementation — it records state changes and call outcomes to the standard Python logging system.Example: Metrics Listener
A metrics listener forwards circuit breaker events to an external monitoring system. The example below targets StatsD, but the same pattern applies to Prometheus counters, DataDog custom metrics, or any other backend.Registering Listeners
You can register listeners at circuit breaker creation time, or add them later after the breaker is already in use.Removing Listeners
Hold a reference to a listener instance to remove it later. You can also inspect all currently registered listeners via thelisteners property.
Multiple listeners are fully supported. PyBreaker calls all registered listeners in the order they were added. Each listener receives the same event independently, so a failure in one listener’s callback does not prevent the others from being notified.
