fuel-telemetry 0.1.1

A tracing library to implement Fuel telemetry
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
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
use crate::{
    self as fuel_telemetry, daemonise, enforce_singleton, info, into_recoverable, span,
    telemetry_config, telemetry_formatter, EnvSetting, Level, Result, TelemetryError,
    WatcherResult,
};

use nix::{
    fcntl::{Flock, FlockArg},
    sys::{
        signal::{kill, Signal::SIGKILL},
        stat::fstat,
    },
    time::ClockId,
    unistd::{getpid, Pid},
};
use std::{
    env::{set_var, var},
    fs::{File, OpenOptions},
    io::Write,
    os::fd::{AsRawFd, RawFd},
    path::{Path, PathBuf},
    process::exit,
    sync::{
        atomic::{AtomicBool, AtomicI32, Ordering},
        LazyLock,
    },
    time::Duration,
};
use sysinfo::{MemoryRefreshKind, System};
use tracing_subscriber::{prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt};

const PROCESS_NAME: &str = "telemetry-systeminfo-watcher";
const TELEMETRY_PKG_NAME: &str = "systeminfo_watcher";

#[derive(Debug, Clone)]
struct SystemInfoWatcherConfig {
    // The path to its lockfile
    lockfile: PathBuf,
    // The path to its logfile
    logfile: PathBuf,
    // The timeout for the cloud provider metadata request
    metadata_timeout: u64,
    // The path to its touchfile
    touchfile: PathBuf,
    // The number of seconds to wait between collecting metrics
    interval: u64,
}

fn config() -> Result<&'static SystemInfoWatcherConfig> {
    static SYSTEMINFO_WATCHER_CONFIG: LazyLock<Result<SystemInfoWatcherConfig>> =
        LazyLock::new(|| {
            let get_env = |key, default| EnvSetting::new(key, default).get();

            Ok(SystemInfoWatcherConfig {
                // 60*60*24*30 = 2592000 (30 days)
                interval: get_env("SYSTEMINFO_WATCHER_INTERVAL", "2592000").parse()?,
                metadata_timeout: get_env("METADATA_TIMEOUT", "3").parse()?,
                lockfile: Path::new(&telemetry_config()?.fuelup_tmp)
                    .join(format!("{PROCESS_NAME}.lock")),
                logfile: Path::new(&telemetry_config()?.fuelup_log)
                    .join(format!("{PROCESS_NAME}.log")),
                touchfile: Path::new(&telemetry_config()?.fuelup_tmp)
                    .join(format!("{PROCESS_NAME}.touch")),
            })
        });

    SYSTEMINFO_WATCHER_CONFIG
        .as_ref()
        .map_err(|e| TelemetryError::InvalidConfig(e.to_string()))
}

// Although there's no need to keep the state of this watcher, keep it consistent
// with the other watchers

#[derive(Default)]
pub struct SystemInfoWatcher;

// Prevent recursive calls to start()
static STARTED: AtomicBool = AtomicBool::new(false);

/// The PID of the currently running `SystemInfoWatcher` daemon
pub static PID: AtomicI32 = AtomicI32::new(0);

impl SystemInfoWatcher {
    pub fn new() -> Self {
        Self
    }

    pub fn start(&mut self) -> WatcherResult<()> {
        self.start_with_helpers(&DefaultStartHelpers)
    }

