Skip to content

Commit ca93e1e

Browse files
committed
ruby substitute function changes
1 parent 1f921cb commit ca93e1e

File tree

7 files changed

+44
-69
lines changed

7 files changed

+44
-69
lines changed

.github/workflows/ruby.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,9 @@ jobs:
128128
fail-fast: false
129129
matrix:
130130
include:
131-
- macos-version: "10.15"
131+
- macos-version: "11.0"
132132
target: x86_64-apple-darwin
133-
py-platform: macosx-10_15_x86_64
133+
py-platform: macosx-11_0_x86_64
134134
- macos-version: "11.0"
135135
target: aarch64-apple-darwin
136136
py-platform: macosx-11_0_arm64

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ documentation = "https://docs.rs/pyroscope"
1313
repository = "https://github.com/pyroscope-io/pyroscope-rs"
1414
readme = "README.md"
1515
autobins = false
16-
autoexamples = true
17-
autotests = true
18-
autobenches = true
16+
autoexamples = true
17+
autotests = true
18+
autobenches = true
1919

2020
[workspace]
2121
members = [
@@ -57,7 +57,6 @@ names = "0.13.0"
5757
reqwest = { version = "0.11", features = ["blocking", "rustls-tls-native-roots"]}
5858
url = "2.2.2"
5959
libc = "^0.2.124"
60-
regex = "1"
6160

6261
[dev-dependencies]
6362
tokio = { version = "1.18", features = ["full"] }

pyroscope_ffi/ruby/ext/rbspy/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ pyroscope = { path = "../../../../" }
1414
pyroscope_rbspy = { path = "../../../../pyroscope_backends/pyroscope_rbspy" }
1515
ffikit = { path = "../../../ffikit" }
1616
pretty_env_logger = "0.4.0"
17-
regex = "1"
1817

1918
[patch.crates-io]
2019
read-process-memory = {git = "https://github.com/omarabid/read-process-memory.git", branch = "0.1.4-fix"}

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

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@ use std::collections::hash_map::DefaultHasher;
66
use std::ffi::CStr;
77
use std::hash::Hasher;
88
use std::os::raw::c_char;
9+
use std::env;
910

1011
pub fn transform_report(report: Report) -> Report {
12+
let cwd = env::current_dir().unwrap();
13+
let cwd = cwd.to_str().unwrap_or("");
14+
1115
let data = report
1216
.data
1317
.iter()
@@ -17,18 +21,41 @@ pub fn transform_report(report: Report) -> Report {
1721
.iter()
1822
.map(|frame| {
1923
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-
);
24+
let mut s = frame.filename.unwrap();
25+
match s.find(cwd) {
26+
Some(i) => {
27+
s = s[(i+cwd.len()+1)..].to_string();
28+
}
29+
None => {
30+
match s.find("/gems/") {
31+
Some(i) => {
32+
s = s[(i+1)..].to_string();
33+
}
34+
None => {
35+
match s.find("/ruby/") {
36+
Some(i) => {
37+
s = s[(i+6)..].to_string();
38+
match s.find("/") {
39+
Some(i) => {
40+
s = s[(i+1)..].to_string();
41+
}
42+
None => {
43+
}
44+
}
45+
}
46+
None => {
47+
}
48+
}
49+
}
50+
}
51+
}
52+
}
2653

2754
// something
2855
StackFrame::new(
2956
frame.module,
3057
frame.name,
31-
new_filename,
58+
Some(s.to_string()),
3259
frame.relative_path,
3360
frame.absolute_path,
3461
frame.line,
@@ -52,8 +79,8 @@ pub fn transform_report(report: Report) -> Report {
5279
#[no_mangle]
5380
pub extern "C" fn initialize_agent(
5481
application_name: *const c_char, server_address: *const c_char, auth_token: *const c_char,
55-
directory_name: *const c_char, sample_rate: u32, detect_subprocesses: bool, on_cpu: bool,
56-
report_pid: bool, report_thread_id: bool, tags: *const c_char,
82+
sample_rate: u32, detect_subprocesses: bool, on_cpu: bool, report_pid: bool,
83+
report_thread_id: bool, tags: *const c_char,
5784
) -> bool {
5885
// Initialize FFIKit
5986
let recv = ffikit::initialize_ffi().unwrap();
@@ -73,11 +100,6 @@ pub extern "C" fn initialize_agent(
73100
.unwrap()
74101
.to_string();
75102

76-
let directory_name = unsafe { CStr::from_ptr(directory_name) }
77-
.to_str()
78-
.unwrap()
79-
.to_string();
80-
81103
let tags_string = unsafe { CStr::from_ptr(tags) }
82104
.to_str()
83105
.unwrap()
@@ -97,17 +119,9 @@ pub extern "C" fn initialize_agent(
97119
let tags = string_to_tags(tags_ref);
98120
let rbspy = rbspy_backend(rbspy_config);
99121

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-
107122
let mut agent_builder = PyroscopeAgent::builder(server_address, application_name)
108123
.backend(rbspy)
109124
.func(transform_report)
110-
.regex(regex::Regex::new(regex_pattern.as_str()).unwrap())
111125
.tags(tags);
112126

113127
if auth_token != "" {

pyroscope_ffi/ruby/lib/pyroscope.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module Pyroscope
44
module Rust
55
extend FFI::Library
66
ffi_lib File.expand_path(File.dirname(__FILE__)) + "/rbspy/rbspy.#{RbConfig::CONFIG["DLEXT"]}"
7-
attach_function :initialize_agent, [:string, :string, :string, :string, :int, :bool, :bool, :bool, :bool, :string], :bool
7+
attach_function :initialize_agent, [:string, :string, :string, :int, :bool, :bool, :bool, :bool, :string], :bool
88
attach_function :add_thread_tag, [:uint64, :string, :string], :bool
99
attach_function :remove_thread_tag, [:uint64, :string, :string], :bool
1010
attach_function :add_global_tag, [:string, :string], :bool
@@ -18,12 +18,11 @@ module Utils
1818
attach_function :thread_id, [], :uint64
1919
end
2020

21-
Config = Struct.new(:application_name, :app_name, :server_address, :auth_token, :directory_name, :sample_rate, :detect_subprocesses, :on_cpu, :report_pid, :report_thread_id, :log_level, :tags) do
21+
Config = Struct.new(:application_name, :app_name, :server_address, :auth_token, :sample_rate, :detect_subprocesses, :on_cpu, :report_pid, :report_thread_id, :log_level, :tags) do
2222
def initialize(*)
2323
self.application_name = ''
2424
self.server_address = 'http://localhost:4040'
2525
self.auth_token = ''
26-
self.directory_name = File.dirname($0)
2726
self.sample_rate = 100
2827
self.detect_subprocesses = false
2928
self.on_cpu = true
@@ -46,7 +45,6 @@ def configure
4645
@config.app_name || @config.application_name || "",
4746
@config.server_address || "",
4847
@config.auth_token || "",
49-
@config.directory_name || File.dirname($0),
5048
@config.sample_rate || 100,
5149
@config.detect_subprocesses || false,
5250
@config.on_cpu || false,

src/pyroscope.rs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use regex::Regex;
21
use std::{
32
collections::HashMap,
43
marker::PhantomData,
@@ -43,8 +42,6 @@ pub struct PyroscopeConfig {
4342
pub spy_name: String,
4443
/// Authentication Token
4544
pub auth_token: Option<String>,
46-
/// Regex to apply
47-
pub regex: Option<Regex>,
4845
/// Function to apply
4946
pub func: Option<fn(Report) -> Report>,
5047
}
@@ -61,7 +58,6 @@ impl Default for PyroscopeConfig {
6158
sample_rate: 100u32,
6259
spy_name: "undefined".to_string(),
6360
auth_token: None,
64-
regex: None,
6561
func: None,
6662
}
6763
}
@@ -83,7 +79,6 @@ impl PyroscopeConfig {
8379
sample_rate: 100u32, // Default sample rate
8480
spy_name: String::from("undefined"), // Spy Name should be set by the backend
8581
auth_token: None, // No authentication token
86-
regex: None, // No regex
8782
func: None, // No function
8883
}
8984
}
@@ -125,14 +120,6 @@ impl PyroscopeConfig {
125120
}
126121
}
127122

128-
/// Set the Regex.
129-
pub fn regex(self, regex: Regex) -> Self {
130-
Self {
131-
regex: Some(regex),
132-
..self
133-
}
134-
}
135-
136123
/// Set the Function.
137124
pub fn func(self, func: fn(Report) -> Report) -> Self {
138125
Self {
@@ -268,22 +255,6 @@ impl PyroscopeAgentBuilder {
268255
}
269256
}
270257

271-
/// Set Regex.
272-
/// This is optional. If not set, the agent will not apply any regex.
273-
/// #Example
274-
/// ```ignore
275-
/// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
276-
/// .regex(Regex::new("^my-app.*").unwrap())
277-
/// .build()
278-
/// ?;
279-
/// ```
280-
pub fn regex(self, regex: Regex) -> Self {
281-
Self {
282-
config: self.config.regex(regex),
283-
..self
284-
}
285-
}
286-
287258
/// Set the Function.
288259
/// This is optional. If not set, the agent will not apply any function.
289260
/// #Example

src/session.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,13 +159,7 @@ impl Session {
159159
}
160160

161161
// Convert a report to a byte array
162-
let mut report_string = report_owned.to_string();
163-
164-
// Apply Regex to the report
165-
if let Some(regex) = self.config.regex.clone() {
166-
report_string = regex.replace_all(&report_string, "").to_string();
167-
}
168-
162+
let report_string = report_owned.to_string();
169163
let report_u8 = report_string.into_bytes();
170164

171165
// Check if the report is empty

0 commit comments

Comments
 (0)