Skip to content

Commit 557c0fd

Browse files
committed
imp(ruby): add report transformation function
1 parent feaffcd commit 557c0fd

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

pyroscope_ffi/ruby/ext/rbspy/src/lib.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,54 @@
11
use ffikit::Signal;
2-
use pyroscope::backend::Tag;
2+
use pyroscope::backend::{Report, StackFrame, Tag};
33
use pyroscope::PyroscopeAgent;
44
use pyroscope_rbspy::{rbspy_backend, RbspyConfig};
55
use std::collections::hash_map::DefaultHasher;
66
use std::ffi::CStr;
77
use std::hash::Hasher;
88
use std::os::raw::c_char;
99

10+
pub fn transform_report(report: Report) -> Report {
11+
let data = report
12+
.data
13+
.iter()
14+
.map(|(stacktrace, count)| {
15+
let new_frames = stacktrace
16+
.frames
17+
.iter()
18+
.map(|frame| {
19+
let frame = frame.to_owned();
20+
let regex = regex::Regex::new(r"(.+?/gems/|.+?/ruby/)").unwrap();
21+
let new_filename = Some(
22+
regex
23+
.replace_all(frame.filename.unwrap().as_str(), "")
24+
.to_string(),
25+
);
26+
27+
// something
28+
StackFrame::new(
29+
frame.module,
30+
frame.name,
31+
new_filename,
32+
frame.relative_path,
33+
frame.absolute_path,
34+
frame.line,
35+
)
36+
})
37+
.collect();
38+
39+
let mut mystack = stacktrace.to_owned();
40+
41+
mystack.frames = new_frames;
42+
43+
(mystack, count.to_owned())
44+
})
45+
.collect();
46+
47+
let new_report = Report::new(data).metadata(report.metadata.clone());
48+
49+
new_report
50+
}
51+
1052
#[no_mangle]
1153
pub extern "C" fn initialize_agent(
1254
application_name: *const c_char, server_address: *const c_char, auth_token: *const c_char,
@@ -55,9 +97,17 @@ pub extern "C" fn initialize_agent(
5597
let tags = string_to_tags(tags_ref);
5698
let rbspy = rbspy_backend(rbspy_config);
5799

100+
let mut regex_pattern = String::from(r"");
101+
102+
if directory_name != String::from(".") {
103+
regex_pattern.push_str(directory_name.as_str());
104+
regex_pattern.push_str(r"/");
105+
}
106+
58107
let mut agent_builder = PyroscopeAgent::builder(server_address, application_name)
59108
.backend(rbspy)
60-
.regex(regex::Regex::new(&directory_name).unwrap())
109+
.func(transform_report)
110+
.regex(regex::Regex::new(regex_pattern.as_str()).unwrap())
61111
.tags(tags);
62112

63113
if auth_token != "" {

pyroscope_ffi/ruby/lib/pyroscope.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def initialize(*)
2323
self.application_name = ''
2424
self.server_address = 'http://localhost:4040'
2525
self.auth_token = ''
26-
self.directory_name = __dir__
26+
self.directory_name = File.dirname($0)
2727
self.sample_rate = 100
2828
self.detect_subprocesses = false
2929
self.on_cpu = true
@@ -46,7 +46,7 @@ def configure
4646
@config.app_name || @config.application_name || "",
4747
@config.server_address || "",
4848
@config.auth_token || "",
49-
@config.directory_name || __dir__,
49+
@config.directory_name || File.dirname($0),
5050
@config.sample_rate || 100,
5151
@config.detect_subprocesses || false,
5252
@config.on_cpu || false,

0 commit comments

Comments
 (0)