    fn start_with_helpers(&mut self, helpers: &impl StartHelpers) -> WatcherResult<()> {
        if var("FUELUP_NO_TELEMETRY").is_ok() {
            // If telemetry is disabled, immediately return
            return Ok(());
        }

        let logfile = &config().map_err(into_recoverable)?.logfile;

        if STARTED.load(Ordering::Relaxed) {
            return Ok(());
        } else {
            STARTED.store(true, Ordering::Relaxed);
        }

        // Even though we won't be hanging around long, we still daemonise
        // so that we don't get in the way of the calling process
        match helpers.daemonise(logfile) {
            Ok(Some(pid)) => {
                // We are the parent, so record the PID then immediately return
                PID.store(pid.as_raw(), Ordering::Relaxed);
                return Ok(());
            }
            Err(e) => {
                // Couldn't daemonise, so clear globals before returning the error
                STARTED.store(false, Ordering::Relaxed);
                PID.store(0, Ordering::Relaxed);
                return Err(e);
            }
            Ok(None) => {
                // We are the child process, so continue as the `SystemInfoWatcher`...
                PID.store(getpid().as_raw(), Ordering::Relaxed);
            }
        }

        // From here on, we are no longer the original process, so the caller should
        // treat errors as fatal. This means that on error the process should exit
        // immediately as there should not be two identical flows of execution

        // As we record system metrics via a `TelemetryLayer`, we will need our
        // own `tracing` `Subscriber` as the orginial `Subscriber` lives in a
        // thread within a different process space as we've since daemonised.
        //
        // Warning: We need to create the `TelemetryLayer` after daemonising
        // as there is a race condition in the thread runtime of `tracing` and
        // the tokio runtime of `Reqwest`. Swapping order of the two could lead to
        // possible deadlocks.
        //
        // Also, we need to set the bucket name as the SystemInfoWatcher is
        // system-wide rather than being crate/process specific
        set_var("TELEMETRY_PKG_NAME", TELEMETRY_PKG_NAME);
        let (telemetry_layer, _guard) = helpers.new_fuel_telemetry()?;
        tracing_subscriber::registry().with(telemetry_layer).init();

        // Enforce a singleton to ensure we are the only process submitting
        // telemetry to InfluxDB
        let _lock = helpers.enforce_singleton(&config()?.lockfile)?;

        // Check if it's time to collect metrics
        helpers.poll_systeminfo(self)?;

        helpers.exit(0)
    }

    /// Kill the `SystemInfoWatcher` daemon if one is running
    pub fn kill() -> Result<bool> {
        let pid = PID.load(Ordering::Relaxed);

        if pid > 0 {
            kill(Pid::from_raw(pid), SIGKILL)?;
            PID.store(0, Ordering::Relaxed);
            return Ok(true);
        }

        Ok(false)
    }

    fn poll_systeminfo(&self) -> Result<()> {
        self.poll_systeminfo_with_helpers(&mut DefaultPollSysteminfoHelpers)
    }

    fn poll_systeminfo_with_helpers(&self, helpers: &mut impl PollSysteminfoHelpers) -> Result<()> {
        // If the lockfile is not found, create it and continue. Otherwise,
        // check its modification time and return if it's too recent
        let touchfile_lock = if !config()?.touchfile.exists() {
            helpers.create_and_lock_touchfile(&config()?.touchfile)?
        } else {
            let locked_file = helpers.open_and_lock_touchfile(&config()?.touchfile)?;
            let now = helpers.now()?;

            if now.tv_sec()
                < helpers.fstat(locked_file.as_raw_fd())?.st_mtime + config()?.interval as i64
            {
                // We must have collected metrics recently, so return
                return Ok(());
            }

            locked_file
        };

        let mut sysinfo = System::new();
        //
        // CPU metrics
        //

        // Need to refresh twice with delay in between so that there are enough
        // data points for "cpu_usage". See `sysinfo` docs for more details.
        sysinfo.refresh_cpu_usage();
        std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
        sysinfo.refresh_cpu_usage();

        let cpus = sysinfo.cpus();
        let cpu_count = cpus.len();
        let cpu_brand = cpus
            .first()
            .map_or_else(String::default, |cpu| cpu.brand().into());

        //
        // Memory and OS metrics
        //

        sysinfo.refresh_memory_specifics(MemoryRefreshKind::nothing().with_ram());

        let total_memory = sysinfo.total_memory();
        let free_memory = sysinfo.used_memory();
        let load_average = System::load_average();

        //
        // Build the line protocol and write to disk
        //

        #[allow(clippy::cast_precision_loss)]
        // These values are just informational, so we can take a precision loss
        let free_memory_percentage = (free_memory as f64) / (total_memory as f64);

        // Detect if we're not running on bare metal
        let ci = detect_ci();
        let vm = helpers.detect_vm()?;

        let span = span!(Level::INFO, "poll_systeminfo", telemetry = true);
        let _guard = span.enter();

        info!(
            cpu_arch = System::cpu_arch(),
            cpu_brand = cpu_brand,
            cpu_count = cpu_count,
            global_cpu_usage = (sysinfo.global_cpu_usage() as f64 * 100.0).trunc() / 100.0,
            total_memory = total_memory,
            free_memory = free_memory,
            free_memory_percentage = (free_memory_percentage * 100.0).trunc() / 100.0,
            os_long_name = System::long_os_version().unwrap_or_default(),
            kernel_version = System::kernel_version().unwrap_or_default(),
            uptime = System::uptime(),
            vm = vm,
            ci = ci,
            load_average_1m = (load_average.one * 100.0).trunc() / 100.0,
            load_average_5m = (load_average.five * 100.0).trunc() / 100.0,
            load_average_15m = (load_average.fifteen * 100.0).trunc() / 100.0,
        );

        // Update the touchfile's modification time by truncating it
        helpers.set_len(&touchfile_lock, 0)?;
        helpers.sync_all(&touchfile_lock)?;

        Ok(())
    }

