Hooks and Metrics API¶
Observe the transition lifecycle and collect metrics without modifying guards or actions.
See Hooks and Metrics guide for the full guide.
Protocol¶
TransitionHook
¶
Bases: Protocol
Protocol for transition lifecycle hooks.
Hooks are called in the following order:
on_before_process— before any processing (orchestrator level).on_transition_start— a candidate transition is about to fire.on_transition_complete— transition finished (success or fail).on_transition_error— an unhandled exception occurred.
Data classes¶
TransitionMetrics
dataclass
¶
TransitionMetrics(source_state: str, target_state: str, trigger: str, success: bool, duration_ms: float, guards_evaluated: int = 0, guards_passed: int = 0, timestamp: datetime = (lambda: now(UTC))(), machine_name: str = 'unknown', metadata: dict[str, Any] = dict())
Metrics for a single transition.
Built-in hooks¶
LoggingHook
¶
Logs transition details.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_level
|
int
|
Logging level for transition messages. |
DEBUG
|
logger_name
|
str
|
Name of the logger to use. |
'pystator.transitions'
|
Source code in src/pystator/hooks.py
on_before_process
¶
on_before_process(entity_id: str, current_state: str, trigger: str, context: dict[str, Any]) -> None
Log the start of event processing.
Source code in src/pystator/hooks.py
on_transition_start
¶
Log the start of a transition.
Source code in src/pystator/hooks.py
on_transition_complete
¶
on_transition_complete(result: TransitionResult, duration_ms: float, context: dict[str, Any]) -> None
Log the completion of a transition.
Source code in src/pystator/hooks.py
on_transition_error
¶
on_transition_error(error: Exception, current_state: str, trigger: str, context: dict[str, Any]) -> None
Log a transition error with full traceback.
Source code in src/pystator/hooks.py
MetricsCollector
¶
Collects and aggregates transition metrics.
Thread-safe with bounded memory. The max_history parameter controls
the maximum number of transition records and duration samples kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_history
|
int
|
Maximum number of transition records to retain. |
10000
|
Source code in src/pystator/hooks.py
on_before_process
¶
on_transition_start
¶
on_transition_complete
¶
on_transition_complete(result: TransitionResult, duration_ms: float, context: dict[str, Any]) -> None
Record transition metrics on completion.
Source code in src/pystator/hooks.py
on_transition_error
¶
on_transition_error(error: Exception, current_state: str, trigger: str, context: dict[str, Any]) -> None
Increment the error counter.
get_summary
¶
Return aggregated metrics snapshot.
Source code in src/pystator/hooks.py
reset
¶
Clear all accumulated metrics.
Source code in src/pystator/hooks.py
Observer¶
TransitionObserver
¶
Manages transition lifecycle hooks and provides timing.
Per-thread timing state avoids races when concurrent transitions overwrite each other's start times.
Source code in src/pystator/hooks.py
add_hook
¶
add_hook(hook: TransitionHook) -> TransitionObserver
remove_hook
¶
remove_hook(hook: TransitionHook) -> None
before_process
¶
Dispatch on_before_process to all hooks.
Call this at the orchestrator level before any work begins.
Source code in src/pystator/hooks.py
before_transition
¶
Record start time and dispatch on_transition_start to all hooks.
Source code in src/pystator/hooks.py
after_transition
¶
Compute duration and dispatch on_transition_complete to all hooks.
Source code in src/pystator/hooks.py
on_error
¶
Dispatch on_transition_error to all hooks.
Source code in src/pystator/hooks.py
Import¶
from pystator.hooks import (
TransitionHook,
TransitionMetrics,
LoggingHook,
MetricsCollector,
TransitionObserver,
)
See also¶
- Hooks and Metrics guide
- Orchestrator —
hooksparameter