Tracing Classes¶
TraceHook¶
Protocol for custom trace hooks. Implement this to create your own tracing backend.
Methods:
on_enter(name: str, ctx: Any, depth: int) -> Any-- Called before a combinator runs. Return a span token.on_exit(span: Any, name: str, ok: bool, duration_ms: float, depth: int) -> None-- Called after a combinator completes.on_error(span: Any, name: str, error: Exception, duration_ms: float, depth: int) -> None-- Called if a combinator raises (optional).
Example:
class MyHook:
def on_enter(self, name, ctx, depth):
return time.time()
def on_exit(self, span, name, ok, duration_ms, depth):
print(f"{name}: {'OK' if ok else 'FAIL'} ({duration_ms:.2f}ms)")
def on_error(self, span, name, error, duration_ms, depth):
print(f"{name}: ERROR {error}")
TraceConfig¶
@dataclass
class TraceConfig:
include_leaf_only: bool = False
max_depth: int | None = None
nested: bool = True
Configuration for tracing behavior.
Fields:
include_leaf_only-- Only trace leaf predicates, skip AND/OR/NOT containersmax_depth-- Maximum nesting depth to tracenested-- Whether to trace nested combinators (setFalsefor top-level only)
PrintHook¶
Built-in hook that prints trace output to stdout.
Constructor:
LoggingHook¶
Built-in hook that uses Python's logging module.
Constructor:
OpenTelemetryHook¶
Hook that creates OpenTelemetry spans for each combinator.
Requires the opentelemetry extra: pip install kompoz[opentelemetry]
Constructor:
Example: