apollo-router 2.14.0-rc.2

A configurable, high-performance routing runtime for Apollo Federation 🚀
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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
//! System information collection module
//!
//! This module handles the collection of comprehensive system information
//! for diagnostic purposes. The following information is collected and output:
//!
//! - **System Information**
//!   - Operating system
//!   - Architecture (x86_64, aarch64, etc.)
//!   - Target family (unix, windows, etc.)
//!   - Container environment detection (Docker, Podman, Kubernetes, generic containers)
//!
//! - **Rust & Build Information**
//!   - Router version
//!   - Rust version
//!   - Build type (Debug/Release)
//!   - Build profile
//!   - Target triple
//!   - Optimization level
//!
//! - **Memory Information**
//!   - Total memory
//!   - Available memory
//!   - Used memory
//!   - Free memory
//!   - Swap usage (total and used)
//!   - Linux-specific details from /proc/meminfo (Buffers, Cached, SReclaimable, Shmem, MemAvailable)
//!
//! - **Jemalloc Memory Statistics** (when available)
//!   - Allocated memory
//!   - Active memory
//!   - Mapped memory
//!   - Retained memory
//!   - Resident memory
//!   - Metadata memory
//!   - Number of arenas
//!   - Memory efficiency percentage (allocated/resident)
//!   - Memory utilization percentage (active/mapped)
//!
//! - **CPU Information**
//!   - Physical CPU cores
//!   - CPU model/brand
//!   - Average CPU frequency
//!   - Available parallelism (thread count)
//!   - Container/cgroup CPU limits (effective CPU count)
//!   - CPU priority/shares/weight (cgroup v1/v2)
//!   - Kubernetes CPU requests and limits
//!
//! - **System Load**
//!   - Load averages (1, 5, and 15 minutes)
//!   - Load per CPU
//!   - Individual CPU core usage percentages
//!   - Average CPU usage
//!
//! - **Environment Variables**
//!   - Apollo-specific configuration variables (APOLLO_GRAPH_REF)

use std::env::consts::ARCH;
use std::env::consts::OS;
use std::fmt;
use std::time::Duration;

use sysinfo::System;

use crate::plugins::diagnostics::DiagnosticsResult;

/// Complete system diagnostic information
struct SystemDiagnostics {
    basic_system: BasicSystemInfo,
    rust: RustInfo,
    memory: MemoryInfo,
    jemalloc: JemallocInfo,
    cpu: CpuInfo,
    system_load: SystemLoadInfo,
    environment: EnvironmentInfo,
}

impl SystemDiagnostics {
    /// Collect all system diagnostic information
    async fn new() -> Self {
        // Create a single System instance for all system info collection
        let mut system = System::new_all();

        Self {
            basic_system: BasicSystemInfo::new().await,
            rust: RustInfo::new(),
            memory: MemoryInfo::new(&system).await,
            jemalloc: JemallocInfo::new(),
            cpu: CpuInfo::new(&system).await,
            system_load: SystemLoadInfo::new(&mut system).await,
            environment: EnvironmentInfo::new(),
        }
    }
}

impl fmt::Display for SystemDiagnostics {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{}{}{}{}{}{}{}",
            self.basic_system,
            self.rust,
            self.memory,
            self.jemalloc,
            self.cpu,
            self.system_load,
            self.environment
        )
    }
}

/// Basic system information
struct BasicSystemInfo {
    operating_system: &'static str,
    architecture: &'static str,
    target_family: &'static str,
    container_environment: Vec<String>,
}

impl BasicSystemInfo {
    /// Collect basic system information
    async fn new() -> Self {
        let container_environment = Self::collect_container_environment_info().await;
        Self {
            operating_system: OS,
            architecture: ARCH,
            target_family: std::env::consts::FAMILY,
            container_environment,
        }
    }

