|
| 1 | +# Copyright (c) Microsoft. All rights reserved. |
| 2 | + |
| 3 | +""" |
| 4 | +FlightRecorderAdapter - Import Audit Logs to LightningStore |
| 5 | +============================================================= |
| 6 | +
|
| 7 | +Adapts Agent-OS Flight Recorder to Agent-Lightning store format. |
| 8 | +""" |
| 9 | + |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import logging |
| 13 | +from datetime import datetime, timezone |
| 14 | +from typing import Any, Dict, List |
| 15 | + |
| 16 | +logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +class FlightRecorderAdapter: |
| 20 | + """ |
| 21 | + Import Agent-OS Flight Recorder logs to LightningStore. |
| 22 | +
|
| 23 | + Example: |
| 24 | + >>> from agent_os import FlightRecorder |
| 25 | + >>> |
| 26 | + >>> recorder = FlightRecorder() |
| 27 | + >>> adapter = FlightRecorderAdapter(recorder) |
| 28 | + >>> |
| 29 | + >>> # Import to Lightning store |
| 30 | + >>> adapter.import_to_store(lightning_store) |
| 31 | + """ |
| 32 | + |
| 33 | + def __init__( |
| 34 | + self, |
| 35 | + flight_recorder: Any, |
| 36 | + *, |
| 37 | + trace_id_prefix: str = "agentos", |
| 38 | + ): |
| 39 | + """ |
| 40 | + Initialize adapter. |
| 41 | +
|
| 42 | + Args: |
| 43 | + flight_recorder: Agent-OS FlightRecorder |
| 44 | + trace_id_prefix: Prefix for trace IDs |
| 45 | + """ |
| 46 | + self.recorder = flight_recorder |
| 47 | + self.trace_id_prefix = trace_id_prefix |
| 48 | + self._imported_count = 0 |
| 49 | + |
| 50 | + def _convert_entry(self, entry: Any, index: int) -> Dict[str, Any]: |
| 51 | + """Convert Flight Recorder entry to span format.""" |
| 52 | + entry_type = getattr(entry, "type", "unknown") |
| 53 | + timestamp = getattr(entry, "timestamp", datetime.now(timezone.utc)) |
| 54 | + agent_id = getattr(entry, "agent_id", "unknown") |
| 55 | + |
| 56 | + span = { |
| 57 | + "span_id": f"{self.trace_id_prefix}-{index}", |
| 58 | + "trace_id": f"{self.trace_id_prefix}-{agent_id}", |
| 59 | + "name": f"agent_os.{entry_type}", |
| 60 | + "start_time": timestamp.isoformat() if hasattr(timestamp, "isoformat") else str(timestamp), |
| 61 | + "attributes": { |
| 62 | + "agent_os.entry_type": entry_type, |
| 63 | + "agent_os.agent_id": agent_id, |
| 64 | + }, |
| 65 | + } |
| 66 | + |
| 67 | + # Add type-specific attributes |
| 68 | + if entry_type == "policy_check": |
| 69 | + span["attributes"].update( |
| 70 | + { |
| 71 | + "agent_os.policy_name": getattr(entry, "policy_name", "unknown"), |
| 72 | + "agent_os.policy_violated": getattr(entry, "violated", False), |
| 73 | + } |
| 74 | + ) |
| 75 | + elif entry_type == "signal": |
| 76 | + span["attributes"].update( |
| 77 | + { |
| 78 | + "agent_os.signal_type": getattr(entry, "signal", "unknown"), |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + return span |
| 83 | + |
| 84 | + def get_spans(self) -> List[Dict[str, Any]]: |
| 85 | + """Get all entries as spans.""" |
| 86 | + entries = [] |
| 87 | + if hasattr(self.recorder, "get_entries"): |
| 88 | + entries = self.recorder.get_entries() |
| 89 | + elif hasattr(self.recorder, "entries"): |
| 90 | + entries = self.recorder.entries |
| 91 | + |
| 92 | + return [self._convert_entry(e, i) for i, e in enumerate(entries)] |
| 93 | + |
| 94 | + def import_to_store(self, store: Any) -> int: |
| 95 | + """ |
| 96 | + Import spans to LightningStore. |
| 97 | +
|
| 98 | + Args: |
| 99 | + store: LightningStore instance |
| 100 | +
|
| 101 | + Returns: |
| 102 | + Number of spans imported |
| 103 | + """ |
| 104 | + spans = self.get_spans() |
| 105 | + |
| 106 | + for span in spans: |
| 107 | + try: |
| 108 | + if hasattr(store, "emit_span"): |
| 109 | + store.emit_span(span) |
| 110 | + elif hasattr(store, "add_span"): |
| 111 | + store.add_span(span) |
| 112 | + except Exception as e: |
| 113 | + logger.error(f"Failed to import span: {e}") |
| 114 | + |
| 115 | + self._imported_count += len(spans) |
| 116 | + logger.info(f"Imported {len(spans)} spans to LightningStore") |
| 117 | + return len(spans) |
| 118 | + |
| 119 | + def get_violation_summary(self) -> Dict[str, Any]: |
| 120 | + """Get summary of policy violations.""" |
| 121 | + spans = self.get_spans() |
| 122 | + violations = [s for s in spans if s["attributes"].get("agent_os.policy_violated", False)] |
| 123 | + return { |
| 124 | + "total_entries": len(spans), |
| 125 | + "total_violations": len(violations), |
| 126 | + "violation_rate": len(violations) / len(spans) if len(spans) > 0 else 0.0, |
| 127 | + } |
0 commit comments