    /// Last resort logging of errors, only to be used by the caller when there
    /// is no other way to report an error.
    pub fn log_error(message: &str) -> Result<()> {
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(&config()?.logfile)?;

        Ok(writeln!(file, "{message}")?)
    }
}

trait StartHelpers {
    fn daemonise(&self, logfile: &PathBuf) -> WatcherResult<Option<Pid>> {
        daemonise(logfile)
    }

    #[allow(clippy::type_complexity)]
    fn new_fuel_telemetry(
        &self,
    ) -> Result<(
        tracing_subscriber::filter::Filtered<
            tracing_subscriber::fmt::Layer<
                tracing_subscriber::Registry,
                tracing_subscriber::fmt::format::DefaultFields,
                telemetry_formatter::TelemetryFormatter,
                tracing_appender::non_blocking::NonBlocking,
            >,
            tracing_subscriber::EnvFilter,
            tracing_subscriber::Registry,
        >,
        tracing_appender::non_blocking::WorkerGuard,
    )> {
        fuel_telemetry::new!()
    }

    fn enforce_singleton(&self, lockfile_path: &Path) -> Result<Flock<File>> {
        enforce_singleton(lockfile_path)
    }

    fn poll_systeminfo(&self, systeminfo_watcher: &SystemInfoWatcher) -> Result<()> {
        systeminfo_watcher.poll_systeminfo()
    }

    fn exit(&self, code: i32) -> ! {
        exit(code)
    }
}

struct DefaultStartHelpers;
impl StartHelpers for DefaultStartHelpers {}

trait PollSysteminfoHelpers {
    fn create_and_lock_touchfile(&self, touchfile: &Path) -> Result<Flock<File>> {
        Ok(Flock::lock(
            OpenOptions::new()
                .create(true)
                .append(true)
                .open(touchfile)?,
            FlockArg::LockExclusiveNonblock,
        )
        .map_err(|(_, e)| e)?)
    }

    fn open_and_lock_touchfile(&self, touchfile: &Path) -> Result<Flock<File>> {
        Ok(Flock::lock(
            OpenOptions::new()
                .create(false)
                .append(true)
                .open(touchfile)?,
            FlockArg::LockExclusiveNonblock,
        )
        .map_err(|(_, e)| e)?)
    }

    fn now(&self) -> nix::Result<nix::sys::time::TimeSpec> {
        ClockId::CLOCK_REALTIME.now()
    }

    fn fstat(&self, fd: RawFd) -> nix::Result<libc::stat> {
        fstat(fd)
    }

    fn detect_vm(&self) -> Result<&'static str> {
        detect_vm()
    }

    fn set_len(&self, flock: &Flock<File>, len: u64) -> std::io::Result<()> {
        flock.set_len(len)
    }

    fn sync_all(&self, flock: &Flock<File>) -> std::io::Result<()> {
        flock.sync_all()
    }
}

struct DefaultPollSysteminfoHelpers;
impl PollSysteminfoHelpers for DefaultPollSysteminfoHelpers {}

fn detect_ci() -> &'static str {
    // Check if we are running in any type of CI
    let ci_environments = [
        ("GITHUB_ACTIONS", "GitHub Actions"),
        ("GITLAB_CI", "GitLab CI"),
        ("CODEBUILD_BUILD_NUMBER", "AWS CodePipeline"),
        ("CLOUD_RUN_JOB", "Google Cloud Build"),
        ("TF_BUILD", "Azure Pipelines"),
        ("CIRCLECI", "CircleCI"),
        ("TEAMCITY_VERSION", "TeamCity"),
        ("BITBUCKET_BUILD_NUMBER", "BitBucket Pipelines"),
        ("TRAVIS", "Travis CI"),
        ("JENKINS_URL", "Jenkins"),
    ];

    for (env_var, ci_name) in ci_environments {
        if var(env_var).is_ok() {
            return ci_name;
        }
    }
    ""
}