    /// Enhanced container environment detection
    async fn collect_container_environment_info() -> Vec<String> {
        let mut container_indicators = Vec::new();

        // Docker
        if std::path::Path::new("/.dockerenv").exists() {
            container_indicators.push("Docker".to_string());
        }

        // Podman
        if std::path::Path::new("/run/.containerenv").exists() {
            container_indicators.push("Podman".to_string());
        }

        // Generic container indicators
        if std::env::var("container").is_ok() {
            container_indicators.push("Generic Container".to_string());
        }

        // Kubernetes
        if std::env::var("KUBERNETES_SERVICE_HOST").is_ok() {
            container_indicators.push("Kubernetes".to_string());
        }

        container_indicators
    }
}

impl fmt::Display for BasicSystemInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "SYSTEM INFORMATION")?;
        writeln!(f, "==================")?;
        writeln!(f)?;
        writeln!(f, "Operating System: {}", self.operating_system)?;
        writeln!(f, "Architecture: {}", self.architecture)?;
        writeln!(f, "Target Family: {}", self.target_family)?;

        if self.container_environment.is_empty() {
            writeln!(
                f,
                "Container Environment: Not detected (likely bare metal/VM)"
            )?;
        } else {
            writeln!(
                f,
                "Container Environment: {} detected",
                self.container_environment.join(", ")
            )?;
        }

        writeln!(f)
    }
}

/// Rust and build information
struct RustInfo {
    router_version: &'static str,
    rust_version: &'static str,
    build: BuildInfo,
}

impl RustInfo {
    /// Collect Rust and Cargo information
    fn new() -> Self {
        Self {
            router_version: env!("CARGO_PKG_VERSION"),
            rust_version: env!("CARGO_PKG_RUST_VERSION"),
            build: BuildInfo::new(),
        }
    }
}

impl fmt::Display for RustInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "Router Version: {}", self.router_version)?;
        writeln!(f, "Rust Version: {}", self.rust_version)?;
        writeln!(f, "{}", self.build)?;
        writeln!(f)
    }
}

/// Build and debug information
struct BuildInfo {
    build_type: &'static str,
    build_profile: Option<String>,
    target_triple: Option<String>,
    optimization_level: Option<String>,
}

impl BuildInfo {
    /// Collect build and debug symbol information
    fn new() -> Self {
        // Check if running in debug vs release mode
        let build_type = if cfg!(debug_assertions) {
            "Debug (with debug assertions)"
        } else {
            "Release (optimized)"
        };

        // Build profile information
        let build_profile = std::env::var("CARGO_BUILD_PROFILE").ok();

        // Target information
        let target_triple = std::env::var("CARGO_CFG_TARGET_TRIPLE").ok();
        let optimization_level = std::env::var("CARGO_CFG_OPT_LEVEL").ok();

        Self {
            build_type,
            build_profile,
            target_triple,
            optimization_level,
        }
    }
}

impl fmt::Display for BuildInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f)?;
        writeln!(f, "BUILD INFORMATION")?;
        writeln!(f, "-----------------")?;
        writeln!(f, "Build Type: {}", self.build_type)?;

        if let Some(ref profile) = self.build_profile {
            writeln!(f, "Build Profile: {}", profile)?;
        }
        if let Some(ref target) = self.target_triple {
            writeln!(f, "Target Triple: {}", target)?;
        }
        if let Some(ref opt_level) = self.optimization_level {
            writeln!(f, "Optimization Level: {}", opt_level)?;
        }
        Ok(())
    }
}

/// System memory information
struct MemoryInfo {
    total_memory: u64,
    available_memory: u64,
    used_memory: u64,
    free_memory: u64,
    total_swap: u64,
    used_swap: u64,
    detailed_linux_info: Option<String>,
}

impl MemoryInfo {
    /// Collect memory information using sysinfo for cross-platform support
    async fn new(system: &System) -> Self {
        let total_memory = system.total_memory();
        let available_memory = system.available_memory();
        let used_memory = system.used_memory();
        let free_memory = system.free_memory();
        let total_swap = system.total_swap();
        let used_swap = system.used_swap();

        // Add Linux-specific detailed info if available
        #[cfg(target_family = "unix")]
        let detailed_linux_info = {
            if let Ok(meminfo) = tokio::fs::read_to_string("/proc/meminfo").await {
                let mut detailed = String::new();
                for line in meminfo.lines().take(15) {
                    if line.starts_with("Buffers:")
                        || line.starts_with("Cached:")
                        || line.starts_with("SReclaimable:")
                        || line.starts_with("Shmem:")
                        || line.starts_with("MemAvailable:")
                    {
                        detailed.push_str(&format!("  {}\n", line));
                    }
                }
                Some(detailed)
            } else {
                None
            }
        };

        #[cfg(not(target_family = "unix"))]
        let detailed_linux_info = None;

        Self {
            total_memory,
            available_memory,
            used_memory,
            free_memory,
            total_swap,
            used_swap,
            detailed_linux_info,
        }
    }
}

