Skip to content

Commit e0565df

Browse files
committed
Use MetricId instead of UUID::new_v4
1 parent 5542080 commit e0565df

3 files changed

Lines changed: 87 additions & 20 deletions

File tree

kernel/src/metrics/mod.rs

Lines changed: 80 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
//!
@@ -58,9 +58,36 @@
5858
//! }
5959
//! ```
6060
61+
use std::fmt;
6162
use std::time::{Duration, Instant};
6263
use 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)]
80107
pub 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.

kernel/src/snapshot.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use crate::checkpoint::CheckpointWriter;
1111
use crate::committer::Committer;
1212
use crate::listed_log_files::ListedLogFiles;
1313
use crate::log_segment::LogSegment;
14-
use crate::metrics::{MetricEvent, Timer};
14+
use crate::metrics::{MetricEvent, MetricId, Timer};
1515
use crate::path::ParsedLogPath;
1616
use crate::scan::ScanBuilder;
1717
use crate::schema::SchemaRef;
@@ -21,7 +21,6 @@ use crate::transaction::Transaction;
2121
use crate::LogCompactionWriter;
2222
use crate::{DeltaResult, Engine, Error, Version};
2323
use delta_kernel_derive::internal_api;
24-
use uuid::Uuid;
2524

2625
mod builder;
2726
pub use builder::SnapshotBuilder;
@@ -263,9 +262,9 @@ impl Snapshot {
263262
location: Url,
264263
log_segment: LogSegment,
265264
engine: &dyn Engine,
266-
operation_id: Option<Uuid>,
265+
operation_id: Option<MetricId>,
267266
) -> DeltaResult<Self> {
268-
let operation_id = operation_id.unwrap_or_else(Uuid::new_v4);
267+
let operation_id = operation_id.unwrap_or_else(MetricId::new);
269268
let reporter = engine.get_metrics_reporter();
270269
let timer = Timer::new();
271270

@@ -280,12 +279,8 @@ impl Snapshot {
280279
});
281280
}
282281

283-
let table_configuration = TableConfiguration::try_new(
284-
metadata,
285-
protocol,
286-
location,
287-
log_segment.end_version,
288-
)?;
282+
let table_configuration =
283+
TableConfiguration::try_new(metadata, protocol, location, log_segment.end_version)?;
289284

290285
Ok(Self {
291286
log_segment,

kernel/src/snapshot/builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
//! Builder for creating [`Snapshot`] instances.
22
use crate::log_path::LogPath;
33
use crate::log_segment::LogSegment;
4-
use crate::metrics::{MetricEvent, Timer};
4+
use crate::metrics::{MetricEvent, MetricId, Timer};
55
use crate::snapshot::SnapshotRef;
66
use crate::{DeltaResult, Engine, Error, Snapshot, Version};
7-
use uuid::Uuid;
87

98
use url::Url;
109

@@ -85,7 +84,7 @@ impl SnapshotBuilder {
8584
pub fn build(self, engine: &dyn Engine) -> DeltaResult<SnapshotRef> {
8685
let log_tail = self.log_tail.into_iter().map(Into::into).collect();
8786
if let Some(table_root) = self.table_root {
88-
let operation_id = Uuid::new_v4();
87+
let operation_id = MetricId::new();
8988
let reporter = engine.get_metrics_reporter();
9089

9190
if let Some(ref r) = reporter {

0 commit comments

Comments
 (0)