ai-vitals 0.7.0

A tool to monitor LLM endpoints and report status to Cronitor.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
//! # ai-vitals
//!
//! A monitoring tool for LLM endpoints that reports status to Cronitor.
//!
//! ## Running Tests
//!
//! ```bash
//! cargo test
//! ```
use anyhow::{Context, Result};
use chrono::Utc;
use clap::Parser;
use hostname::get;
use reqwest::Client;
use serde_json::json;
use std::time::Duration;
use tracing::{error, info};

/// State of a Cronitor ping
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum PingState {
    Run,
    Complete,
    Fail,
}

impl PingState {
    pub fn as_str(&self) -> &'static str {
        match self {
            PingState::Run => "run",
            PingState::Complete => "complete",
            PingState::Fail => "fail",
        }
    }
}

/// Type of LLM endpoint to probe
#[derive(Debug, Clone, Copy, PartialEq, clap::ValueEnum)]
pub enum EndpointType {
    #[value(name = "chat")]
    ChatCompletion,
    #[value(name = "embedding")]
    Embedding,
}

/// Configuration for the monitoring tool
#[derive(Parser, Debug, Clone, PartialEq)]
#[command(
    author,
    version,
    about,
    long_about = "Probe an LLM endpoint and report status to Cronitor."
)]
pub struct Config {
    /// Base URL for Cronitor, e.g. https://cronitor.link
    #[arg(long, env = "CRONITOR_BASE_URL")]
    pub cronitor_base_url: String,

    /// Base URL for Cronitor, e.g. https://cronitor.link
    #[arg(long, env = "CRONITOR_API_KEY")]
    pub cronitor_api_key: Option<String>,

    /// Monitor name / code in Cronitor
    #[arg(long, env = "MONITOR_NAME")]
    pub monitor_name: String,

    /// Base URL of the server to probe, e.g. https://my-openai-proxy
    #[arg(long, env = "SERVER_URL")]
    pub server_url: String,

    /// "chat" or "embedding"
    #[arg(long, env = "ENDPOINT_TYPE")]
    pub endpoint_type: EndpointType,

    /// Name of the model to query
    #[arg(long, env = "MODEL_NAME")]
    pub model_name: String,

    /// Environment descriptor (defaults to "production")
    #[arg(long, env = "APP_ENV", default_value = "production")]
    pub env: String,

    /// Request timeout in seconds (default 10)
    #[arg(long, env = "TIMEOUT_SECONDS", default_value_t = 10)]
    pub timeout_seconds: u64,

    
    /// The below all require an API key to be set to take effect.

    /// minFreqRequiredMins catches inactive alerts - if an alert starts but never completes, 
    /// it'll be marked as inactive by Cronitor. To force this into raising an alert,
    /// we require a successful ping once per any minFreqRequiredMins period. 
    #[arg(long, env = "MIN_SUCCESS_FREQ")]
    pub min_success_freq: Option<u8>,
    
    
    /// Which schedule to display in the frontend and to guide CONSECUTIVE_FAILURES_FOR_ALERT.
    #[arg(long, env = "SCHEDULE")]
    pub schedule: Option<String>,
    
    /// Optional: how many failed pings are needed to trigger an alert. Cronitor assumes 1 if unset.
    #[arg(long, env = "CONSECUTIVE_FAILURES_FOR_ALERT")]
    pub consecutive_failures: Option<u8>,

    /// Optional: how many missing pings are needed to trigger an alert. Cronitor disables this
    /// unless specified here as > 0. Requires schedule to be set.
    #[arg(long, env = "CONSECUTIVE_MISSING_FOR_ALERT")]
    pub consecutive_missing: Option<u8>,

    /// Optional: Group to put monitor in, mostly for frontend viewing.
    #[arg(long, env = "MONITOR_GROUP")]
    pub monitor_group: Option<String>,
}