impl fmt::Display for MemoryInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "MEMORY INFORMATION")?;
        writeln!(f, "------------------")?;

        writeln!(
            f,
            "Total Memory: {:.2} GB ({} bytes)",
            self.total_memory as f64 / (1024.0 * 1024.0 * 1024.0),
            self.total_memory
        )?;
        writeln!(
            f,
            "Available Memory: {:.2} GB ({} bytes)",
            self.available_memory as f64 / (1024.0 * 1024.0 * 1024.0),
            self.available_memory
        )?;
        writeln!(
            f,
            "Used Memory: {:.2} GB ({} bytes)",
            self.used_memory as f64 / (1024.0 * 1024.0 * 1024.0),
            self.used_memory
        )?;
        writeln!(
            f,
            "Free Memory: {:.2} GB ({} bytes)",
            self.free_memory as f64 / (1024.0 * 1024.0 * 1024.0),
            self.free_memory
        )?;

        if self.total_swap > 0 {
            writeln!(
                f,
                "Total Swap: {:.2} GB ({} bytes)",
                self.total_swap as f64 / (1024.0 * 1024.0 * 1024.0),
                self.total_swap
            )?;
            writeln!(
                f,
                "Used Swap: {:.2} GB ({} bytes)",
                self.used_swap as f64 / (1024.0 * 1024.0 * 1024.0),
                self.used_swap
            )?;
        } else {
            writeln!(f, "Swap: Not available or disabled")?;
        }

        if let Some(ref detailed_info) = self.detailed_linux_info {
            writeln!(f, "\nDetailed Memory Information (Linux /proc/meminfo):")?;
            write!(f, "{}", detailed_info)?;
        } else {
            writeln!(
                f,
                "\nDetailed Memory Information: not available on this platform"
            )?;
        }

        writeln!(f)
    }
}

/// Jemalloc memory statistics
struct JemallocInfo {
    available: bool,
    allocated: Option<u64>,
    active: Option<u64>,
    mapped: Option<u64>,
    retained: Option<u64>,
    resident: Option<u64>,
    metadata: Option<u64>,
    arenas: Option<u64>,
    memory_efficiency: Option<f64>,
    memory_utilization: Option<f64>,
}

impl JemallocInfo {
    /// Collect jemalloc memory statistics
    fn new() -> Self {
        #[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix))]
        {
            Self::collect_jemalloc_stats()
        }

        #[cfg(not(all(feature = "global-allocator", not(feature = "dhat-heap"), unix)))]
        {
            Self {
                available: false,
                allocated: None,
                active: None,
                mapped: None,
                retained: None,
                resident: None,
                metadata: None,
                arenas: None,
                memory_efficiency: None,
                memory_utilization: None,
            }
        }
    }

