statsig_rust/sdk_diagnostics/diagnostics_utils.rs
1use super::{diagnostics::ContextType, marker::Marker};
2use std::collections::HashMap;
3
4pub struct DiagnosticsUtils;
5
6impl DiagnosticsUtils {
7 pub fn format_diagnostics_metadata(
8 context_type: &ContextType,
9 markers: &Vec<Marker>,
10 ) -> Result<HashMap<String, String>, String> {
11 let mut metadata = HashMap::new();
12
13 metadata.insert("context".to_string(), context_type.to_string());
14
15 match serde_json::to_string(&markers) {
16 Ok(markers_json) => {
17 metadata.insert("markers".to_string(), markers_json);
18 Ok(metadata)
19 }
20 Err(err) => Err(format!("Failed to serialize markers: {err}")),
21 }
22 }
23}
24
25// #[cfg(test)]
26// mod tests {
27// use super::*;
28// use crate::sdk_diagnostics::{
29// diagnostics::ContextType,
30// marker::{ActionType, KeyType, Marker, StepType},
31// };
32
33// #[test]
34// fn test_format_diagnostics_metadata_with_valid_markers() {
35// let start_timestamp: u64 = 1_640_995_200_000; // 2022-01-01T00:00:00Z
36// let end_timestamp: u64 = 1_640_995_800_000; // 10 minutes later
37
38// let start_marker = Marker::new(
39// KeyType::Initialize,
40// ActionType::Start,
41// Some(StepType::Process),
42// start_timestamp,
43// )
44// .with_is_success(true);
45
46// let end_marker = Marker::new(
47// KeyType::Initialize,
48// ActionType::End,
49// Some(StepType::Process),
50// end_timestamp,
51// )
52// .with_status_code(200);
53
54// let markers = vec![start_marker, end_marker];
55// let context_type = ContextType::Initialize;
56// let metadata = DiagnosticsUtils::format_diagnostics_metadata(&context_type, &markers)
57// .expect("format_diagnostics_metadata returned an Err");
58// let markers_json = metadata
59// .get("markers")
60// .expect("Markers not found in metadata");
61// let parsed_markers: serde_json::Value = serde_json::from_str(markers_json)
62// .expect("Failed to parse markers JSON into serde_json::Value");
63
64// let expected_json = serde_json::json!([
65// {
66// "key": "initialize",
67// "action": "start",
68// "step": "process",
69// "success": true,
70// "timestamp": start_timestamp,
71// },
72// {
73// "key": "initialize",
74// "action": "end",
75// "step": "process",
76// "timestamp": end_timestamp,
77// "statusCode": 200,
78// }
79// ]);
80
81// assert_eq!(
82// metadata
83// .get("context")
84// .expect("Context not found in metadata"),
85// "initialize"
86// );
87// assert_eq!(parsed_markers, expected_json);
88// }
89// }