fn detect_vm() -> Result<&'static str> {
    // Check if we are running in a container
    let container_environments = [
        ("/.dockerenv", "Docker"),
        ("/.lxc-private", "LXD"),
        ("/.lxc", "LXD"),
        ("/.podman-private", "Podman"),
    ];

    for (env_var, container_name) in container_environments {
        if Path::new(env_var).exists() {
            return Ok(container_name);
        }
    }

    // Check if we are running in AWS Lambda
    if var("LAMBDA_TASK_ROOT").is_ok() {
        return Ok("AWS Lambda");
    }

    // Check for a cloud provider
    //
    // These are static values and are not configurable. If they change, the
    // structure of the call may need to change as has happened in the past
    let cloud_checks = [
        (
            "http://metadata.google.internal/computeMetadata/v1/instance/id",
            vec![("Metadata-Flavor", "Google")],
            "GCP GCE",
        ),
        (
            "http://169.254.169.254/latest/api/token",
            vec![("X-aws-ec2-metadata-token-ttl-seconds", "21600")],
            "AWS EC2",
        ),
        (
            "http://169.254.169.254/metadata/instance?api-version=2021-02-01",
            vec![("Metadata", "true")],
            "Azure",
        ),
    ];

    let client = reqwest::blocking::Client::new();
    let timeout = Duration::from_secs(config()?.metadata_timeout);

    for (url, headers, provider) in cloud_checks {
        let mut request = client.get(url).timeout(timeout);

        for (key, value) in headers {
            request = request.header(key, value);
        }

        if request.send().is_ok_and(|r| r.status().is_success()) {
            return Ok(provider);
        }
    }

    //  Check for a VM vendor
    let vm_vendors = ["QEMU", "VMware", "VirtualBox", "Hyper-V", "KVM"];
    let vendor_files = ["bios_vendor", "sys_vendor", "product_name"];

    for file in vendor_files {
        if let Ok(contents) = std::fs::read_to_string(format!("/sys/class/dmi/id/{file}")) {
            for vendor in vm_vendors {
                if contents.contains(vendor) {
                    return Ok(vendor);
                }
            }
        }
    }

    Ok("")
}

#[cfg(test)]
mod config {
    use super::*;
    use crate::setup_fuelup_home;
    use dirs::home_dir;
    use rusty_fork::rusty_fork_test;
    use std::env::var;
    use std::path::Path;

    rusty_fork_test! {
        #[test]
        fn all_unset() {
            let config = config().unwrap();

            assert_eq!(config.interval, 2592000);
            assert_eq!(config.metadata_timeout, 3);

            assert_eq!(
                config.lockfile,
                Path::new(
                    &home_dir()
                        .unwrap()
                        .join(format!(".fuelup/tmp/{}.lock", PROCESS_NAME))
                )
            );

            assert_eq!(
                config.logfile,
                Path::new(
                    &home_dir()
                        .unwrap()
                        .join(format!(".fuelup/log/{}.log", PROCESS_NAME))
                )
            );

            assert_eq!(
                config.touchfile,
                Path::new(
                    &home_dir()
                        .unwrap()
                        .join(format!(".fuelup/tmp/{}.touch", PROCESS_NAME))
                )
            );
        }

        #[test]
        fn systeminfo_watcher_interval_set() {
            set_var("SYSTEMINFO_WATCHER_INTERVAL", "2222");

            let config = config().unwrap();
            assert_eq!(config.interval, 2222);
        }

        #[test]
        fn systeminfo_watcher_interval_invalid() {
            set_var("SYSTEMINFO_WATCHER_INTERVAL", "invalid interval");

            assert_eq!(
                config().err(),
                Some(TelemetryError::InvalidConfig(
                    TelemetryError::Parse("invalid digit found in string".to_string()).into()
                ))
            );
        }

        #[test]
        fn metadata_timeout_set() {
            set_var("METADATA_TIMEOUT", "2222");

            let config = config().unwrap();
            assert_eq!(config.metadata_timeout, 2222);
        }

        #[test]
        fn metadata_timeout_invalid() {
            set_var("METADATA_TIMEOUT", "invalid");

            assert_eq!(
                config().err(),
                Some(TelemetryError::InvalidConfig(
                    TelemetryError::Parse("invalid digit found in string".to_string()).into()
                ))
            );
        }

        #[test]
        fn all_set() {
            setup_fuelup_home();
            let fuelup_home = var("FUELUP_HOME").unwrap();

            set_var("SYSTEMINFO_WATCHER_INTERVAL", "2222");
            set_var("METADATA_TIMEOUT", "3333");

            let config = config().unwrap();

            assert_eq!(config.interval, 2222);
            assert_eq!(config.metadata_timeout, 3333);

            assert_eq!(
                config.lockfile,
                Path::new(&format!(
                    "{}/tmp/{}.lock",
                    &fuelup_home, PROCESS_NAME
                ))
            );

            assert_eq!(
                config.logfile,
                Path::new(&format!(
                    "{}/log/{}.log",
                    &fuelup_home, PROCESS_NAME
                ))
            );

            assert_eq!(
                config.touchfile,
                Path::new(&format!(
                    "{}/tmp/{}.touch",
                    &fuelup_home, PROCESS_NAME
                ))
            );
        }
    }
}