    /// Collect jemalloc memory statistics if available
    #[cfg(all(feature = "global-allocator", not(feature = "dhat-heap"), unix))]
    fn collect_jemalloc_stats() -> Self {
        // Advance epoch to get fresh statistics
        if let Err(_e) = tikv_jemalloc_ctl::epoch::advance() {
            return Self {
                available: true,
                allocated: None,
                active: None,
                mapped: None,
                retained: None,
                resident: None,
                metadata: None,
                arenas: None,
                memory_efficiency: None,
                memory_utilization: None,
            };
        }

        // Helper macro to read jemalloc stats
        macro_rules! read_stat {
            ($stat:ident) => {
                tikv_jemalloc_ctl::stats::$stat::read().ok()
            };
        }

        let allocated = read_stat!(allocated).map(|v| v as u64);
        let active = read_stat!(active).map(|v| v as u64);
        let mapped = read_stat!(mapped).map(|v| v as u64);
        let retained = read_stat!(retained).map(|v| v as u64);
        let resident = read_stat!(resident).map(|v| v as u64);
        let metadata = read_stat!(metadata).map(|v| v as u64);

        // Additional detailed statistics if available
        let arenas = tikv_jemalloc_ctl::arenas::narenas::read()
            .ok()
            .map(|v| v as u64);

        // Memory usage efficiency indicators
        let memory_efficiency = match (allocated, resident) {
            (Some(alloc), Some(res)) if res > 0 => Some((alloc as f64 / res as f64) * 100.0),
            _ => None,
        };

        let memory_utilization = match (active, mapped) {
            (Some(act), Some(map)) if map > 0 => Some((act as f64 / map as f64) * 100.0),
            _ => None,
        };

        Self {
            available: true,
            allocated,
            active,
            mapped,
            retained,
            resident,
            metadata,
            arenas,
            memory_efficiency,
            memory_utilization,
        }
    }
}

impl fmt::Display for JemallocInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "JEMALLOC MEMORY STATISTICS")?;
        writeln!(f, "-------------------------")?;

        if !self.available {
            writeln!(f, "Jemalloc statistics: not available on this platform")?;
            writeln!(f)?;
            return Ok(());
        }

        macro_rules! display_stat {
            ($field:ident, $description:expr) => {
                match self.$field {
                    Some(value) => {
                        writeln!(
                            f,
                            "{}: {:.2} MB ({} bytes)",
                            $description,
                            value as f64 / (1024.0 * 1024.0),
                            value
                        )?;
                    }
                    None => {
                        writeln!(f, "{}: Error reading", $description)?;
                    }
                }
            };
        }

        display_stat!(allocated, "Allocated Memory");
        display_stat!(active, "Active Memory");
        display_stat!(mapped, "Mapped Memory");
        display_stat!(retained, "Retained Memory");
        display_stat!(resident, "Resident Memory");
        display_stat!(metadata, "Metadata Memory");

        if let Some(arenas) = self.arenas {
            writeln!(f, "Number of Arenas: {}", arenas)?;
        }

        if let Some(efficiency) = self.memory_efficiency {
            writeln!(
                f,
                "Memory Efficiency: {:.1}% (allocated/resident)",
                efficiency
            )?;
        }

        if let Some(utilization) = self.memory_utilization {
            writeln!(f, "Memory Utilization: {:.1}% (active/mapped)", utilization)?;
        }

        writeln!(f)
    }
}

/// CPU information and container details
struct CpuInfo {
    physical_cores: usize,
    cpu_model: String,
    average_frequency: Option<u64>,
    available_parallelism: Option<usize>,
    container_cpu: ContainerCpuInfo,
}

impl CpuInfo {
    /// Collect CPU information using sysinfo and improved cgroup detection
    async fn new(system: &System) -> Self {
        let cpus = system.cpus();
        let cpu_count = cpus.len();

        let (cpu_model, average_frequency) = if !cpus.is_empty() {
            // Get CPU brand/model from first CPU
            let cpu = &cpus[0];
            let model = cpu.brand().to_string();

            // Calculate average frequency
            let total_freq: u64 = cpus.iter().map(|cpu| cpu.frequency()).sum();
            let avg_freq = if total_freq > 0 {
                Some(total_freq / cpu_count as u64)
            } else {
                None
            };

            (model, avg_freq)
        } else {
            (String::new(), None)
        };

        // Thread parallelism info
        let available_parallelism = std::thread::available_parallelism().map(|p| p.get()).ok();

        // Improved container/cgroup CPU information
        let container_cpu = ContainerCpuInfo::new(system).await;

        Self {
            physical_cores: cpu_count,
            cpu_model,
            average_frequency,
            available_parallelism,
            container_cpu,
        }
    }
}

