Skip to main content

rustfs_obs/metrics/collectors/
compression.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::metrics::report::PrometheusMetric;
16use crate::metrics::schema::compression::*;
17
18/// Cluster-level compression statistics.
19#[derive(Debug, Clone, Default)]
20pub struct CompressionClusterStats {
21    pub original_bytes_total: u64,
22    pub compressed_bytes_total: u64,
23    pub bytes_saved_total: u64,
24    pub compression_ratio: f64,
25    pub compression_operations_total: u64,
26}
27
28/// Collect cluster-level compression metrics from cluster stats.
29pub fn collect_compression_cluster_metrics(stats: &CompressionClusterStats) -> Vec<PrometheusMetric> {
30    vec![
31        PrometheusMetric::from_descriptor(&COMPRESSION_OPERATIONS_TOTAL, stats.compression_operations_total as f64),
32        PrometheusMetric::from_descriptor(&COMPRESSION_BYTES_ORIGINAL_TOTAL, stats.original_bytes_total as f64),
33        PrometheusMetric::from_descriptor(&COMPRESSION_BYTES_COMPRESSED_TOTAL, stats.compressed_bytes_total as f64),
34        PrometheusMetric::from_descriptor(&COMPRESSION_BYTES_SAVED_TOTAL, stats.bytes_saved_total as f64),
35        PrometheusMetric::from_descriptor(&COMPRESSION_RATIO, stats.compression_ratio),
36    ]
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42    use crate::metrics::report::report_metrics;
43
44    #[test]
45    fn test_collect_compression_cluster_metrics() {
46        let stats = CompressionClusterStats {
47            original_bytes_total: 1000,
48            compressed_bytes_total: 700,
49            bytes_saved_total: 300,
50            compression_ratio: 0.7,
51            compression_operations_total: 10,
52        };
53
54        let metrics = collect_compression_cluster_metrics(&stats);
55        report_metrics(&metrics);
56
57        assert_eq!(metrics.len(), 5);
58
59        // Verify original bytes
60        let name = COMPRESSION_BYTES_ORIGINAL_TOTAL.get_full_metric_name();
61        let m = metrics.iter().find(|m| m.name == name && m.value == 1000.0);
62        assert!(m.is_some(), "missing or wrong value for original_bytes_total");
63
64        // Verify compressed bytes
65        let name = COMPRESSION_BYTES_COMPRESSED_TOTAL.get_full_metric_name();
66        let m = metrics.iter().find(|m| m.name == name && m.value == 700.0);
67        assert!(m.is_some(), "missing or wrong value for compressed_bytes_total");
68
69        // Verify saved bytes
70        let name = COMPRESSION_BYTES_SAVED_TOTAL.get_full_metric_name();
71        let m = metrics.iter().find(|m| m.name == name && m.value == 300.0);
72        assert!(m.is_some(), "missing or wrong value for bytes_saved_total");
73
74        // Verify compression ratio
75        let name = COMPRESSION_RATIO.get_full_metric_name();
76        let m = metrics.iter().find(|m| m.name == name && m.value == 0.7);
77        assert!(m.is_some(), "missing or wrong value for compression_ratio");
78
79        // Verify operations total
80        let name = COMPRESSION_OPERATIONS_TOTAL.get_full_metric_name();
81        let m = metrics.iter().find(|m| m.name == name && m.value == 10.0);
82        assert!(m.is_some(), "missing or wrong value for compression_operations_total");
83    }
84
85    #[test]
86    fn test_collect_compression_cluster_metrics_empty() {
87        let stats = CompressionClusterStats::default();
88
89        let metrics = collect_compression_cluster_metrics(&stats);
90        report_metrics(&metrics);
91
92        assert_eq!(metrics.len(), 5);
93
94        // All values should be zero
95        for metric in &metrics {
96            assert_eq!(metric.value, 0.0, "expected zero value for {}", metric.name);
97            assert!(metric.labels.is_empty());
98        }
99    }
100
101    #[test]
102    fn test_compression_cluster_stats_default() {
103        let stats = CompressionClusterStats::default();
104        assert_eq!(stats.original_bytes_total, 0);
105        assert_eq!(stats.compressed_bytes_total, 0);
106        assert_eq!(stats.bytes_saved_total, 0);
107        assert_eq!(stats.compression_ratio, 0.0);
108        assert_eq!(stats.compression_operations_total, 0);
109    }
110}