Skip to content

Commit cc2f090

Browse files
committed
imp(pyspy): add BackendConfig support
1 parent 284dcbb commit cc2f090

File tree

2 files changed

+69
-10
lines changed

2 files changed

+69
-10
lines changed

pyroscope_backends/pyroscope_pyspy/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ pyroscope = { version = "0.5.2", path = "../../" }
1919
thiserror ="1.0"
2020
log = "0.4"
2121

22+
[patch.crates-io]
23+
read-process-memory = {git = "https://github.com/omarabid/read-process-memory.git", branch = "0.1.4-fix"}
24+
25+
2226
[profile.dev]
2327
opt-level=0
2428
debug = true

pyroscope_backends/pyroscope_pyspy/src/lib.rs

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use py_spy::{config::Config, sampler::Sampler};
22
use pyroscope::{
33
backend::{
4-
Backend, BackendImpl, BackendUninitialized, Report, Rule, Ruleset, StackBuffer, StackFrame,
5-
StackTrace,
4+
Backend, BackendConfig, BackendImpl, BackendUninitialized, Report, Rule, Ruleset,
5+
StackBuffer, StackFrame, StackTrace,
66
},
77
error::{PyroscopeError, Result},
88
};
@@ -19,7 +19,10 @@ const LOG_TAG: &str = "Pyroscope::Pyspy";
1919

2020
/// Short-hand function for creating a new Pyspy backend.
2121
pub fn pyspy_backend(config: PyspyConfig) -> BackendImpl<BackendUninitialized> {
22-
BackendImpl::new(Box::new(Pyspy::new(config)))
22+
// Clone BackendConfig to pass to the backend object.
23+
let backend_config = config.backend_config.clone();
24+
25+
BackendImpl::new(Box::new(Pyspy::new(config)), Some(backend_config))
2326
}
2427

2528
/// Pyspy Configuration
@@ -29,6 +32,8 @@ pub struct PyspyConfig {
2932
pid: Option<i32>,
3033
/// Sampling rate
3134
sample_rate: u32,
35+
/// Backend Config
36+
backend_config: BackendConfig,
3237
/// Lock Process while sampling
3338
lock_process: py_spy::config::LockingStrategy,
3439
/// Profiling duration (None for infinite)
@@ -48,6 +53,7 @@ impl Default for PyspyConfig {
4853
PyspyConfig {
4954
pid: Some(0),
5055
sample_rate: 100,
56+
backend_config: BackendConfig::default(),
5157
lock_process: py_spy::config::LockingStrategy::NonBlocking,
5258
time_limit: None,
5359
with_subprocesses: false,
@@ -75,6 +81,45 @@ impl PyspyConfig {
7581
}
7682
}
7783

84+
/// Tag thread id in report
85+
pub fn report_pid(self) -> Self {
86+
let backend_config = BackendConfig {
87+
report_pid: true,
88+
..self.backend_config
89+
};
90+
91+
PyspyConfig {
92+
backend_config,
93+
..self
94+
}
95+
}
96+
97+
/// Tag thread id in report
98+
pub fn report_thread_id(self) -> Self {
99+
let backend_config = BackendConfig {
100+
report_thread_id: true,
101+
..self.backend_config
102+
};
103+
104+
PyspyConfig {
105+
backend_config,
106+
..self
107+
}
108+
}
109+
110+
/// Tag thread name in report
111+
pub fn report_thread_name(self) -> Self {
112+
let backend_config = BackendConfig {
113+
report_thread_name: true,
114+
..self.backend_config
115+
};
116+
117+
PyspyConfig {
118+
backend_config,
119+
..self
120+
}
121+
}
122+
78123
/// Set the lock process flag
79124
pub fn lock_process(self, lock_process: bool) -> Self {
80125
PyspyConfig {
@@ -172,6 +217,12 @@ impl Backend for Pyspy {
172217
Ok(self.config.sample_rate)
173218
}
174219

220+
fn set_config(&self, config: BackendConfig) {}
221+
222+
fn get_config(&self) -> Result<BackendConfig> {
223+
Ok(self.config.backend_config)
224+
}
225+
175226
/// Add a rule to the ruleset.
176227
fn add_rule(&self, rule: Rule) -> Result<()> {
177228
self.ruleset.lock()?.add_rule(rule)?;
@@ -229,6 +280,8 @@ impl Backend for Pyspy {
229280
// create a new ruleset reference
230281
let ruleset = self.ruleset.clone();
231282

283+
let backend_config = self.config.backend_config.clone();
284+
232285
self.sampler_thread = Some(std::thread::spawn(move || {
233286
// Get PID
234287
let pid = config
@@ -257,7 +310,7 @@ impl Backend for Pyspy {
257310

258311
// Convert py-spy trace to a Pyroscope trace
259312
let own_trace: StackTrace =
260-
Into::<StackTraceWrapper>::into(trace.clone()).into();
313+
Into::<StackTraceWrapper>::into((trace.clone(), &backend_config)).into();
261314

262315
// apply ruleset
263316
let stacktrace = own_trace + &ruleset.lock()?.clone();
@@ -335,13 +388,15 @@ impl From<StackTraceWrapper> for StackTrace {
335388
}
336389
}
337390

338-
impl From<py_spy::StackTrace> for StackTraceWrapper {
339-
fn from(trace: py_spy::StackTrace) -> Self {
391+
impl From<(py_spy::StackTrace, &BackendConfig)> for StackTraceWrapper {
392+
fn from(arg: (py_spy::StackTrace, &BackendConfig)) -> Self {
393+
let (stack_trace, config) = arg;
340394
let stacktrace = StackTrace::new(
341-
Some(trace.pid as u32),
342-
Some(trace.thread_id as u64),
343-
trace.thread_name.clone(),
344-
trace
395+
config,
396+
Some(stack_trace.pid as u32),
397+
Some(stack_trace.thread_id as u64),
398+
stack_trace.thread_name.clone(),
399+
stack_trace
345400
.frames
346401
.iter()
347402
.map(|frame| Into::<StackFrameWrapper>::into(frame.clone()).into())

0 commit comments

Comments
 (0)