impl fmt::Display for CpuInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "CPU INFORMATION")?;
        writeln!(f, "---------------")?;
        writeln!(f, "Physical CPU cores: {}", self.physical_cores)?;

        if !self.cpu_model.is_empty() {
            writeln!(f, "CPU Model: {}", self.cpu_model)?;
        }

        if let Some(freq) = self.average_frequency {
            writeln!(f, "Average CPU Frequency: {} MHz", freq)?;
        }

        match self.available_parallelism {
            Some(parallelism) => {
                writeln!(f, "Available Parallelism: {} threads", parallelism)?;
            }
            None => {
                writeln!(f, "Available Parallelism: Unknown")?;
            }
        }

        writeln!(f, "{}", self.container_cpu)?;
        writeln!(f)
    }
}

/// Container/Kubernetes CPU information
struct ContainerCpuInfo {
    effective_cpu_count: u64,
    detection_method: String,
    system_cpu_count: u64,
    cpu_priority: Option<String>,
    k8s_cpu_request: Option<String>,
    k8s_cpu_limit: Option<String>,
}

impl ContainerCpuInfo {
    /// Enhanced container/Kubernetes CPU resource information with improved cgroup detection
    async fn new(system: &System) -> Self {
        let system_cpu_count = system.cpus().len() as u64;
        let (detection_method, effective_cpu_count) =
            Self::detect_effective_cpu_count(system_cpu_count).await;

        // Check CPU shares/weight (relative priority)
        let cpu_priority = Self::collect_cpu_priority_info().await;

        // Check for Kubernetes downward API environment variables
        let (k8s_cpu_request, k8s_cpu_limit) = Self::collect_k8s_cpu_info();

        Self {
            effective_cpu_count,
            detection_method,
            system_cpu_count,
            cpu_priority,
            k8s_cpu_request,
            k8s_cpu_limit,
        }
    }

    /// Detect effective CPU count considering cgroup limits (improved from fleet detector)
    #[cfg(not(target_os = "linux"))]
    async fn detect_effective_cpu_count(system_cpus: u64) -> (String, u64) {
        ("system".to_string(), system_cpus)
    }

    #[cfg(target_os = "linux")]
    async fn detect_effective_cpu_count(system_cpus: u64) -> (String, u64) {
        // Determine cgroup version first
        match tokio::fs::read_to_string("/proc/filesystems")
            .await
            .map(|fs| Self::detect_cgroup_version(&fs))
        {
            Ok(CGroupVersion::V2) => {
                // cgroup v2: read from cpu.max
                match tokio::fs::read_to_string("/sys/fs/cgroup/cpu.max").await {
                    Ok(contents) => {
                        if contents.starts_with("max") {
                            ("system".to_string(), system_cpus)
                        } else {
                            match contents.split_once(' ') {
                                Some((quota, period)) => (
                                    "cgroup2".to_string(),
                                    Self::calculate_cpu_count_with_default(
                                        system_cpus,
                                        quota,
                                        period,
                                    ),
                                ),
                                None => ("system".to_string(), system_cpus),
                            }
                        }
                    }
                    Err(_) => ("system".to_string(), system_cpus),
                }
            }
            Ok(CGroupVersion::V1) => {
                // cgroup v1: read from separate quota and period files
                let quota = tokio::fs::read_to_string("/sys/fs/cgroup/cpu/cpu.cfs_quota_us")
                    .await
                    .ok();
                let period = tokio::fs::read_to_string("/sys/fs/cgroup/cpu/cpu.cfs_period_us")
                    .await
                    .ok();

                match (quota, period) {
                    (Some(quota), Some(period)) => {
                        let quota = quota.trim();
                        let period = period.trim();
                        if quota == "-1" {
                            ("system".to_string(), system_cpus)
                        } else {
                            (
                                "cgroup".to_string(),
                                Self::calculate_cpu_count_with_default(system_cpus, quota, period),
                            )
                        }
                    }
                    _ => ("system".to_string(), system_cpus),
                }
            }
            _ => ("system".to_string(), system_cpus),
        }
    }

    /// Detect cgroup version from /proc/filesystems content
    #[cfg(target_os = "linux")]
    fn detect_cgroup_version(filesystems: &str) -> CGroupVersion {
        use std::collections::HashSet;

        let versions: HashSet<_> = filesystems
            .lines()
            .flat_map(|line| line.split_whitespace())
            .filter(|word| word.contains("cgroup"))
            .collect();

        if versions.contains("cgroup2") {
            CGroupVersion::V2
        } else if versions.contains("cgroup") {
            CGroupVersion::V1
        } else {
            CGroupVersion::NotDetected
        }
    }

