/ crates / metrics / src / event.rs
event.rs
  1  /// Bot event definitions for comprehensive observability
  2  use serde::{Deserialize, Serialize};
  3  
  4  /// All possible bot events with structured data
  5  #[derive(Debug, Clone, Serialize, Deserialize)]
  6  pub enum BotEvent {
  7      /// Bot lifecycle events
  8      BotStarted {
  9          bot_id: String,
 10          role: String,
 11          timestamp_ms: i64,
 12      },
 13      BotStopped {
 14          bot_id: String,
 15          timestamp_ms: i64,
 16          reason: String,
 17      },
 18      BotError {
 19          bot_id: String,
 20          timestamp_ms: i64,
 21          error: String,
 22      },
 23  
 24      /// Behavior execution events
 25      BehaviorStarted {
 26          bot_id: String,
 27          behavior_id: String,
 28          timestamp_ms: i64,
 29      },
 30      BehaviorCompleted {
 31          bot_id: String,
 32          behavior_id: String,
 33          timestamp_ms: i64,
 34          duration_ms: u64,
 35          success: bool,
 36      },
 37  
 38      /// Transaction events
 39      TransactionSubmitted {
 40          bot_id: String,
 41          tx_hash: String,
 42          tx_type: String,
 43          timestamp_ms: i64,
 44      },
 45      TransactionConfirmed {
 46          bot_id: String,
 47          tx_hash: String,
 48          timestamp_ms: i64,
 49          confirmation_time_ms: u64,
 50      },
 51      TransactionFailed {
 52          bot_id: String,
 53          tx_hash: String,
 54          timestamp_ms: i64,
 55          error: String,
 56      },
 57  
 58      /// Network events
 59      NetworkRequest {
 60          bot_id: String,
 61          endpoint: String,
 62          timestamp_ms: i64,
 63      },
 64      NetworkResponse {
 65          bot_id: String,
 66          endpoint: String,
 67          timestamp_ms: i64,
 68          latency_ms: u64,
 69          status_code: u16,
 70      },
 71  
 72      /// DEX events
 73      OrderPlaced {
 74          bot_id: String,
 75          order_id: String,
 76          pair: String,
 77          side: String,
 78          timestamp_ms: i64,
 79      },
 80      OrderFilled {
 81          bot_id: String,
 82          order_id: String,
 83          timestamp_ms: i64,
 84          fill_time_ms: u64,
 85      },
 86      OrderCanceled {
 87          bot_id: String,
 88          order_id: String,
 89          timestamp_ms: i64,
 90      },
 91  
 92      /// Governance events
 93      ProposalCreated {
 94          bot_id: String,
 95          proposal_id: String,
 96          timestamp_ms: i64,
 97      },
 98      VoteCast {
 99          bot_id: String,
100          proposal_id: String,
101          vote: String,
102          timestamp_ms: i64,
103      },
104  
105      /// Cross-chain events
106      CrossChainLock {
107          bot_id: String,
108          lock_id: String,
109          amount: u128,
110          timestamp_ms: i64,
111      },
112      CrossChainMint {
113          bot_id: String,
114          mint_id: String,
115          amount: u128,
116          timestamp_ms: i64,
117      },
118  
119      /// Validator events
120      BlockProposed {
121          bot_id: String,
122          block_height: u32,
123          timestamp_ms: i64,
124      },
125      BlockAttested {
126          bot_id: String,
127          block_height: u32,
128          timestamp_ms: i64,
129      },
130  
131      /// Scenario events
132      ScenarioStarted {
133          scenario_id: String,
134          timestamp_ms: i64,
135      },
136      ScenarioCompleted {
137          scenario_id: String,
138          timestamp_ms: i64,
139          duration_ms: u64,
140          success: bool,
141      },
142  
143      /// Metric snapshots
144      MetricSnapshot {
145          timestamp_ms: i64,
146          tps: f64,
147          latency_p50_ms: f64,
148          latency_p95_ms: f64,
149          latency_p99_ms: f64,
150          error_rate: f64,
151          active_bots: usize,
152      },
153  }
154  
155  impl BotEvent {
156      /// Get the bot ID associated with this event, if any
157      pub fn bot_id(&self) -> Option<&str> {
158          match self {
159              BotEvent::BotStarted { bot_id, .. }
160              | BotEvent::BotStopped { bot_id, .. }
161              | BotEvent::BotError { bot_id, .. }
162              | BotEvent::BehaviorStarted { bot_id, .. }
163              | BotEvent::BehaviorCompleted { bot_id, .. }
164              | BotEvent::TransactionSubmitted { bot_id, .. }
165              | BotEvent::TransactionConfirmed { bot_id, .. }
166              | BotEvent::TransactionFailed { bot_id, .. }
167              | BotEvent::NetworkRequest { bot_id, .. }
168              | BotEvent::NetworkResponse { bot_id, .. }
169              | BotEvent::OrderPlaced { bot_id, .. }
170              | BotEvent::OrderFilled { bot_id, .. }
171              | BotEvent::OrderCanceled { bot_id, .. }
172              | BotEvent::ProposalCreated { bot_id, .. }
173              | BotEvent::VoteCast { bot_id, .. }
174              | BotEvent::CrossChainLock { bot_id, .. }
175              | BotEvent::CrossChainMint { bot_id, .. }
176              | BotEvent::BlockProposed { bot_id, .. }
177              | BotEvent::BlockAttested { bot_id, .. } => Some(bot_id.as_str()),
178              _ => None,
179          }
180      }
181  
182      /// Get the timestamp of this event
183      pub fn timestamp_ms(&self) -> i64 {
184          match self {
185              BotEvent::BotStarted { timestamp_ms, .. }
186              | BotEvent::BotStopped { timestamp_ms, .. }
187              | BotEvent::BotError { timestamp_ms, .. }
188              | BotEvent::BehaviorStarted { timestamp_ms, .. }
189              | BotEvent::BehaviorCompleted { timestamp_ms, .. }
190              | BotEvent::TransactionSubmitted { timestamp_ms, .. }
191              | BotEvent::TransactionConfirmed { timestamp_ms, .. }
192              | BotEvent::TransactionFailed { timestamp_ms, .. }
193              | BotEvent::NetworkRequest { timestamp_ms, .. }
194              | BotEvent::NetworkResponse { timestamp_ms, .. }
195              | BotEvent::OrderPlaced { timestamp_ms, .. }
196              | BotEvent::OrderFilled { timestamp_ms, .. }
197              | BotEvent::OrderCanceled { timestamp_ms, .. }
198              | BotEvent::ProposalCreated { timestamp_ms, .. }
199              | BotEvent::VoteCast { timestamp_ms, .. }
200              | BotEvent::CrossChainLock { timestamp_ms, .. }
201              | BotEvent::CrossChainMint { timestamp_ms, .. }
202              | BotEvent::BlockProposed { timestamp_ms, .. }
203              | BotEvent::BlockAttested { timestamp_ms, .. }
204              | BotEvent::ScenarioStarted { timestamp_ms, .. }
205              | BotEvent::ScenarioCompleted { timestamp_ms, .. }
206              | BotEvent::MetricSnapshot { timestamp_ms, .. } => *timestamp_ms,
207          }
208      }
209  
210      /// Check if this is an error event
211      pub fn is_error(&self) -> bool {
212          matches!(
213              self,
214              BotEvent::BotError { .. } | BotEvent::TransactionFailed { .. }
215          )
216      }
217  }