#[cfg(test)]
mod start {
    use super::*;
    use crate::{into_recoverable, setup_fuelup_home, WatcherError};
    use nix::sys::signal::kill;
    use rusty_fork::rusty_fork_test;
    use std::{fs::File, thread::sleep, time::Duration};

    rusty_fork_test! {
        #[test]
        fn opted_out_is_true() {
            setup_fuelup_home();

            set_var("FUELUP_NO_TELEMETRY", "true");

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start();

            // Make sure it didn't continue and init values
            assert!(result.is_ok());
            assert!(!STARTED.load(Ordering::Relaxed));
            assert_eq!(PID.load(Ordering::Relaxed), 0);
        }

        #[test]
        fn opted_out_is_empty() {
            setup_fuelup_home();

            // Even though it's empty, we only care if it's set
            set_var("FUELUP_NO_TELEMETRY", "");

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start();

            // Make sure it didn't continue and init values
            assert!(result.is_ok());
            assert!(!STARTED.load(Ordering::Relaxed));
            assert_eq!(PID.load(Ordering::Relaxed), 0);
        }

        #[test]
        fn already_started() {
            setup_fuelup_home();

            STARTED.store(true, Ordering::Relaxed);
            PID.store(1, Ordering::Relaxed);

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start();

            // Make sure it didn't continue and init values
            assert!(result.is_ok());
            assert!(STARTED.load(Ordering::Relaxed));
            assert_eq!(PID.load(Ordering::Relaxed), 1);

            // Try to start it again to test re-entrance
            let result = systeminfo_watcher.start();

            assert!(result.is_ok());
            assert!(STARTED.load(Ordering::Relaxed));
            assert_eq!(PID.load(Ordering::Relaxed), 1);
        }

        #[test]
        fn daemonise_failed() {
            setup_fuelup_home();

            struct DaemoniseFailed;

            impl StartHelpers for DaemoniseFailed {
                fn daemonise(&self, _logfile: &PathBuf) -> WatcherResult<Option<Pid>> {
                    Err(into_recoverable(TelemetryError::Mock))
                }
            }

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start_with_helpers(&DaemoniseFailed);

            assert_eq!(result, Err(WatcherError::Recoverable(TelemetryError::Mock)));
            assert!(!STARTED.load(Ordering::Relaxed));
            assert_eq!(PID.load(Ordering::Relaxed), 0);
        }

        #[test]
        fn daemonise_is_parent() {
            setup_fuelup_home();

            struct DaemoniseIsParent;

            impl StartHelpers for DaemoniseIsParent {
                fn daemonise(&self, _logfile: &PathBuf) -> WatcherResult<Option<Pid>> {
                    Ok(Some(Pid::from_raw(2222)))
                }
            }

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start_with_helpers(&DaemoniseIsParent);

            assert_eq!(result, Ok(()));
            assert!(STARTED.load(Ordering::Relaxed));
            assert_eq!(PID.load(Ordering::Relaxed), 2222);
        }

        #[test]
        fn new_fuel_telemetry_failed() {
            setup_fuelup_home();

            struct NewFuelTelemetryFailed;

            impl StartHelpers for NewFuelTelemetryFailed {
                fn new_fuel_telemetry(
                    &self,
                ) -> Result<(
                    tracing_subscriber::filter::Filtered<
                        tracing_subscriber::fmt::Layer<
                            tracing_subscriber::Registry,
                            tracing_subscriber::fmt::format::DefaultFields,
                            telemetry_formatter::TelemetryFormatter,
                            tracing_appender::non_blocking::NonBlocking,
                        >,
                        tracing_subscriber::EnvFilter,
                        tracing_subscriber::Registry,
                    >,
                    tracing_appender::non_blocking::WorkerGuard,
                )> {
                    Err(TelemetryError::Mock)
                }

                fn daemonise(&self, _logfile: &PathBuf) -> WatcherResult<Option<Pid>> {
                    Ok(None)
                }
            }

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start_with_helpers(&NewFuelTelemetryFailed);

            assert_eq!(result, Err(WatcherError::Fatal(TelemetryError::Mock)));
        }

        #[test]
        fn enforce_singleton_failed() {
            setup_fuelup_home();

            struct EnforceSingletonFailed;

            impl StartHelpers for EnforceSingletonFailed {
                fn enforce_singleton(&self, _lockfile_path: &Path) -> Result<Flock<File>> {
                    Err(TelemetryError::Mock)
                }

                fn daemonise(&self, _logfile: &PathBuf) -> WatcherResult<Option<Pid>> {
                    Ok(None)
                }
            }

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start_with_helpers(&EnforceSingletonFailed);

            assert_eq!(result, Err(WatcherError::Fatal(TelemetryError::Mock)));
        }

        #[test]
        fn poll_systeminfo_failed() {
            setup_fuelup_home();

            struct PollSysteminfoFailed;

            impl StartHelpers for PollSysteminfoFailed {
                fn poll_systeminfo(&self, _systeminfo_watcher: &SystemInfoWatcher) -> Result<()> {
                    Err(TelemetryError::Mock)
                }

                fn daemonise(&self, _logfile: &PathBuf) -> WatcherResult<Option<Pid>> {
                    Ok(None)
                }
            }

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start_with_helpers(&PollSysteminfoFailed);

            assert_eq!(result, Err(WatcherError::Fatal(TelemetryError::Mock)));
        }

        #[test]
        fn ok() {
            setup_fuelup_home();

            let mut systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.start();
            let pid = PID.load(Ordering::Relaxed);

            assert_eq!(result, Ok(()));
            assert!(STARTED.load(Ordering::Relaxed));
            assert!(pid > 0);

            // SysInfoWatcher takes a few seconds to complete, so wait long
            // enough to be run within Github Actions
            for _ in 0..30 {
                if kill(Pid::from_raw(pid), None).is_ok() {
                    sleep(Duration::from_secs(1))
                } else {
                    break;
                }
            }
        }
    }
}