impl Default for Config {
    fn default() -> Self {
        Config {
            cronitor_base_url: "https://cronitor.link".to_string(),
            cronitor_api_key: None,
            monitor_name: "test-monitor".to_string(),
            server_url: "https://api.openai.com".to_string(),
            endpoint_type: EndpointType::ChatCompletion,
            model_name: "gpt-4".to_string(),
            env: "test".to_string(),
            timeout_seconds: 10,
            schedule: Option::from("*/5 * * * *".to_string()),
            consecutive_failures: Some(1),
            min_success_freq: Some(60),
            monitor_group: None,
            consecutive_missing: Some(1),
        }
    }
}

/// Result of an LLM endpoint probe
#[derive(Debug, PartialEq)]
pub enum ProbeResult {
    Success,
    HttpError(u16),
    Timeout,
    NetworkError(String),
}

/// Cronitor client to send pings
pub struct CronitorClient {
    config: Config,
    client: Client,
    host: String,
    series_id: String,
}

impl CronitorClient {
    pub fn new(config: Config) -> Result<Self> {
        let client = Client::builder()
            .timeout(Duration::from_secs(config.timeout_seconds))
            .build()
            .context("building reqwest client")?;

        let host = get().unwrap_or_default().to_string_lossy().into_owned();
        let series_id = format!("{}-{}", Utc::now().timestamp(), std::process::id());

        info!("Starting job with series ID: {series_id}");

        Ok(CronitorClient {
            config,
            client,
            host,
            series_id,
        })
    }

    pub fn build_ping_url(
        &self,
        state: PingState,
        status_code: u16,
        message: Option<&str>,
    ) -> String {
        let mut url = format!(
            "{}/{}?state={}&series={}&status_code={}&env={}&host={}",
            self.config.cronitor_base_url,
            self.config.monitor_name,
            state.as_str(),
            self.series_id,
            status_code,
            self.config.env,
            self.host
        );
        if let Some(msg) = message {
            url.push_str("&message=");
            url.push_str(&urlencoding::encode(msg));
        }
        url
    }

    pub async fn ping(&self, state: PingState, status_code: u16, message: Option<&str>) {
        let url = self.build_ping_url(state, status_code, message);

        match self.client.get(&url).send().await {
            Ok(resp) if resp.status().is_success() => {
                // success: optionally peek at body for debugging
                info!("Cronitor ping OK");
            }
            Ok(resp) => {
                // non-2xx: log status + response body (often has the reason)
                let status = resp.status();
                let body = resp.text().await.unwrap_or_default(); // consumes resp
                error!("Cronitor ping non-2xx {status}: {body}");
            }
            Err(e) => {
                // request failed before a response was received
                error!("Failed to send ping to Cronitor: {e}");
            }
        }

        if state == PingState::Run {
            // The above handles the ping. We also want to update the created monitor if we can.

            let Some(api_key) = self.config.cronitor_api_key.as_deref() else {
                info!("No api key, skipping monitor enrichment");
                return; // no key => skip update
            };

            match self
                .client
                .put("https://cronitor.io/api/monitors")
                .basic_auth(api_key, Some("")) // username = API key, blank password
                .json(&self.get_monitor_update_payload())
                .send()
                .await
            {
                Ok(resp) if resp.status().is_success() => {
                    info!("Monitor enriched successful");
                }
                Ok(resp) => {
                    if !resp.status().is_success() {
                        error!(
                            "Monitor enrichment failed {}: {}",
                            resp.status(),
                            resp.text().await.unwrap_or_default()
                        );
                    }
                }
                Err(err) => {
                    error!("Failed to enrich Cronitor monitor: {err}");
                }
            }
        }
    }