    /// Calculate CPU count from quota and period with fallback
    #[cfg(target_os = "linux")]
    fn calculate_cpu_count_with_default(default: u64, quota: &str, period: &str) -> u64 {
        match (quota.parse::<u64>(), period.parse::<u64>()) {
            (Ok(q), Ok(p)) if p > 0 => q / p,
            _ => default,
        }
    }

    /// Collect CPU priority information (shares/weight)
    async fn collect_cpu_priority_info() -> Option<String> {
        #[cfg(target_os = "linux")]
        {
            // cgroup v1 shares
            if let Ok(cpu_shares) = tokio::fs::read_to_string("/sys/fs/cgroup/cpu/cpu.shares").await
            {
                let shares = cpu_shares.trim();
                return Some(format!("CPU Shares (cgroup v1): {}", shares));
            }
            // cgroup v2 weight
            else if let Ok(cpu_weight) =
                tokio::fs::read_to_string("/sys/fs/cgroup/cpu.weight").await
            {
                let weight = cpu_weight.trim();
                return Some(format!("CPU Weight (cgroup v2): {}", weight));
            }
        }

        #[cfg(not(target_os = "linux"))]
        {
            // Return None for non-Linux platforms - the Display trait will handle this
        }

        None
    }

    /// Collect Kubernetes CPU resource information
    fn collect_k8s_cpu_info() -> (Option<String>, Option<String>) {
        let cpu_request = std::env::var("CPU_REQUEST").ok();
        let cpu_limit = std::env::var("CPU_LIMIT").ok();
        (cpu_request, cpu_limit)
    }
}

impl fmt::Display for ContainerCpuInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "\nContainer/Kubernetes CPU Information:")?;
        writeln!(
            f,
            "Effective CPU Count: {} (detected via: {})",
            self.effective_cpu_count, self.detection_method
        )?;

        if self.effective_cpu_count != self.system_cpu_count {
            writeln!(
                f,
                "Note: System reports {} CPUs, but container is limited to {}",
                self.system_cpu_count, self.effective_cpu_count
            )?;
        }

        if let Some(ref priority) = self.cpu_priority {
            writeln!(f, "{}", priority)?;
        } else {
            writeln!(
                f,
                "CPU Priority Information: not available on this platform"
            )?;
        }

        if let Some(ref request) = self.k8s_cpu_request {
            writeln!(f, "CPU Request (K8s): {}", request)?;
        }
        if let Some(ref limit) = self.k8s_cpu_limit {
            writeln!(f, "CPU Limit (K8s): {}", limit)?;
        }

        Ok(())
    }
}

/// System load and CPU usage information
struct SystemLoadInfo {
    load_available: bool,
    load_one: f64,
    load_five: f64,
    load_fifteen: f64,
    cpu_count: usize,
    individual_cpu_usage: Vec<f32>,
    average_cpu_usage: f32,
}

impl SystemLoadInfo {
    /// Collect system load information (cross-platform)
    async fn new(system: &mut System) -> Self {
        // Use sysinfo's cross-platform load average if available
        let load_avg = System::load_average();
        let cpu_count = system.cpus().len();
        let load_available = load_avg.one.is_finite() && load_avg.one >= 0.0;

        // Take initial CPU measurement
        system.refresh_cpu_usage();

        // Wait for CPU usage calculation
        tokio::time::sleep(Duration::from_millis(1000)).await;

        // Take second measurement
        system.refresh_cpu_usage();

        let cpus = system.cpus();
        let mut individual_cpu_usage = Vec::new();
        let mut total_usage = 0.0f32;

        for cpu in cpus.iter() {
            let usage = cpu.cpu_usage();
            total_usage += usage;
            individual_cpu_usage.push(usage);
        }

        let average_cpu_usage = if !cpus.is_empty() {
            total_usage / cpus.len() as f32
        } else {
            0.0
        };

        Self {
            load_available,
            load_one: load_avg.one,
            load_five: load_avg.five,
            load_fifteen: load_avg.fifteen,
            cpu_count,
            individual_cpu_usage,
            average_cpu_usage,
        }
    }
}

