@@ -31,31 +31,47 @@ const DEFAULT_MAX_MEMORY_BYTES: usize = 10 * 1024 * 1024;
3131/// Sample collected for a managed unit at a specific timestamp.
3232#[ derive( Debug , Clone , Serialize , Deserialize ) ]
3333pub struct MetricSample {
34+ /// Timestamp when this sample was collected.
3435 pub timestamp : DateTime < Utc > ,
36+ /// CPU usage percentage (0-100+ for multi-core).
3537 pub cpu_percent : f32 ,
38+ /// Resident set size in bytes.
3639 pub rss_bytes : u64 ,
40+ /// Total bytes read from disk.
3741 pub io_read_bytes : u64 ,
42+ /// Total bytes written to disk.
3843 pub io_write_bytes : u64 ,
44+ /// Total bytes received from network.
3945 pub net_rx_bytes : u64 ,
46+ /// Total bytes transmitted to network.
4047 pub net_tx_bytes : u64 ,
4148}
4249
4350/// Summary statistics derived from recent samples.
4451#[ derive( Debug , Clone , Serialize , Deserialize ) ]
4552pub struct MetricsSummary {
53+ /// Most recent CPU usage percentage.
4654 pub latest_cpu_percent : f32 ,
55+ /// Average CPU usage across all samples.
4756 pub average_cpu_percent : f32 ,
57+ /// Maximum CPU usage observed.
4858 pub max_cpu_percent : f32 ,
59+ /// Most recent resident set size in bytes.
4960 pub latest_rss_bytes : u64 ,
61+ /// Total number of samples used for statistics.
5062 pub samples : usize ,
5163}
5264
5365/// Configuration for runtime metrics collection.
5466#[ derive( Debug , Clone ) ]
5567pub struct MetricsSettings {
68+ /// How long to retain metrics in memory.
5669 pub retention : Duration ,
70+ /// Interval between metric samples.
5771 pub sample_interval : Duration ,
72+ /// Maximum memory used for metrics storage.
5873 pub max_memory_bytes : usize ,
74+ /// Optional spillover configuration for disk persistence.
5975 pub spillover : Option < SpilloverSettings > ,
6076}
6177
@@ -73,17 +89,24 @@ impl Default for MetricsSettings {
7389/// Spillover configuration used to persist evicted samples to disk.
7490#[ derive( Debug , Clone ) ]
7591pub struct SpilloverSettings {
92+ /// Directory where spillover segments are written.
7693 pub directory : PathBuf ,
94+ /// Maximum total bytes allowed for spillover storage.
7795 pub max_bytes : u64 ,
96+ /// Target size for individual spillover segment files.
7897 pub segment_bytes : u64 ,
7998}
8099
100+ /// Errors that can occur during metrics operations.
81101#[ derive( Debug , Error ) ]
82102pub enum MetricsError {
103+ /// Failed to create spillover directory.
83104 #[ error( "failed to create spillover directory: {0}" ) ]
84105 CreateDir ( std:: io:: Error ) ,
106+ /// Failed to write spillover segment to disk.
85107 #[ error( "failed to write spillover segment: {0}" ) ]
86108 SpilloverWrite ( std:: io:: Error ) ,
109+ /// Failed to serialize spillover record.
87110 #[ error( "failed to serialise spillover record: {0}" ) ]
88111 SpilloverSerialize ( serde_json:: Error ) ,
89112}
@@ -440,21 +463,26 @@ struct SpilloverRecord<'a> {
440463 sample : & ' a MetricSample ,
441464}
442465
466+ /// Creates a new shared, thread-safe metrics store with the given settings.
443467pub fn shared_store ( settings : MetricsSettings ) -> Result < MetricsHandle , MetricsError > {
444468 Ok ( Arc :: new ( RwLock :: new ( MetricsStore :: new ( settings) ?) ) )
445469}
446470
447471/// Unit metadata used by the collector to emit samples.
448472#[ derive( Debug ) ]
449473pub struct UnitTarget {
474+ /// Unique hash identifying the unit.
450475 pub hash : String ,
476+ /// Process ID if the unit has a running process.
451477 pub pid : Option < u32 > ,
452478}
453479
454480/// Result of sampling a unit in the collector.
455481#[ derive( Debug ) ]
456482pub struct CollectedSample {
483+ /// Hash of the unit that was sampled.
457484 pub hash : String ,
485+ /// Collected metric sample data.
458486 pub sample : MetricSample ,
459487}
460488
0 commit comments