| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
use old_metrics_subscriber::{ |
| 13 |
AggregatedMetricsSubscriber, Attribution, MetricRecord, TelemetrySink, |
| 14 |
}; |
| 15 |
use s2n_tls::{ |
| 16 |
security::DEFAULT_TLS13, |
| 17 |
testing::{build_config, config_builder, TestPair}, |
| 18 |
}; |
| 19 |
use std::sync::{Arc, Mutex}; |
| 20 |
|
| 21 |
#[derive(Debug, Clone)] |
| 22 |
struct VecSink { |
| 23 |
records: Arc<Mutex<Vec<MetricRecord>>>, |
| 24 |
} |
| 25 |
|
| 26 |
impl VecSink { |
| 27 |
fn new() -> Self { |
| 28 |
Self { |
| 29 |
records: Arc::new(Mutex::new(Vec::new())), |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl TelemetrySink for VecSink { |
| 35 |
fn export_record(&self, record: &MetricRecord) { |
| 36 |
self.records.lock().unwrap().push(record.clone()); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#[test] |
| 43 |
fn old_event_subscriber_builds() { |
| 44 |
let sink = VecSink::new(); |
| 45 |
let attribution = Attribution { |
| 46 |
service: "test-service".to_owned(), |
| 47 |
resource: "test-resource".to_owned(), |
| 48 |
component: "test-component".to_owned(), |
| 49 |
}; |
| 50 |
let subscriber = AggregatedMetricsSubscriber::new(sink.clone(), attribution); |
| 51 |
|
| 52 |
let server_config = { |
| 53 |
let mut cfg = config_builder(&DEFAULT_TLS13).unwrap(); |
| 54 |
cfg.set_event_subscriber(subscriber.clone()).unwrap(); |
| 55 |
cfg.build().unwrap() |
| 56 |
}; |
| 57 |
let client_config = build_config(&DEFAULT_TLS13).unwrap(); |
| 58 |
|
| 59 |
let mut pair = TestPair::from_configs(&client_config, &server_config); |
| 60 |
pair.handshake().unwrap(); |
| 61 |
|
| 62 |
subscriber.finish_record(); |
| 63 |
|
| 64 |
let records = sink.records.lock().unwrap(); |
| 65 |
assert_eq!(records.len(), 1); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
let json: serde_json::Value = serde_json::to_value(&records[0]).unwrap(); |
| 70 |
let handshake = &json["handshake"]; |
| 71 |
|
| 72 |
|
| 73 |
assert_eq!(handshake["handshake_count"], 1); |
| 74 |
assert_eq!(handshake["synthetic_traffic_count"], 0); |
| 75 |
|
| 76 |
|
| 77 |
assert_eq!(total_count(&handshake["negotiated_protocols"]), 1); |
| 78 |
assert_eq!(total_count(&handshake["negotiated_ciphers"]), 1); |
| 79 |
assert_eq!(total_count(&handshake["negotiated_groups"]), 1); |
| 80 |
assert_eq!(total_count(&handshake["negotiated_signatures"]), 1); |
| 81 |
|
| 82 |
|
| 83 |
assert!(total_count(&handshake["supported_protocols"]) >= 1); |
| 84 |
assert!(total_count(&handshake["supported_ciphers"]) >= 1); |
| 85 |
assert!(total_count(&handshake["supported_groups"]) >= 1); |
| 86 |
assert!(total_count(&handshake["supported_signatures"]) >= 1); |
| 87 |
|
| 88 |
|
| 89 |
assert!(handshake["handshake_duration_us"].as_u64().unwrap() > 0); |
| 90 |
assert!(handshake["handshake_compute_us"].as_u64().unwrap() > 0); |
| 91 |
|
| 92 |
|
| 93 |
assert_eq!(json["attribution"]["service"], "test-service"); |
| 94 |
assert_eq!(json["attribution"]["resource"], "test-resource"); |
| 95 |
assert_eq!(json["attribution"]["component"], "test-component"); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
fn total_count(counter: &serde_json::Value) -> u64 { |
| 101 |
counter |
| 102 |
.as_array() |
| 103 |
.expect("counter should be an array") |
| 104 |
.iter() |
| 105 |
.filter_map(|entry| { |
| 106 |
let arr = entry.as_array()?; |
| 107 |
arr.last()?.as_u64() |
| 108 |
}) |
| 109 |
.sum() |
| 110 |
} |