|
2 | 2 | //! |
3 | 3 | //! This module provides metrics tracking for various Delta operations including |
4 | 4 | //! snapshot creation, scans, and transactions. Metrics are collected during operations |
5 | | -//! and can be reported via the `MetricsReporter` trait. |
| 5 | +//! and reported as events via the `MetricsReporter` trait. |
| 6 | +//! |
| 7 | +//! Each operation (Snapshot, Transaction, Scan) is assigned a unique operation ID (UUID) |
| 8 | +//! when it starts, and all subsequent events for that operation reference this ID. |
| 9 | +//! This allows reporters to correlate events and track operation lifecycles. |
6 | 10 | //! |
7 | 11 | //! # Example: Implementing a Custom MetricsReporter |
8 | 12 | //! |
9 | 13 | //! ``` |
10 | 14 | //! use std::sync::Arc; |
11 | | -//! use delta_kernel::metrics::{MetricsReporter, SnapshotMetrics}; |
| 15 | +//! use delta_kernel::metrics::{MetricsReporter, MetricEvent}; |
12 | 16 | //! |
13 | 17 | //! struct LoggingReporter; |
14 | 18 | //! |
15 | 19 | //! impl MetricsReporter for LoggingReporter { |
16 | | -//! fn report_snapshot(&self, metrics: &SnapshotMetrics) { |
17 | | -//! println!("Snapshot creation took {:?}", metrics.load_snapshot_total_duration); |
18 | | -//! println!(" - Protocol/Metadata load: {:?}", metrics.load_protocol_metadata_duration); |
19 | | -//! println!(" - Log segment load: {:?}", metrics.load_log_segment_duration); |
20 | | -//! println!(" - Files: {} commits, {} checkpoints, {} compactions", |
21 | | -//! metrics.num_commit_files, |
22 | | -//! metrics.num_checkpoint_files, |
23 | | -//! metrics.num_compaction_files); |
| 20 | +//! fn report(&self, event: MetricEvent) { |
| 21 | +//! match event { |
| 22 | +//! MetricEvent::SnapshotStarted { operation_id, table_path } => { |
| 23 | +//! println!("Snapshot started: {} for table {}", operation_id, table_path); |
| 24 | +//! } |
| 25 | +//! MetricEvent::LogSegmentLoaded { operation_id, duration, num_commit_files, .. } => { |
| 26 | +//! println!(" Log segment loaded in {:?}: {} commits", duration, num_commit_files); |
| 27 | +//! } |
| 28 | +//! MetricEvent::SnapshotCompleted { operation_id, version, total_duration } => { |
| 29 | +//! println!("Snapshot completed: v{} in {:?}", version, total_duration); |
| 30 | +//! } |
| 31 | +//! MetricEvent::SnapshotFailed { operation_id } => { |
| 32 | +//! println!("Snapshot failed: {}", operation_id); |
| 33 | +//! } |
| 34 | +//! _ => {} |
| 35 | +//! } |
24 | 36 | //! } |
25 | 37 | //! } |
26 | 38 | //! ``` |
|
31 | 43 | //! |
32 | 44 | //! ``` |
33 | 45 | //! use std::sync::Arc; |
34 | | -//! use delta_kernel::metrics::{MetricsReporter, SnapshotMetrics}; |
| 46 | +//! use delta_kernel::metrics::{MetricsReporter, MetricEvent}; |
35 | 47 | //! |
36 | 48 | //! struct CompositeReporter { |
37 | 49 | //! reporters: Vec<Arc<dyn MetricsReporter>>, |
38 | 50 | //! } |
39 | 51 | //! |
40 | 52 | //! impl MetricsReporter for CompositeReporter { |
41 | | -//! fn report_snapshot(&self, metrics: &SnapshotMetrics) { |
| 53 | +//! fn report(&self, event: MetricEvent) { |
42 | 54 | //! for reporter in &self.reporters { |
43 | | -//! reporter.report_snapshot(metrics); |
| 55 | +//! reporter.report(event.clone()); |
44 | 56 | //! } |
45 | 57 | //! } |
46 | 58 | //! } |
47 | 59 | //! ``` |
48 | 60 |
|
49 | 61 | use std::time::{Duration, Instant}; |
| 62 | +use uuid::Uuid; |
50 | 63 |
|
51 | | -/// Trait for reporting metrics from Delta operations. |
| 64 | +/// Trait for reporting metrics events from Delta operations. |
52 | 65 | /// |
53 | | -/// Implementations of this trait receive metrics after operations complete |
| 66 | +/// Implementations of this trait receive metric events as they occur during operations |
54 | 67 | /// and can forward them to monitoring systems like Prometheus, DataDog, etc. |
| 68 | +/// |
| 69 | +/// Events are emitted throughout an operation's lifecycle, allowing real-time monitoring. |
55 | 70 | pub trait MetricsReporter: Send + Sync { |
56 | | - /// Report snapshot creation metrics. |
57 | | - fn report_snapshot(&self, metrics: &SnapshotMetrics); |
| 71 | + /// Report a metric event. |
| 72 | + fn report(&self, event: MetricEvent); |
58 | 73 | } |
59 | 74 |
|
60 | | -/// Metrics collected during snapshot creation. |
| 75 | +/// Metric events emitted during Delta Kernel operations. |
61 | 76 | /// |
62 | | -/// These metrics track the time spent in various phases of loading a snapshot, |
63 | | -/// including log segment construction and protocol/metadata loading. |
64 | | -/// They also track counts of files processed during snapshot creation. |
65 | | -#[derive(Debug, Clone, Default)] |
66 | | -pub struct SnapshotMetrics { |
67 | | - /// Total time spent loading the snapshot (all operations combined). |
68 | | - pub load_snapshot_total_duration: Duration, |
| 77 | +/// Each event includes an `operation_id` (UUID) that uniquely identifies the operation |
| 78 | +/// instance. This allows correlating multiple events from the same operation. |
| 79 | +#[derive(Debug, Clone)] |
| 80 | +pub enum MetricEvent { |
| 81 | + /// A snapshot creation operation has started. |
| 82 | + SnapshotStarted { |
| 83 | + operation_id: Uuid, |
| 84 | + table_path: String, |
| 85 | + }, |
69 | 86 |
|
70 | | - /// Time spent loading protocol and metadata actions. |
71 | | - pub load_protocol_metadata_duration: Duration, |
| 87 | + /// Log segment loading completed (listing and organizing log files). |
| 88 | + LogSegmentLoaded { |
| 89 | + operation_id: Uuid, |
| 90 | + duration: Duration, |
| 91 | + num_commit_files: u64, |
| 92 | + num_checkpoint_files: u64, |
| 93 | + num_compaction_files: u64, |
| 94 | + }, |
72 | 95 |
|
73 | | - /// Time spent building the log segment (listing and organizing log files). |
74 | | - pub load_log_segment_duration: Duration, |
| 96 | + /// Protocol and metadata loading completed. |
| 97 | + ProtocolMetadataLoaded { |
| 98 | + operation_id: Uuid, |
| 99 | + duration: Duration, |
| 100 | + }, |
75 | 101 |
|
76 | | - /// Number of commit files processed during snapshot creation. |
77 | | - pub num_commit_files: u64, |
| 102 | + /// Snapshot creation completed successfully. |
| 103 | + SnapshotCompleted { |
| 104 | + operation_id: Uuid, |
| 105 | + version: u64, |
| 106 | + total_duration: Duration, |
| 107 | + }, |
78 | 108 |
|
79 | | - /// Number of checkpoint files processed during snapshot creation. |
80 | | - pub num_checkpoint_files: u64, |
81 | | - |
82 | | - /// Number of compaction files processed during snapshot creation. |
83 | | - pub num_compaction_files: u64, |
| 109 | + /// Snapshot creation failed. |
| 110 | + SnapshotFailed { operation_id: Uuid }, |
84 | 111 | } |
85 | 112 |
|
86 | 113 | /// A simple timer for tracking operation durations. |
87 | 114 | /// |
88 | 115 | /// # Example |
89 | | -/// ```ignore |
| 116 | +/// ``` |
| 117 | +/// use delta_kernel::metrics::Timer; |
| 118 | +/// |
90 | 119 | /// let timer = Timer::new(); |
91 | 120 | /// // ... do work ... |
92 | 121 | /// let duration = timer.elapsed(); |
|
0 commit comments