#[cfg(test)]
mod kill {
    use super::*;
    use nix::{
        sys::{
            signal::Signal,
            wait::{waitpid, WaitPidFlag, WaitStatus},
        },
        unistd::{fork, ForkResult},
    };
    use rusty_fork::rusty_fork_test;
    use std::thread::sleep;

    rusty_fork_test! {
        #[test]
        fn kill_nobody() {
            crate::systeminfo_watcher::PID.store(0, Ordering::Relaxed);
            assert!(!SystemInfoWatcher::kill().unwrap());
        }

        #[test]
        fn kill_systeminfo_watcher() {
            let mut kill_called = false;

            match unsafe { fork() }.unwrap() {
                ForkResult::Parent { child } => {
                    loop {
                        match waitpid(child, Some(WaitPidFlag::WNOHANG)) {
                            Ok(WaitStatus::StillAlive) => {
                                if !kill_called {
                                    // Since we're not daemonising, we have to set the PID manually
                                    crate::systeminfo_watcher::PID
                                        .store(child.as_raw(), Ordering::Relaxed);
                                    assert!(SystemInfoWatcher::kill().unwrap());

                                    kill_called = true;
                                    continue;
                                }
                            }
                            Ok(WaitStatus::Signaled(child_pid, signal, _)) => {
                                assert_eq!(child_pid, child);
                                assert_eq!(signal, Signal::SIGKILL);
                                break;
                            }
                            _ => panic!("Child process terminated unexpectedly"),
                        }
                    }
                }
                ForkResult::Child => loop {
                    sleep(Duration::from_secs(10));
                },
            }
        }
    }
}

