Skip to main content

telemetrylib/
lib.rs

1use lazy_static::lazy_static;
2use serde::Serialize;
3use std::sync::RwLock as StdRwLock;
4mod metrics_exporter_file;
5mod open_telemetry;
6mod span_exporter_file;
7
8pub use metrics_exporter_file::FileMetricExporter;
9pub use open_telemetry::*;
10pub use span_exporter_file::SpanExporterFile;
11
12#[derive(Default, Serialize)]
13#[allow(dead_code)]
14pub struct Telemetry {
15    /// Total number of connections opened to Valkey
16    total_connections: usize,
17    /// Total number of GLIDE clients
18    total_clients: usize,
19    /// Total number of values compressed
20    total_values_compressed: usize,
21    /// Total number of values decompressed
22    total_values_decompressed: usize,
23    /// Total original bytes before compression
24    total_original_bytes: usize,
25    /// Total bytes after compression
26    total_bytes_compressed: usize,
27    /// Total bytes after decompression
28    total_bytes_decompressed: usize,
29    /// Number of times compression was skipped
30    compression_skipped_count: usize,
31    /// Number of times subscriptions were detected as out of sync
32    subscription_out_of_sync_count: usize,
33    /// Unix timestamp (in milliseconds) of the last time subscriptions were in sync
34    subscription_last_sync_timestamp: u64,
35}
36
37lazy_static! {
38    static ref TELEMETRY: StdRwLock<Telemetry> = StdRwLock::<Telemetry>::default();
39}
40
41const MUTEX_WRITE_ERR: &str = "Failed to obtain write lock for mutex. Poisoned mutex";
42const MUTEX_READ_ERR: &str = "Failed to obtain read lock for mutex. Poisoned mutex";
43
44impl Telemetry {
45    /// Increment the total number of connections by `incr_by`
46    /// Return the number of total connections after the increment
47    pub fn incr_total_connections(incr_by: usize) -> usize {
48        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
49        t.total_connections = t.total_connections.saturating_add(incr_by);
50        t.total_connections
51    }
52
53    /// Decrease the total number of connections by `decr_by`
54    /// Return the number of total connections after the decrease
55    pub fn decr_total_connections(decr_by: usize) -> usize {
56        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
57        t.total_connections = t.total_connections.saturating_sub(decr_by);
58        t.total_connections
59    }
60
61    /// Increment the total number of clients by `incr_by`
62    /// Return the number of total clients after the increment
63    pub fn incr_total_clients(incr_by: usize) -> usize {
64        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
65        t.total_clients = t.total_clients.saturating_add(incr_by);
66        t.total_clients
67    }
68
69    /// Decrease the total number of clients by `decr_by`
70    /// Return the number of total clients after the decrease
71    pub fn decr_total_clients(decr_by: usize) -> usize {
72        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
73        t.total_clients = t.total_clients.saturating_sub(decr_by);
74        t.total_clients
75    }
76
77    /// Return the number of active connections
78    pub fn total_connections() -> usize {
79        TELEMETRY.read().expect(MUTEX_READ_ERR).total_connections
80    }
81
82    /// Return the number of active clients
83    pub fn total_clients() -> usize {
84        TELEMETRY.read().expect(MUTEX_READ_ERR).total_clients
85    }
86
87    /// Increment the total number of values compressed
88    pub fn incr_total_values_compressed(incr_by: usize) -> usize {
89        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
90        t.total_values_compressed = t.total_values_compressed.saturating_add(incr_by);
91        t.total_values_compressed
92    }
93
94    /// Return the total number of values compressed
95    pub fn total_values_compressed() -> usize {
96        TELEMETRY
97            .read()
98            .expect(MUTEX_READ_ERR)
99            .total_values_compressed
100    }
101
102    /// Increment the total number of values decompressed
103    pub fn incr_total_values_decompressed(incr_by: usize) -> usize {
104        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
105        t.total_values_decompressed = t.total_values_decompressed.saturating_add(incr_by);
106        t.total_values_decompressed
107    }
108
109    /// Return the total number of values decompressed
110    pub fn total_values_decompressed() -> usize {
111        TELEMETRY
112            .read()
113            .expect(MUTEX_READ_ERR)
114            .total_values_decompressed
115    }
116
117    /// Increment the total original bytes before compression
118    pub fn incr_total_original_bytes(incr_by: usize) -> usize {
119        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
120        t.total_original_bytes = t.total_original_bytes.saturating_add(incr_by);
121        t.total_original_bytes
122    }
123
124    /// Return the total original bytes before compression
125    pub fn total_original_bytes() -> usize {
126        TELEMETRY.read().expect(MUTEX_READ_ERR).total_original_bytes
127    }
128
129    /// Increment the total bytes after compression
130    pub fn incr_total_bytes_compressed(incr_by: usize) -> usize {
131        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
132        t.total_bytes_compressed = t.total_bytes_compressed.saturating_add(incr_by);
133        t.total_bytes_compressed
134    }
135
136    /// Return the total bytes after compression
137    pub fn total_bytes_compressed() -> usize {
138        TELEMETRY
139            .read()
140            .expect(MUTEX_READ_ERR)
141            .total_bytes_compressed
142    }
143
144    /// Increment the total bytes after decompression
145    pub fn incr_total_bytes_decompressed(incr_by: usize) -> usize {
146        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
147        t.total_bytes_decompressed = t.total_bytes_decompressed.saturating_add(incr_by);
148        t.total_bytes_decompressed
149    }
150
151    /// Return the total bytes after decompression
152    pub fn total_bytes_decompressed() -> usize {
153        TELEMETRY
154            .read()
155            .expect(MUTEX_READ_ERR)
156            .total_bytes_decompressed
157    }
158
159    /// Increment the compression skipped count
160    pub fn incr_compression_skipped_count(incr_by: usize) -> usize {
161        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
162        t.compression_skipped_count = t.compression_skipped_count.saturating_add(incr_by);
163        t.compression_skipped_count
164    }
165
166    /// Return the compression skipped count
167    pub fn compression_skipped_count() -> usize {
168        TELEMETRY
169            .read()
170            .expect(MUTEX_READ_ERR)
171            .compression_skipped_count
172    }
173    /// Increment the subscription out of sync count
174    /// Return the new count after increment
175    pub fn incr_subscription_out_of_sync() -> usize {
176        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
177        t.subscription_out_of_sync_count = t.subscription_out_of_sync_count.saturating_add(1);
178        t.subscription_out_of_sync_count
179    }
180
181    /// Get the current subscription out of sync count
182    pub fn subscription_out_of_sync_count() -> usize {
183        TELEMETRY
184            .read()
185            .expect(MUTEX_READ_ERR)
186            .subscription_out_of_sync_count
187    }
188
189    /// Update the subscription last sync timestamp
190    /// Return the new timestamp
191    pub fn update_subscription_last_sync_timestamp(timestamp: u64) -> u64 {
192        let mut t = TELEMETRY.write().expect(MUTEX_WRITE_ERR);
193        t.subscription_last_sync_timestamp = timestamp;
194        t.subscription_last_sync_timestamp
195    }
196
197    /// Get the subscription last sync timestamp
198    pub fn subscription_last_sync_timestamp() -> u64 {
199        TELEMETRY
200            .read()
201            .expect(MUTEX_READ_ERR)
202            .subscription_last_sync_timestamp
203    }
204
205    /// Reset the telemetry collected thus far
206    pub fn reset() {
207        *TELEMETRY.write().expect(MUTEX_WRITE_ERR) = Telemetry::default();
208    }
209}