antithesis_sdk/
lifecycle.rs

1use crate::internal;
2use serde::Serialize;
3use serde_json::{json, Value};
4
5#[derive(Serialize, Debug)]
6struct AntithesisSetupData<'a, 'b> {
7    status: &'a str,
8    details: &'b Value,
9}
10
11#[derive(Serialize, Debug)]
12struct SetupCompleteData<'a> {
13    antithesis_setup: AntithesisSetupData<'a, 'a>,
14}
15
16/// Indicates to Antithesis that setup has completed. Call this function when your system and workload are fully initialized.
17/// After this function is called, Antithesis will take a snapshot of your system and begin [injecting faults]( https://antithesis.com/docs/environment/fault_injection/).
18///
19/// Calling this function multiple times or from multiple processes will have no effect.
20/// Antithesis will treat the first time any process called this function as the moment that the setup was completed.
21///
22/// # Example
23///
24/// ```
25/// use serde_json::{json, Value};
26/// use antithesis_sdk::lifecycle;
27///
28/// let (num_nodes, main_id) = (10, "n-001");
29///
30/// let startup_data: Value = json!({
31///     "num_nodes": num_nodes,
32///     "main_node_id": main_id,
33/// });
34///
35/// lifecycle::setup_complete(&startup_data);
36/// ```
37pub fn setup_complete(details: &Value) {
38    let status = "complete";
39    let antithesis_setup = AntithesisSetupData::<'_, '_> { status, details };
40
41    let setup_complete_data = SetupCompleteData { antithesis_setup };
42
43    internal::dispatch_output(&setup_complete_data)
44}
45
46/// Indicates to Antithesis that a certain event has been reached. It sends a structured log message to Antithesis that you may later use to aid debugging.
47///
48/// In addition to ``details``, you also provide ``name``, which is the name of the event that you are logging.
49///
50/// # Example
51///
52/// ```
53/// use serde_json::{json, Value};
54/// use antithesis_sdk::lifecycle;
55///
56/// let info_value: Value = json!({
57///     "month": "July",
58///     "day": 17
59/// });
60///
61/// lifecycle::send_event("start_day", &info_value);
62/// ```
63pub fn send_event(name: &str, details: &Value) {
64    // The name is passed through verbatim, like every other SDK: renaming
65    // or trimming here would make the same program emit different events
66    // depending on which SDK it was written against.
67    let json_event = json!({ name: details });
68    internal::dispatch_output(&json_event)
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn setup_complete_without_details() {
77        eprintln!("setup_complete");
78        let details: Value = json!({});
79        setup_complete(&details);
80    }
81
82    #[test]
83    fn setup_complete_with_details() {
84        let details: Value = json!({
85            "name": "Meow Cat",
86            "age": 11,
87            "phones": [
88                "+1 2126581356",
89                "+1 2126581384"
90            ]
91        });
92        setup_complete(&details);
93    }
94
95    #[test]
96    fn send_event_without_details() {
97        let details: Value = json!({});
98        send_event("my event", &details);
99    }
100
101    #[test]
102    fn send_event_with_details() {
103        let details: Value = json!({
104            "name": "Tweety Bird",
105            "age": 4,
106            "phones": [
107                "+1 9734970340"
108            ]
109        });
110        send_event("my event 2", &details);
111    }
112
113    #[test]
114    fn send_event_unnamed_without_details() {
115        let details: Value = json!({});
116        send_event("", &details);
117    }
118
119    #[test]
120    fn send_event_unnamed_with_details() {
121        let details: Value = json!({
122            "color": "red"
123        });
124        send_event("   ", &details);
125    }
126}