|
| 1 | +"""Callback-based observability sink. |
| 2 | +
|
| 3 | +Routes events, logs, and traces to a user-supplied callback function, |
| 4 | +enabling integration with custom dashboards, message queues, or alerting |
| 5 | +systems without subclassing. |
| 6 | +
|
| 7 | +Usage:: |
| 8 | +
|
| 9 | + from modelmesh.cdk import CallbackObservability, CallbackObservabilityConfig |
| 10 | +
|
| 11 | + def on_event(event): |
| 12 | + my_dashboard.send(event.event_type, event.model_id, event.timestamp) |
| 13 | +
|
| 14 | + obs = CallbackObservability(CallbackObservabilityConfig( |
| 15 | + callback=on_event, |
| 16 | + )) |
| 17 | +""" |
| 18 | +from __future__ import annotations |
| 19 | + |
| 20 | +from dataclasses import dataclass, field |
| 21 | +from typing import Any, Callable, Optional |
| 22 | + |
| 23 | +from modelmesh.cdk.base_observability import ( |
| 24 | + BaseObservability, |
| 25 | + BaseObservabilityConfig, |
| 26 | +) |
| 27 | +from modelmesh.interfaces.observability import ( |
| 28 | + RequestLogEntry, |
| 29 | + RoutingEvent, |
| 30 | + Severity, |
| 31 | + TraceEntry, |
| 32 | +) |
| 33 | + |
| 34 | +__all__ = [ |
| 35 | + "CallbackObservabilityConfig", |
| 36 | + "CallbackObservability", |
| 37 | +] |
| 38 | + |
| 39 | + |
| 40 | +@dataclass |
| 41 | +class CallbackObservabilityConfig(BaseObservabilityConfig): |
| 42 | + """Configuration for CallbackObservability. |
| 43 | +
|
| 44 | + Attributes: |
| 45 | + callback: Function called with each event, log entry, or trace. |
| 46 | + Receives the original object (RoutingEvent, RequestLogEntry, |
| 47 | + or TraceEntry), not a formatted string. |
| 48 | + """ |
| 49 | + |
| 50 | + callback: Optional[Callable[[Any], None]] = None |
| 51 | + |
| 52 | + |
| 53 | +class CallbackObservability(BaseObservability): |
| 54 | + """Observability sink that routes events to a user-supplied callback. |
| 55 | +
|
| 56 | + The callback receives the original event/log/trace object (not a |
| 57 | + formatted string), enabling integration with custom dashboards, |
| 58 | + message queues, or alerting systems. |
| 59 | +
|
| 60 | + Respects all BaseObservability filters (event_filter, min_severity, |
| 61 | + redact_secrets) before invoking the callback. |
| 62 | + """ |
| 63 | + |
| 64 | + def __init__(self, config: CallbackObservabilityConfig) -> None: |
| 65 | + super().__init__(config) |
| 66 | + self._callback_fn = config.callback |
| 67 | + |
| 68 | + def emit(self, event: RoutingEvent) -> None: |
| 69 | + """Emit a routing event to the callback. |
| 70 | +
|
| 71 | + Applies event_filter from config before invoking. |
| 72 | + """ |
| 73 | + if self._config.event_filter: |
| 74 | + if event.event_type.value not in self._config.event_filter: |
| 75 | + return |
| 76 | + if self._callback_fn: |
| 77 | + self._callback_fn(event) |
| 78 | + |
| 79 | + def log(self, entry: RequestLogEntry) -> None: |
| 80 | + """Route a request/response log entry to the callback.""" |
| 81 | + if self._callback_fn: |
| 82 | + self._callback_fn(entry) |
| 83 | + |
| 84 | + def trace(self, entry: TraceEntry) -> None: |
| 85 | + """Route a trace entry to the callback, filtering by min_severity.""" |
| 86 | + min_level = self._SEVERITY_ORDER.get( |
| 87 | + Severity(self._config.min_severity), 1 |
| 88 | + ) |
| 89 | + entry_level = self._SEVERITY_ORDER.get(entry.severity, 0) |
| 90 | + if entry_level < min_level: |
| 91 | + return |
| 92 | + if self._callback_fn: |
| 93 | + self._callback_fn(entry) |
| 94 | + |
| 95 | + def _write(self, line: str) -> None: |
| 96 | + """No-op: output goes through callback, not formatted _write.""" |
| 97 | + pass |
0 commit comments