    pub fn get_monitor_update_payload(&self) -> serde_json::Value {
        let mut monitor = serde_json::Map::new();
        monitor.insert("type".into(), json!("job"));
        monitor.insert("key".into(), json!(self.config.monitor_name));

        if let Some(consecutive_failures) = self.config.consecutive_failures.clone() {
            monitor.insert("failure_tolerance".into(), json!(consecutive_failures));
        }

        if let Some(schedule) = self.config.schedule.clone() {
            monitor.insert("schedule".into(), json!(schedule));
        }

        
        if let (Some(consecutive_missing), Some(_)) = (self.config.consecutive_missing, self.config.schedule.clone()) {
            monitor.insert("schedule_tolerance".into(), json!(consecutive_missing));
        }
        
        if let Some(group) = self.config.monitor_group.clone() {
            monitor.insert("group".into(), json!(group));
        }

        // always include the duration assertion
        let mut assertions: Vec<String> = vec![format!(
            "metric.duration < {}s",
            self.config.timeout_seconds * 2
        )];

        if let Some(min_success_freq) = self.config.min_success_freq.clone() {
            assertions.push(format!("job.completes < {} minute", min_success_freq));
        }
        monitor.insert("assertions".into(), json!(assertions));

        json!({ "monitors": [serde_json::Value::Object(monitor)] })
    }
}

/// LLM endpoint probe functionality
pub struct LLMProbe {
    client: Client,
    config: Config,
}

impl LLMProbe {
    pub fn new(config: Config) -> Result<Self> {
        let client = Client::builder()
            .timeout(Duration::from_secs(config.timeout_seconds))
            .build()
            .context("building reqwest client")?;

        Ok(LLMProbe { client, config })
    }

    pub fn build_endpoint_url(&self) -> String {
        match self.config.endpoint_type {
            EndpointType::ChatCompletion => {
                format!("{}/v1/chat/completions", self.config.server_url)
            }
            EndpointType::Embedding => format!("{}/v1/embeddings", self.config.server_url),
        }
    }

    pub fn build_payload(&self) -> serde_json::Value {
        match self.config.endpoint_type {
            EndpointType::ChatCompletion => json!({
                "model": self.config.model_name,
                "messages": [{ "role": "user", "content": "test" }],
                "max_tokens": 1,
                "extra_body": {"priority": -100}
            }),
            EndpointType::Embedding => json!({
                "model": self.config.model_name,
                "input": "test",
                "extra_body": {"priority": -100}
            }),
        }
    }

    pub async fn probe(&self) -> ProbeResult {
        let endpoint = self.build_endpoint_url();
        let payload = self.build_payload();

        info!("Querying {endpoint}");

        match self.client.post(&endpoint).json(&payload).send().await {
            Ok(resp) => {
                let status = resp.status();
                let body = resp.text().await.unwrap_or_default();
                info!("Response body: {body}");

                if status.is_success() {
                    ProbeResult::Success
                } else {
                    ProbeResult::HttpError(status.as_u16())
                }
            }
            Err(e) if e.is_timeout() => ProbeResult::Timeout,
            Err(e) => ProbeResult::NetworkError(e.to_string()),
        }
    }
}

/// Main monitoring orchestrator
pub struct Monitor {
    cronitor_client: CronitorClient,
    llm_probe: LLMProbe,
}

impl Monitor {
    pub fn new(config: Config) -> Result<Self> {
        let cronitor_client = CronitorClient::new(config.clone())?;
        let llm_probe = LLMProbe::new(config)?;

        Ok(Monitor {
            cronitor_client,
            llm_probe,
        })
    }

