zerodds_security_crypto/
metrics.rs1use std::sync::Arc;
10use std::time::Instant;
11
12use zerodds_monitor::{Counter, LabeledHistogram, Labels, default_registry, metric_names};
13
14fn op_counter(operation: &'static str) -> Arc<Counter> {
15 let r = default_registry();
16 r.set_help(
17 metric_names::DDS_SECURITY_CRYPTO_OPERATIONS_TOTAL,
18 "Crypto-Operationen (zerodds-monitor-1.0 §2.5)",
19 );
20 r.counter(
21 metric_names::DDS_SECURITY_CRYPTO_OPERATIONS_TOTAL,
22 Labels::new().with("operation", operation),
23 )
24}
25
26fn op_histogram(operation: &'static str) -> Arc<LabeledHistogram> {
27 let r = default_registry();
28 r.set_help(
29 metric_names::DDS_SECURITY_CRYPTO_LATENCY_SECONDS,
30 "Crypto-Latency (zerodds-monitor-1.0 §2.5)",
31 );
32 r.histogram(
33 metric_names::DDS_SECURITY_CRYPTO_LATENCY_SECONDS,
34 Labels::new().with("operation", operation),
35 )
36}
37
38pub struct CryptoOp {
41 operation: &'static str,
42 start: Instant,
43}
44
45impl CryptoOp {
46 pub fn start(operation: &'static str) -> Self {
48 Self {
49 operation,
50 start: Instant::now(),
51 }
52 }
53}
54
55impl Drop for CryptoOp {
56 fn drop(&mut self) {
57 op_counter(self.operation).inc();
58 let elapsed = self.start.elapsed();
59 let ns = elapsed.as_nanos().min(u64::MAX as u128) as u64;
60 op_histogram(self.operation).record_ns(ns);
61 }
62}