File size: 2,723 Bytes
d8435ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::sync::Arc;

use parking_lot::Mutex;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::common::pyroscope_state::pyro::PyroscopeState;
use crate::settings::Settings;

#[derive(Serialize, JsonSchema, Debug, Deserialize, Clone)]
pub struct PyroscopeConfig {
    pub url: String,
    pub identifier: String,
    pub user: Option<String>,
    pub password: Option<String>,
    pub sampling_rate: Option<u32>,
}

#[derive(Default, Debug, Serialize, JsonSchema, Deserialize, Clone)]
pub struct DebuggerConfig {
    pub pyroscope: Option<PyroscopeConfig>,
}

#[derive(Debug, Serialize, JsonSchema, Deserialize, Clone)]
#[serde(rename_all = "snake_case")]
pub enum DebugConfigPatch {
    Pyroscope(Option<PyroscopeConfig>),
}

pub struct DebuggerState {
    #[cfg_attr(not(target_os = "linux"), allow(dead_code))]
    pub pyroscope: Arc<Mutex<Option<PyroscopeState>>>,
}

impl DebuggerState {
    pub fn from_settings(settings: &Settings) -> Self {
        let pyroscope_config = settings.debugger.pyroscope.clone();
        Self {
            pyroscope: Arc::new(Mutex::new(PyroscopeState::from_config(pyroscope_config))),
        }
    }

    #[cfg_attr(not(target_os = "linux"), allow(clippy::unused_self))]
    pub fn get_config(&self) -> DebuggerConfig {
        let pyroscope_config = {
            #[cfg(target_os = "linux")]
            {
                let pyroscope_state_guard = self.pyroscope.lock();
                pyroscope_state_guard.as_ref().map(|s| s.config.clone())
            }
            #[cfg(not(target_os = "linux"))]
            {
                None
            }
        };

        DebuggerConfig {
            pyroscope: pyroscope_config,
        }
    }

    #[cfg_attr(not(target_os = "linux"), allow(clippy::unused_self))]
    pub fn apply_config_patch(&self, patch: DebugConfigPatch) -> bool {
        #[cfg(target_os = "linux")]
        {
            match patch {
                DebugConfigPatch::Pyroscope(new_config) => {
                    let mut pyroscope_guard = self.pyroscope.lock();
                    if let Some(pyroscope_state) = pyroscope_guard.as_mut() {
                        let stopped = pyroscope_state.stop_agent();
                        if !stopped {
                            return false;
                        }
                    }

                    if let Some(new_config) = new_config {
                        *pyroscope_guard = PyroscopeState::from_config(Some(new_config));
                    }
                    true
                }
            }
        }

        #[cfg(not(target_os = "linux"))]
        {
            let _ = patch; // Ignore new_config on non-linux OS
            false
        }
    }
}