|
| 1 | +//! Metric event types and utilities. |
| 2 | +
|
| 3 | +use std::fmt; |
| 4 | +use std::time::{Duration, Instant}; |
| 5 | +use uuid::Uuid; |
| 6 | + |
| 7 | +/// Unique identifier for a metrics operation. |
| 8 | +/// |
| 9 | +/// Each operation (Snapshot, Transaction, Scan) gets a unique MetricId that |
| 10 | +/// is used to correlate all events from that operation. |
| 11 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 12 | +pub struct MetricId(Uuid); |
| 13 | + |
| 14 | +impl MetricId { |
| 15 | + /// Generate a new unique MetricId. |
| 16 | + pub fn new() -> Self { |
| 17 | + Self(Uuid::new_v4()) |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +impl Default for MetricId { |
| 22 | + fn default() -> Self { |
| 23 | + Self::new() |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl fmt::Display for MetricId { |
| 28 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 29 | + write!(f, "{}", self.0) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/// Metric events emitted during Delta Kernel operations. |
| 34 | +/// |
| 35 | +/// Some events include an `operation_id` (MetricId) that uniquely identifies the operation |
| 36 | +/// instance. This allows correlating multiple events from the same operation. |
| 37 | +#[derive(Debug, Clone)] |
| 38 | +pub enum MetricEvent { |
| 39 | + /// A snapshot creation operation has started. |
| 40 | + SnapshotStarted { |
| 41 | + operation_id: MetricId, |
| 42 | + table_path: String, |
| 43 | + }, |
| 44 | + |
| 45 | + /// Log segment loading completed (listing and organizing log files). |
| 46 | + LogSegmentLoaded { |
| 47 | + operation_id: MetricId, |
| 48 | + duration: Duration, |
| 49 | + num_commit_files: u64, |
| 50 | + num_checkpoint_files: u64, |
| 51 | + num_compaction_files: u64, |
| 52 | + }, |
| 53 | + |
| 54 | + /// Protocol and metadata loading completed. |
| 55 | + ProtocolMetadataLoaded { |
| 56 | + operation_id: MetricId, |
| 57 | + duration: Duration, |
| 58 | + }, |
| 59 | + |
| 60 | + /// Snapshot creation completed successfully. |
| 61 | + SnapshotCompleted { |
| 62 | + operation_id: MetricId, |
| 63 | + version: u64, |
| 64 | + total_duration: Duration, |
| 65 | + }, |
| 66 | + |
| 67 | + /// Snapshot creation failed. |
| 68 | + SnapshotFailed { |
| 69 | + operation_id: MetricId, |
| 70 | + duration: Duration, |
| 71 | + }, |
| 72 | + |
| 73 | + /// Storage list operation completed. |
| 74 | + /// These events track storage-level latencies and are emitted automatically |
| 75 | + /// by the default storage handler implementation. |
| 76 | + StorageListCompleted { duration: Duration, num_files: u64 }, |
| 77 | + |
| 78 | + /// Storage read operation completed. |
| 79 | + StorageReadCompleted { |
| 80 | + duration: Duration, |
| 81 | + num_files: u64, |
| 82 | + bytes_read: u64, |
| 83 | + }, |
| 84 | + |
| 85 | + /// Storage copy operation completed. |
| 86 | + StorageCopyCompleted { duration: Duration }, |
| 87 | +} |
| 88 | + |
| 89 | +impl fmt::Display for MetricEvent { |
| 90 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 91 | + match self { |
| 92 | + MetricEvent::SnapshotStarted { |
| 93 | + operation_id, |
| 94 | + table_path, |
| 95 | + } => write!( |
| 96 | + f, |
| 97 | + "SnapshotStarted(id={}, table={})", |
| 98 | + operation_id, table_path |
| 99 | + ), |
| 100 | + MetricEvent::LogSegmentLoaded { |
| 101 | + operation_id, |
| 102 | + duration, |
| 103 | + num_commit_files, |
| 104 | + num_checkpoint_files, |
| 105 | + num_compaction_files, |
| 106 | + } => write!( |
| 107 | + f, |
| 108 | + "LogSegmentLoaded(id={}, duration={:?}, commits={}, checkpoints={}, compactions={})", |
| 109 | + operation_id, duration, num_commit_files, num_checkpoint_files, num_compaction_files |
| 110 | + ), |
| 111 | + MetricEvent::ProtocolMetadataLoaded { |
| 112 | + operation_id, |
| 113 | + duration, |
| 114 | + } => write!( |
| 115 | + f, |
| 116 | + "ProtocolMetadataLoaded(id={}, duration={:?})", |
| 117 | + operation_id, duration |
| 118 | + ), |
| 119 | + MetricEvent::SnapshotCompleted { |
| 120 | + operation_id, |
| 121 | + version, |
| 122 | + total_duration, |
| 123 | + } => write!( |
| 124 | + f, |
| 125 | + "SnapshotCompleted(id={}, version={}, duration={:?})", |
| 126 | + operation_id, version, total_duration |
| 127 | + ), |
| 128 | + MetricEvent::SnapshotFailed { |
| 129 | + operation_id, |
| 130 | + duration, |
| 131 | + } => write!( |
| 132 | + f, |
| 133 | + "SnapshotFailed(id={}, duration={:?})", |
| 134 | + operation_id, duration |
| 135 | + ), |
| 136 | + MetricEvent::StorageListCompleted { |
| 137 | + duration, |
| 138 | + num_files, |
| 139 | + } => write!( |
| 140 | + f, |
| 141 | + "StorageListCompleted(duration={:?}, files={})", |
| 142 | + duration, num_files |
| 143 | + ), |
| 144 | + MetricEvent::StorageReadCompleted { |
| 145 | + duration, |
| 146 | + num_files, |
| 147 | + bytes_read, |
| 148 | + } => write!( |
| 149 | + f, |
| 150 | + "StorageReadCompleted(duration={:?}, files={}, bytes={})", |
| 151 | + duration, num_files, bytes_read |
| 152 | + ), |
| 153 | + MetricEvent::StorageCopyCompleted { duration } => write!( |
| 154 | + f, |
| 155 | + "StorageCopyCompleted(duration={:?})", |
| 156 | + duration |
| 157 | + ), |
| 158 | + } |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +/// A simple timer for tracking operation durations. |
| 163 | +/// |
| 164 | +/// # Example |
| 165 | +/// ``` |
| 166 | +/// use delta_kernel::metrics::Timer; |
| 167 | +/// |
| 168 | +/// let timer = Timer::new(); |
| 169 | +/// // ... do work ... |
| 170 | +/// let duration = timer.elapsed(); |
| 171 | +/// ``` |
| 172 | +#[derive(Debug)] |
| 173 | +pub struct Timer { |
| 174 | + start: Instant, |
| 175 | +} |
| 176 | + |
| 177 | +impl Timer { |
| 178 | + /// Create a new timer that starts immediately. |
| 179 | + pub fn new() -> Self { |
| 180 | + Self { |
| 181 | + start: Instant::now(), |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /// Get the elapsed time as a Duration since this timer was created. |
| 186 | + pub fn elapsed(&self) -> Duration { |
| 187 | + self.start.elapsed() |
| 188 | + } |
| 189 | +} |
| 190 | + |
| 191 | +impl Default for Timer { |
| 192 | + fn default() -> Self { |
| 193 | + Self::new() |
| 194 | + } |
| 195 | +} |
0 commit comments