|
| 1 | +//! Metrics collection for Delta Kernel operations. |
| 2 | +//! |
| 3 | +//! This module provides metrics tracking for various Delta operations including |
| 4 | +//! snapshot creation, scans, and transactions. Metrics are collected during operations |
| 5 | +//! and can be reported via the `MetricsReporter` trait. |
| 6 | +//! |
| 7 | +//! # Example: Implementing a Custom MetricsReporter |
| 8 | +//! |
| 9 | +//! ``` |
| 10 | +//! use std::sync::Arc; |
| 11 | +//! use delta_kernel::metrics::{MetricsReporter, SnapshotMetrics}; |
| 12 | +//! |
| 13 | +//! struct LoggingReporter; |
| 14 | +//! |
| 15 | +//! 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); |
| 24 | +//! } |
| 25 | +//! } |
| 26 | +//! ``` |
| 27 | +//! |
| 28 | +//! # Example: Implementing a Composite Reporter |
| 29 | +//! |
| 30 | +//! If you need to send metrics to multiple destinations, you can create a composite reporter: |
| 31 | +//! |
| 32 | +//! ``` |
| 33 | +//! use std::sync::Arc; |
| 34 | +//! use delta_kernel::metrics::{MetricsReporter, SnapshotMetrics}; |
| 35 | +//! |
| 36 | +//! struct CompositeReporter { |
| 37 | +//! reporters: Vec<Arc<dyn MetricsReporter>>, |
| 38 | +//! } |
| 39 | +//! |
| 40 | +//! impl MetricsReporter for CompositeReporter { |
| 41 | +//! fn report_snapshot(&self, metrics: &SnapshotMetrics) { |
| 42 | +//! for reporter in &self.reporters { |
| 43 | +//! reporter.report_snapshot(metrics); |
| 44 | +//! } |
| 45 | +//! } |
| 46 | +//! } |
| 47 | +//! ``` |
| 48 | +
|
| 49 | +use std::time::{Duration, Instant}; |
| 50 | + |
| 51 | +/// Trait for reporting metrics from Delta operations. |
| 52 | +/// |
| 53 | +/// Implementations of this trait receive metrics after operations complete |
| 54 | +/// and can forward them to monitoring systems like Prometheus, DataDog, etc. |
| 55 | +pub trait MetricsReporter: Send + Sync { |
| 56 | + /// Report snapshot creation metrics. |
| 57 | + fn report_snapshot(&self, metrics: &SnapshotMetrics); |
| 58 | +} |
| 59 | + |
| 60 | +/// Metrics collected during snapshot creation. |
| 61 | +/// |
| 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, |
| 69 | + |
| 70 | + /// Time spent loading protocol and metadata actions. |
| 71 | + pub load_protocol_metadata_duration: Duration, |
| 72 | + |
| 73 | + /// Time spent building the log segment (listing and organizing log files). |
| 74 | + pub load_log_segment_duration: Duration, |
| 75 | + |
| 76 | + /// Number of commit files processed during snapshot creation. |
| 77 | + pub num_commit_files: u64, |
| 78 | + |
| 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, |
| 84 | +} |
| 85 | + |
| 86 | +/// A simple timer for tracking operation durations. |
| 87 | +/// |
| 88 | +/// # Example |
| 89 | +/// ```ignore |
| 90 | +/// let timer = Timer::new(); |
| 91 | +/// // ... do work ... |
| 92 | +/// let duration = timer.elapsed(); |
| 93 | +/// ``` |
| 94 | +#[derive(Debug)] |
| 95 | +pub struct Timer { |
| 96 | + start: Instant, |
| 97 | +} |
| 98 | + |
| 99 | +impl Timer { |
| 100 | + /// Create a new timer that starts immediately. |
| 101 | + pub fn new() -> Self { |
| 102 | + Self { |
| 103 | + start: Instant::now(), |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + /// Get the elapsed time as a Duration since this timer was created. |
| 108 | + pub fn elapsed(&self) -> Duration { |
| 109 | + self.start.elapsed() |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +impl Default for Timer { |
| 114 | + fn default() -> Self { |
| 115 | + Self::new() |
| 116 | + } |
| 117 | +} |
0 commit comments