use prometheus::{self, core::Collector, register_histogram, Histogram, IntCounter, TextEncoder};
use lazy_static::lazy_static;
use prometheus::register_int_counter;
lazy_static! {
pub static ref ENCRYPTION_DURATION: Histogram = register_histogram!(
"cs_encryption_duration_seconds",
"Duration of encryption operations"
)
.unwrap();
pub static ref DECRYPTION_DURATION: Histogram = register_histogram!(
"cs_decryption_duration_seconds",
"Duration of decryption operations"
)
.unwrap();
pub static ref ENCRYPTIONS: IntCounter =
register_int_counter!("cs_encryptions", "Number of encryption ops")
.expect("Failed to register stats counter");
pub static ref ENCRYPTION_ERRORS: IntCounter =
register_int_counter!("cs_encryption_errors", "Number of encryption errors")
.expect("Failed to register stats counter");
pub static ref DECRYPTIONS: IntCounter =
register_int_counter!("cs_decryptions", "Number of decryption ops")
.expect("Failed to register stats counter");
pub static ref DECRYPTION_ERRORS: IntCounter =
register_int_counter!("cs_decryption_errors", "Number of decryption errors")
.expect("Failed to register stats counter");
pub static ref UNMAPPABLE_QUERIES: IntCounter =
register_int_counter!("cs_unmappable_queries", "Number of unmappable queries")
.expect("Failed to register stats counter");
pub static ref DATA_ACCESS_EVENTS: IntCounter =
register_int_counter!("cs_data_access_events", "Number of data access events ops")
.expect("Failed to register stats counter");
pub static ref ROWS_ACCESSED: IntCounter =
register_int_counter!("cs_rows_accessed", "Total number of rows accessed")
.expect("Failed to register stats counter");
pub static ref COLUMNS_ACCESSED: IntCounter =
register_int_counter!("cs_columns_accessed", "Total number of columns accessed")
.expect("Failed to register stats counter");
pub static ref STATEMENT_ERRORS: IntCounter =
register_int_counter!("cs_statement_errors", "Total number of errors accessed")
.expect("Failed to register stats counter");
pub static ref STATEMENT_DURATION_MS: Histogram = register_histogram!(
"cs_statement_duration_ms",
"Duration of statement operations"
)
.expect("Failed to register stats counter");
pub static ref ROWS_RETURNED_COUNT: IntCounter =
register_int_counter!("cs_rows_returned_count", "Total number of rows returned")
.expect("Failed to register stats counter");
pub static ref ROWS_UPDATED_COUNT: IntCounter =
register_int_counter!("cs_rows_updated_count", "Total number of rows updated")
.expect("Failed to register stats counter");
}
pub fn encode_to_string(prefix: &str) -> String {
let mut lines = Vec::new();
let metrics = vec![
ENCRYPTIONS.collect(),
ENCRYPTION_ERRORS.collect(),
DECRYPTIONS.collect(),
DECRYPTION_ERRORS.collect(),
UNMAPPABLE_QUERIES.collect(),
ENCRYPTION_DURATION.collect(),
DECRYPTION_DURATION.collect(),
DATA_ACCESS_EVENTS.collect(),
ROWS_ACCESSED.collect(),
COLUMNS_ACCESSED.collect(),
STATEMENT_ERRORS.collect(),
STATEMENT_DURATION_MS.collect(),
ROWS_RETURNED_COUNT.collect(),
ROWS_UPDATED_COUNT.collect(),
];
let encoder = TextEncoder::new();
for mut metric in metrics {
for m in metric.iter_mut() {
let name = m.get_name();
let name = format!("{prefix}{name}");
m.set_name(name);
}
if let Ok(s) = encoder.encode_to_string(&metric) {
lines.push(s);
}
}
lines.join("\n")
}