#[cfg(test)]
mod poll_systeminfo {
    use super::*;
    use crate::setup_fuelup_home;
    use base64::{engine::general_purpose::STANDARD, Engine};
    use rusty_fork::rusty_fork_test;
    use std::{
        io::{BufRead, BufReader},
        time::SystemTime,
    };
    use sysinfo::System;

    rusty_fork_test! {
        #[test]
        fn create_locked_touchfile_failed() {
            setup_fuelup_home();

            struct CreateLockedTouchfileFailed;

            impl PollSysteminfoHelpers for CreateLockedTouchfileFailed {
                fn create_and_lock_touchfile(&self, _touchfile: &Path) -> Result<Flock<File>> {
                    Err(TelemetryError::Mock)
                }
            }

            let systeminfo_watcher = SystemInfoWatcher::new();
            let result =
                systeminfo_watcher.poll_systeminfo_with_helpers(&mut CreateLockedTouchfileFailed);

            assert_eq!(result, Err(TelemetryError::Mock));
        }

        #[test]
        fn open_and_lock_touchfile_failed() {
            setup_fuelup_home();

            OpenOptions::new()
                .create(true)
                .append(true)
                .open(Path::new(&config().unwrap().touchfile))
                .unwrap();

            struct OpenAndLockTouchfileFailed;

            impl PollSysteminfoHelpers for OpenAndLockTouchfileFailed {
                fn open_and_lock_touchfile(&self, _touchfile: &Path) -> Result<Flock<File>> {
                    Err(TelemetryError::Mock)
                }
            }

            let systeminfo_watcher = SystemInfoWatcher::new();
            let result =
                systeminfo_watcher.poll_systeminfo_with_helpers(&mut OpenAndLockTouchfileFailed);

            assert_eq!(result, Err(TelemetryError::Mock));
        }

        #[test]
        fn now_failed() {
            setup_fuelup_home();

            OpenOptions::new()
                .create(true)
                .append(true)
                .open(Path::new(&config().unwrap().touchfile))
                .unwrap();

            struct NowFailed;

            impl PollSysteminfoHelpers for NowFailed {
                fn now(&self) -> nix::Result<nix::sys::time::TimeSpec> {
                    Err(nix::errno::Errno::EOWNERDEAD)
                }
            }

            let systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.poll_systeminfo_with_helpers(&mut NowFailed);

            assert_eq!(
                result,
                Err(TelemetryError::Nix(
                    nix::errno::Errno::EOWNERDEAD.to_string()
                ))
            );
        }

        #[test]
        fn fstat_failed() {
            setup_fuelup_home();

            struct FstatFailed;

            OpenOptions::new()
                .create(true)
                .append(true)
                .open(Path::new(&config().unwrap().touchfile))
                .unwrap();

            impl PollSysteminfoHelpers for FstatFailed {
                fn fstat(&self, _fd: RawFd) -> nix::Result<libc::stat> {
                    Err(nix::errno::Errno::EOWNERDEAD)
                }
            }

            let systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.poll_systeminfo_with_helpers(&mut FstatFailed);

            assert_eq!(
                result,
                Err(TelemetryError::Nix(
                    nix::errno::Errno::EOWNERDEAD.to_string()
                ))
            );
        }

        #[test]
        fn recently_polled() {
            setup_fuelup_home();

            OpenOptions::new()
                .create(true)
                .append(true)
                .open(Path::new(&config().unwrap().touchfile))
                .unwrap();

            let systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.poll_systeminfo();

            assert_eq!(result, Ok(()));
        }

        #[test]
        fn detect_vm_failed() {
            setup_fuelup_home();

            let touchfile = OpenOptions::new()
                .create(true)
                .append(true)
                .open(Path::new(&config().unwrap().touchfile))
                .unwrap();

            touchfile
                .set_modified(
                    SystemTime::now() - Duration::from_secs(config().unwrap().interval as u64),
                )
                .unwrap();

            struct DetectVmFailed;

            impl PollSysteminfoHelpers for DetectVmFailed {
                fn detect_vm(&self) -> Result<&'static str> {
                    Err(TelemetryError::Mock)
                }
            }

            let systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.poll_systeminfo_with_helpers(&mut DetectVmFailed);

            assert_eq!(result, Err(TelemetryError::Mock));
        }