impl fmt::Display for SystemLoadInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "SYSTEM LOAD")?;
        writeln!(f, "-----------")?;

        if self.load_available {
            writeln!(f, "Load Average (1min): {:.2}", self.load_one)?;
            writeln!(f, "Load Average (5min): {:.2}", self.load_five)?;
            writeln!(f, "Load Average (15min): {:.2}", self.load_fifteen)?;

            if self.cpu_count > 0 {
                let load_per_cpu_1min = self.load_one / self.cpu_count as f64;
                writeln!(
                    f,
                    "Load per CPU (1min): {:.2} ({:.1}% utilization)",
                    load_per_cpu_1min,
                    load_per_cpu_1min * 100.0
                )?;
            }

            writeln!(f, "Individual CPU Core Usage:")?;
            for (i, usage) in self.individual_cpu_usage.iter().enumerate() {
                writeln!(f, "  CPU {}: {:.1}%", i, usage)?;
            }
            writeln!(f, "Average CPU Usage: {:.1}%", self.average_cpu_usage)?;
        } else {
            writeln!(f, "Load Average: Not available on this platform")?;
            writeln!(f, "CPU Usage (per core):")?;
            for (i, usage) in self.individual_cpu_usage.iter().enumerate() {
                writeln!(f, "  CPU {}: {:.1}%", i, usage)?;
            }
            writeln!(
                f,
                "Total CPU Usage (average): {:.1}%",
                self.average_cpu_usage
            )?;
        }

        writeln!(f)
    }
}

/// Environment variables information
struct EnvironmentInfo {
    relevant_vars: Vec<(String, String)>,
}

impl EnvironmentInfo {
    /// Collect relevant environment variables
    ///
    /// SECURITY: Limited environment variable exposure
    /// Only expose specifically allowlisted environment variables that are safe for diagnostics.
    /// Never expose variables that might contain secrets, tokens, passwords, or API keys.
    fn new() -> Self {
        // SECURITY: Explicit allowlist of safe environment variables
        // Only Apollo-specific configuration variables are included
        // DO NOT add variables that might contain secrets (API keys, passwords, etc.)
        let relevant_vars = ["APOLLO_GRAPH_REF"];
        let mut collected_vars = Vec::new();

        for var in &relevant_vars {
            if let Ok(value) = std::env::var(var) {
                // SECURITY: Truncate long values to prevent log injection or excessive output
                let display_value = if value.len() > 200 {
                    format!(
                        "{}... (truncated, {} chars total)",
                        &value[..200],
                        value.len()
                    )
                } else {
                    value
                };
                collected_vars.push((var.to_string(), display_value));
            }
        }

        Self {
            relevant_vars: collected_vars,
        }
    }
}

impl fmt::Display for EnvironmentInfo {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "RELEVANT ENVIRONMENT VARIABLES")?;
        writeln!(f, "------------------------------")?;

        if self.relevant_vars.is_empty() {
            writeln!(f, "No relevant Apollo environment variables set")?;
        } else {
            for (var, value) in &self.relevant_vars {
                writeln!(f, "{}: {}", var, value)?;
            }
        }

        writeln!(f)
    }
}

/// Collect system information
///
/// SECURITY WARNING: This function collects extensive system information that may be sensitive:
/// - Memory layout, CPU details, container environment
/// - Environment variables, filesystem paths, system architecture
/// - Should only be used in development/debugging environments with proper access controls
pub(crate) async fn collect() -> DiagnosticsResult<String> {
    let diagnostics = SystemDiagnostics::new().await;
    Ok(diagnostics.to_string())
}

/// CGroup version enumeration
#[cfg(target_os = "linux")]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CGroupVersion {
    /// CGroups v2 (unified hierarchy)
    V2,
    /// CGroups v1 (legacy)
    V1,
    /// CGroups not detected or not available
    NotDetected,
}

#[cfg(test)]
mod tests;