44//! snapshot creation, scans, and transactions. Metrics are collected during operations
55//! and reported as events via the `MetricsReporter` trait.
66//!
7- //! Each operation (Snapshot, Transaction, Scan) is assigned a unique operation ID (UUID )
7+ //! Each operation (Snapshot, Transaction, Scan) is assigned a unique operation ID ([`MetricId`] )
88//! when it starts, and all subsequent events for that operation reference this ID.
99//! This allows reporters to correlate events and track operation lifecycles.
1010//!
5858//! }
5959//! ```
6060
61+ use std:: fmt;
6162use std:: time:: { Duration , Instant } ;
6263use uuid:: Uuid ;
6364
65+ /// Unique identifier for a metrics operation.
66+ ///
67+ /// Each operation (Snapshot, Transaction, Scan) gets a unique MetricId that
68+ /// is used to correlate all events from that operation.
69+ #[ derive( Debug , Clone , Copy , PartialEq , Eq , Hash ) ]
70+ pub struct MetricId ( Uuid ) ;
71+
72+ impl MetricId {
73+ /// Generate a new unique MetricId.
74+ pub fn new ( ) -> Self {
75+ Self ( Uuid :: new_v4 ( ) )
76+ }
77+ }
78+
79+ impl Default for MetricId {
80+ fn default ( ) -> Self {
81+ Self :: new ( )
82+ }
83+ }
84+
85+ impl fmt:: Display for MetricId {
86+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
87+ write ! ( f, "{}" , self . 0 )
88+ }
89+ }
90+
6491/// Trait for reporting metrics events from Delta operations.
6592///
6693/// Implementations of this trait receive metric events as they occur during operations
@@ -74,19 +101,19 @@ pub trait MetricsReporter: Send + Sync {
74101
75102/// Metric events emitted during Delta Kernel operations.
76103///
77- /// Each event includes an `operation_id` (UUID ) that uniquely identifies the operation
104+ /// Each event includes an `operation_id` (MetricId ) that uniquely identifies the operation
78105/// instance. This allows correlating multiple events from the same operation.
79106#[ derive( Debug , Clone ) ]
80107pub enum MetricEvent {
81108 /// A snapshot creation operation has started.
82109 SnapshotStarted {
83- operation_id : Uuid ,
110+ operation_id : MetricId ,
84111 table_path : String ,
85112 } ,
86113
87114 /// Log segment loading completed (listing and organizing log files).
88115 LogSegmentLoaded {
89- operation_id : Uuid ,
116+ operation_id : MetricId ,
90117 duration : Duration ,
91118 num_commit_files : u64 ,
92119 num_checkpoint_files : u64 ,
@@ -95,19 +122,65 @@ pub enum MetricEvent {
95122
96123 /// Protocol and metadata loading completed.
97124 ProtocolMetadataLoaded {
98- operation_id : Uuid ,
125+ operation_id : MetricId ,
99126 duration : Duration ,
100127 } ,
101128
102129 /// Snapshot creation completed successfully.
103130 SnapshotCompleted {
104- operation_id : Uuid ,
131+ operation_id : MetricId ,
105132 version : u64 ,
106133 total_duration : Duration ,
107134 } ,
108135
109136 /// Snapshot creation failed.
110- SnapshotFailed { operation_id : Uuid } ,
137+ SnapshotFailed { operation_id : MetricId } ,
138+ }
139+
140+ impl fmt:: Display for MetricEvent {
141+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
142+ match self {
143+ MetricEvent :: SnapshotStarted {
144+ operation_id,
145+ table_path,
146+ } => write ! (
147+ f,
148+ "SnapshotStarted(id={}, table={})" ,
149+ operation_id, table_path
150+ ) ,
151+ MetricEvent :: LogSegmentLoaded {
152+ operation_id,
153+ duration,
154+ num_commit_files,
155+ num_checkpoint_files,
156+ num_compaction_files,
157+ } => write ! (
158+ f,
159+ "LogSegmentLoaded(id={}, duration={:?}, commits={}, checkpoints={}, compactions={})" ,
160+ operation_id, duration, num_commit_files, num_checkpoint_files, num_compaction_files
161+ ) ,
162+ MetricEvent :: ProtocolMetadataLoaded {
163+ operation_id,
164+ duration,
165+ } => write ! (
166+ f,
167+ "ProtocolMetadataLoaded(id={}, duration={:?})" ,
168+ operation_id, duration
169+ ) ,
170+ MetricEvent :: SnapshotCompleted {
171+ operation_id,
172+ version,
173+ total_duration,
174+ } => write ! (
175+ f,
176+ "SnapshotCompleted(id={}, version={}, duration={:?})" ,
177+ operation_id, version, total_duration
178+ ) ,
179+ MetricEvent :: SnapshotFailed { operation_id } => {
180+ write ! ( f, "SnapshotFailed(id={})" , operation_id)
181+ }
182+ }
183+ }
111184}
112185
113186/// A simple timer for tracking operation durations.
0 commit comments