        #[test]
        fn set_len_failed() {
            setup_fuelup_home();

            let touchfile = OpenOptions::new()
                .create(true)
                .append(true)
                .open(Path::new(&config().unwrap().touchfile))
                .unwrap();

            touchfile
                .set_modified(
                    SystemTime::now() - Duration::from_secs(config().unwrap().interval as u64),
                )
                .unwrap();

            struct SetLenFailed;

            impl PollSysteminfoHelpers for SetLenFailed {
                fn set_len(&self, _flock: &Flock<File>, _len: u64) -> std::io::Result<()> {
                    Err(std::io::Error::new(std::io::ErrorKind::Other, "Mock error"))
                }
            }

            let systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.poll_systeminfo_with_helpers(&mut SetLenFailed);

            assert_eq!(result, Err(TelemetryError::IO("Mock error".to_string())));
        }

        #[test]
        fn sync_all_failed() {
            setup_fuelup_home();

            let touchfile = OpenOptions::new()
                .create(true)
                .append(true)
                .open(Path::new(&config().unwrap().touchfile))
                .unwrap();

            touchfile
                .set_modified(
                    SystemTime::now() - Duration::from_secs(config().unwrap().interval as u64),
                )
                .unwrap();

            struct SyncAllFailed;

            impl PollSysteminfoHelpers for SyncAllFailed {
                fn sync_all(&self, _flock: &Flock<File>) -> std::io::Result<()> {
                    Err(std::io::Error::new(std::io::ErrorKind::Other, "Mock error"))
                }
            }

            let systeminfo_watcher = SystemInfoWatcher::new();
            let result = systeminfo_watcher.poll_systeminfo_with_helpers(&mut SyncAllFailed);

            assert_eq!(result, Err(TelemetryError::IO("Mock error".to_string())));
        }

        #[test]
        fn ok() {
            setup_fuelup_home();

            let touchfile = OpenOptions::new()
                .create(true)
                .append(true)
                .open(Path::new(&config().unwrap().touchfile))
                .unwrap();

            let old_modified =
                SystemTime::now() - Duration::from_secs(config().unwrap().interval * 2);

            touchfile.set_modified(old_modified).unwrap();

            let systeminfo_watcher = SystemInfoWatcher::new();

            // Create a telemetry layer so we the telemetry file is written to
            set_var("TELEMETRY_PKG_NAME", "systeminfo_watcher");
            let (telemetry_layer, _guard) = crate::new!().unwrap();
            tracing_subscriber::registry().with(telemetry_layer).init();

            let result = systeminfo_watcher.poll_systeminfo();
            drop(_guard);

            assert_eq!(result, Ok(()));
            assert!(old_modified < touchfile.metadata().unwrap().modified().unwrap());

            // Test some fields were written to the telemetry file

            let telemetry_file =
                std::fs::read_dir(Path::new(&telemetry_config().unwrap().fuelup_tmp))
                    .unwrap()
                    .find(|file| {
                        file.as_ref()
                            .unwrap()
                            .path()
                            .to_str()
                            .unwrap()
                            .contains("systeminfo_watcher.telemetry.")
                    });

            assert!(telemetry_file.is_some());

            let body = {
                let mut body = Vec::new();

                let lines =
                    BufReader::new(File::open(telemetry_file.unwrap().unwrap().path()).unwrap())
                        .lines()
                        .collect::<std::result::Result<Vec<_>, _>>()
                        .unwrap();

                for base64_line in lines {
                    let decoded_line = STANDARD.decode(&base64_line).unwrap();
                    let line = String::from_utf8(decoded_line).unwrap();

                    body.push(line);
                }

                body.join("\n")
            };

            let mut sysinfo = System::new();

            sysinfo.refresh_cpu_usage();
            std::thread::sleep(sysinfo::MINIMUM_CPU_UPDATE_INTERVAL);
            sysinfo.refresh_cpu_usage();

            let cpus = sysinfo.cpus();
            let cpu_brand = cpus
                .first()
                .map_or_else(String::default, |cpu| cpu.brand().into());

            assert!(body.contains(&format!("cpu_arch=\"{}\"", System::cpu_arch())));
            assert!(body.contains(&format!("cpu_count={}", cpus.len())));
            assert!(body.contains(&format!("cpu_brand=\"{}\"", cpu_brand)));

            assert!(body.contains(&format!(
                "os_long_name=\"{}\"",
                System::long_os_version().unwrap_or_default()
            )));

            assert!(body.contains(&format!(
                "kernel_version=\"{}\"",
                System::kernel_version().unwrap_or_default()
            )));
        }
    }
}