cipherstash_stats/
lib.rs

1use prometheus::{self, core::Collector, register_histogram, Histogram, IntCounter, TextEncoder};
2
3use lazy_static::lazy_static;
4use prometheus::register_int_counter;
5
6lazy_static! {
7    pub static ref ENCRYPTION_DURATION: Histogram = register_histogram!(
8        "cs_encryption_duration_seconds",
9        "Duration of encryption operations"
10    )
11    .unwrap();
12    pub static ref DECRYPTION_DURATION: Histogram = register_histogram!(
13        "cs_decryption_duration_seconds",
14        "Duration of decryption operations"
15    )
16    .unwrap();
17    pub static ref ENCRYPTIONS: IntCounter =
18        register_int_counter!("cs_encryptions", "Number of encryption ops")
19            .expect("Failed to register stats counter");
20    pub static ref ENCRYPTION_ERRORS: IntCounter =
21        register_int_counter!("cs_encryption_errors", "Number of encryption errors")
22            .expect("Failed to register stats counter");
23    pub static ref DECRYPTIONS: IntCounter =
24        register_int_counter!("cs_decryptions", "Number of decryption ops")
25            .expect("Failed to register stats counter");
26    pub static ref DECRYPTION_ERRORS: IntCounter =
27        register_int_counter!("cs_decryption_errors", "Number of decryption errors")
28            .expect("Failed to register stats counter");
29    pub static ref UNMAPPABLE_QUERIES: IntCounter =
30        register_int_counter!("cs_unmappable_queries", "Number of unmappable queries")
31            .expect("Failed to register stats counter");
32    pub static ref DATA_ACCESS_EVENTS: IntCounter =
33        register_int_counter!("cs_data_access_events", "Number of data access events ops")
34            .expect("Failed to register stats counter");
35    pub static ref ROWS_ACCESSED: IntCounter =
36        register_int_counter!("cs_rows_accessed", "Total number of rows accessed")
37            .expect("Failed to register stats counter");
38    pub static ref COLUMNS_ACCESSED: IntCounter =
39        register_int_counter!("cs_columns_accessed", "Total number of columns accessed")
40            .expect("Failed to register stats counter");
41    pub static ref STATEMENT_ERRORS: IntCounter =
42        register_int_counter!("cs_statement_errors", "Total number of errors accessed")
43            .expect("Failed to register stats counter");
44    pub static ref STATEMENT_DURATION_MS: Histogram = register_histogram!(
45        "cs_statement_duration_ms",
46        "Duration of statement operations"
47    )
48    .expect("Failed to register stats counter");
49    pub static ref ROWS_RETURNED_COUNT: IntCounter =
50        register_int_counter!("cs_rows_returned_count", "Total number of rows returned")
51            .expect("Failed to register stats counter");
52    pub static ref ROWS_UPDATED_COUNT: IntCounter =
53        register_int_counter!("cs_rows_updated_count", "Total number of rows updated")
54            .expect("Failed to register stats counter");
55}
56
57pub fn encode_to_string(prefix: &str) -> String {
58    let mut lines = Vec::new();
59    let metrics = vec![
60        ENCRYPTIONS.collect(),
61        ENCRYPTION_ERRORS.collect(),
62        DECRYPTIONS.collect(),
63        DECRYPTION_ERRORS.collect(),
64        UNMAPPABLE_QUERIES.collect(),
65        ENCRYPTION_DURATION.collect(),
66        DECRYPTION_DURATION.collect(),
67        DATA_ACCESS_EVENTS.collect(),
68        ROWS_ACCESSED.collect(),
69        COLUMNS_ACCESSED.collect(),
70        STATEMENT_ERRORS.collect(),
71        STATEMENT_DURATION_MS.collect(),
72        ROWS_RETURNED_COUNT.collect(),
73        ROWS_UPDATED_COUNT.collect(),
74    ];
75
76    let encoder = TextEncoder::new();
77    for mut metric in metrics {
78        for m in metric.iter_mut() {
79            let name = m.get_name();
80            let name = format!("{prefix}{name}");
81            m.set_name(name);
82        }
83
84        if let Ok(s) = encoder.encode_to_string(&metric) {
85            lines.push(s);
86        }
87    }
88
89    lines.join("\n")
90}