    pub async fn run(&self) -> i32 {
        // Send start ping
        info!("Sending start ping to Cronitor");
        self.cronitor_client.ping(PingState::Run, 0, None).await;

        // Probe the endpoint
        match self.llm_probe.probe().await {
            ProbeResult::Success => {
                info!("Sending success ping to Cronitor");
                self.cronitor_client
                    .ping(PingState::Complete, 0, None)
                    .await;
                info!("SUCCESS: Endpoint responded successfully");
                0
            }
            ProbeResult::HttpError(status_code) => {
                info!("Sending failure ping to Cronitor");
                self.cronitor_client
                    .ping(PingState::Fail, status_code, None)
                    .await;
                error!("FAILURE: Endpoint failed with HTTP {status_code}");
                1
            }
            ProbeResult::Timeout => {
                info!("Sending timeout ping to Cronitor");
                self.cronitor_client
                    .ping(PingState::Fail, 124, Some("Request timeout"))
                    .await;
                error!("TIMEOUT: Request timed out");
                124
            }
            ProbeResult::NetworkError(error) => {
                info!("Sending failure ping to Cronitor");
                self.cronitor_client
                    .ping(PingState::Fail, 1, Some(&format!("Network error: {error}")))
                    .await;
                error!("FAILURE: Network error: {error}");
                1
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use httpmock::prelude::*;
    use serde_json::json;

    #[test]
    fn test_cronitor_client_creation() {
        let config = Config::default();
        let client = CronitorClient::new(config);
        assert!(client.is_ok());
    }

    #[test]
    fn test_cronitor_ping_url_construction_without_message() {
        let config = Config::default();
        let client = CronitorClient::new(config).unwrap();

        let url = client.build_ping_url(PingState::Run, 0, None);

        assert!(url.contains("https://cronitor.link/test-monitor"));
        assert!(url.contains("state=run"));
        assert!(url.contains("status_code=0"));
        assert!(url.contains("env=test"));
        assert!(url.contains("series="));
        assert!(url.contains("host="));
        assert!(!url.contains("message="));
    }

    #[test]
    fn test_cronitor_ping_url_construction_with_message() {
        let config = Config::default();
        let client = CronitorClient::new(config).unwrap();

        let url = client.build_ping_url(PingState::Fail, 500, Some("Test error"));

        assert!(url.contains("https://cronitor.link/test-monitor"));
        assert!(url.contains("state=fail"));
        assert!(url.contains("status_code=500"));
        assert!(url.contains("env=test"));
        assert!(url.contains("message=Test%20error")); // URL encoded
    }

    #[test]
    fn test_cronitor_ping_url_special_characters() {
        let config = Config::default();
        let client = CronitorClient::new(config).unwrap();

        let url = client.build_ping_url(PingState::Fail, 500, Some("Error: 500 & timeout!"));

        assert!(url.contains("message=Error%3A%20500%20%26%20timeout%21"));
    }

    #[test]
    fn test_llm_probe_creation() {
        let config = Config::default();
        let probe = LLMProbe::new(config);
        assert!(probe.is_ok());
    }

    #[test]
    fn test_llm_probe_chat_endpoint_url() {
        let config = Config {
            endpoint_type: EndpointType::ChatCompletion,
            server_url: "https://api.openai.com".to_string(),
            ..Default::default()
        };
        let probe = LLMProbe::new(config).unwrap();

        let url = probe.build_endpoint_url();
        assert_eq!(url, "https://api.openai.com/v1/chat/completions");
    }

    #[test]
    fn test_llm_probe_embedding_endpoint_url() {
        let config = Config {
            endpoint_type: EndpointType::Embedding,
            server_url: "https://api.example.com".to_string(),
            ..Default::default()
        };
        let probe = LLMProbe::new(config).unwrap();

        let url = probe.build_endpoint_url();
        assert_eq!(url, "https://api.example.com/v1/embeddings");
    }

    #[test]
    fn test_llm_probe_chat_payload() {
        let config = Config {
            endpoint_type: EndpointType::ChatCompletion,
            model_name: "a-piece-of-cheese".to_string(),
            ..Default::default()
        };
        let probe = LLMProbe::new(config).unwrap();

        let payload = probe.build_payload();
        let expected = json!({
            "model": "a-piece-of-cheese",
            "messages": [{ "role": "user", "content": "test" }],
            "max_tokens": 1,
            "extra_body": {"priority": -100}
        });

        assert_eq!(payload, expected);
    }

    #[test]
    fn test_llm_probe_embedding_payload() {
        let config = Config {
            endpoint_type: EndpointType::Embedding,
            model_name: "text-embedding-ada-002".to_string(),
            ..Default::default()
        };
        let probe = LLMProbe::new(config).unwrap();

        let payload = probe.build_payload();
        let expected = json!({
            "model": "text-embedding-ada-002",
            "input": "test",
            "extra_body": {"priority": -100}
        });

        assert_eq!(payload, expected);
    }

    #[tokio::test]
    async fn test_llm_probe_successful_response() {
        let server = MockServer::start();

        // Mock successful LLM response
        let mock = server.mock(|when, then| {
            when.method(POST)
                .path("/v1/chat/completions")
                .json_body(json!({
                    "model": "gpt-4",
                    "messages": [{ "role": "user", "content": "test" }],
                    "max_tokens": 1,
                    "extra_body": {"priority": -100}
                }));
            then.status(200).json_body(json!({
                "choices": [{"message": {"role": "assistant", "content": "Hello"}}]
            }));
        });

        let config = Config {
            server_url: server.base_url(),
            endpoint_type: EndpointType::ChatCompletion,
            model_name: "gpt-4".to_string(),
            ..Default::default()
        };

        let probe = LLMProbe::new(config).unwrap();
        let result = probe.probe().await;

        assert_eq!(result, ProbeResult::Success);

        mock.assert();
    }

    #[tokio::test]
    async fn test_llm_probe_http_error_response() {
        let server = MockServer::start();

        // Mock failed LLM response
        let mock = server.mock(|when, then| {
            when.method(POST).path("/v1/embeddings");
            then.status(420).json_body(json!({
                "error": {"message": "Internal server error"}
            }));
        });

        let config = Config {
            server_url: server.base_url(),
            endpoint_type: EndpointType::Embedding,
            model_name: "text-embedding-ada-002".to_string(),
            ..Default::default()
        };

        let probe = LLMProbe::new(config).unwrap();
        let result = probe.probe().await;

        match result {
            ProbeResult::HttpError(status_code) => {
                assert_eq!(status_code, 420);
            }
            _ => panic!("Expected HTTP error probe result"),
        }

        mock.assert();
    }

    #[tokio::test]
    async fn test_llm_probe_timeout() {
        let config = Config {
            server_url: "http://10.255.255.1:12345".to_string(), // Non-routable IP for timeout
            timeout_seconds: 1,                                  // Very short timeout
            ..Default::default()
        };

        let probe = LLMProbe::new(config).unwrap();
        let result = probe.probe().await;

        assert!(matches!(result, ProbeResult::Timeout));
    }

    #[tokio::test]
    async fn test_llm_probe_network_error() {
        let config = Config {
            server_url: "http://localhost:99999".to_string(), // Invalid port
            ..Default::default()
        };

        let probe = LLMProbe::new(config).unwrap();
        let result = probe.probe().await;

        match result {
            ProbeResult::NetworkError(error) => {
                assert!(!error.is_empty());
            }
            _ => panic!("Expected network error probe result"),
        }
    }

    #[tokio::test]
    async fn test_monitor_creation() {
        let config = Config::default();
        let monitor = Monitor::new(config);
        assert!(monitor.is_ok());
    }

    #[tokio::test]
    async fn test_monitor_run_success() {
        let server = MockServer::start();

        // Mock successful LLM response
        let llm_mock = server.mock(|when, then| {
            when.method(POST).path("/v1/chat/completions");
            then.status(200).json_body(
                json!({"choices": [{"message": {"role": "assistant", "content": "OK"}}]}),
            );
        });

        // Mock Cronitor pings
        let cronitor_run_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "run");
            then.status(200);
        });

        let cronitor_complete_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "complete");
            then.status(200);
        });

        let config = Config {
            cronitor_base_url: server.base_url(),
            server_url: server.base_url(),
            ..Default::default()
        };

        let monitor = Monitor::new(config).unwrap();
        let exit_code = monitor.run().await;

        assert_eq!(exit_code, 0);
        llm_mock.assert();
        cronitor_run_mock.assert();
        cronitor_complete_mock.assert();
    }

    #[tokio::test]
    async fn test_monitor_run_http_error() {
        let server = MockServer::start();

        // Mock failed LLM response
        let llm_mock = server.mock(|when, then| {
            when.method(POST).path("/v1/chat/completions");
            then.status(500)
                .json_body(json!({"error": {"message": "Server error"}}));
        });

        // Mock Cronitor pings
        let cronitor_run_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "run");
            then.status(200);
        });

        let cronitor_fail_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "fail")
                .query_param("status_code", "500");
            then.status(200);
        });

        let config = Config {
            cronitor_base_url: server.base_url(),
            server_url: server.base_url(),
            ..Default::default()
        };

        let monitor = Monitor::new(config).unwrap();
        let exit_code = monitor.run().await;

        assert_eq!(exit_code, 1);
        llm_mock.assert();
        cronitor_run_mock.assert();
        cronitor_fail_mock.assert();
    }

    #[tokio::test]
    async fn test_monitor_run_timeout() {
        let server = MockServer::start();

        // Mock Cronitor pings
        let cronitor_run_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "run");
            then.status(200);
        });

        let cronitor_fail_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "fail")
                .query_param("status_code", "124")
                .query_param("message", "Request timeout");
            then.status(200);
        });

        let config = Config {
            cronitor_base_url: server.base_url(),
            server_url: "http://10.255.255.1:12345".to_string(), // Non-routable for timeout
            timeout_seconds: 1,
            ..Default::default()
        };

        let monitor = Monitor::new(config).unwrap();
        let exit_code = monitor.run().await;

        assert_eq!(exit_code, 124); // Timeout exit code
        cronitor_run_mock.assert();
        cronitor_fail_mock.assert();
    }

    #[tokio::test]
    async fn test_monitor_run_network_error() {
        let server = MockServer::start();

        // Mock Cronitor pings
        let cronitor_run_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "run");
            then.status(200);
        });

        let cronitor_fail_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "fail")
                .query_param("status_code", "1")
                .query_param_exists("message"); // Just check that message parameter exists
            then.status(200);
        });

        let config = Config {
            cronitor_base_url: server.base_url(),
            server_url: "http://localhost:99999".to_string(), // Invalid port
            ..Default::default()
        };

        let monitor = Monitor::new(config).unwrap();
        let exit_code = monitor.run().await;

        assert_eq!(exit_code, 1); // Network error exit code
        cronitor_run_mock.assert();
        cronitor_fail_mock.assert();
    }

    #[tokio::test]
    async fn test_monitor_cronitor_message_validation() {
        let server = MockServer::start();

        // Mock specific Cronitor ping calls with exact message validation
        let cronitor_run_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "run")
                .query_param("status_code", "0")
                .query_param("env", "test");
            then.status(200);
        });

        let cronitor_timeout_mock = server.mock(|when, then| {
            when.method(GET)
                .path("/test-monitor")
                .query_param("state", "fail")
                .query_param("status_code", "124")
                .query_param("message", "Request timeout")
                .query_param("env", "test");
            then.status(200);
        });

        let config = Config {
            cronitor_base_url: server.base_url(),
            server_url: "http://10.255.255.1:12345".to_string(), // Non-routable for timeout
            timeout_seconds: 1,
            ..Default::default()
        };

        let monitor = Monitor::new(config).unwrap();
        let exit_code = monitor.run().await;

        assert_eq!(exit_code, 124);
        cronitor_run_mock.assert();
        cronitor_timeout_mock.assert();
    }
}