diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index 764a6ce7..15898f53 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -128,9 +128,9 @@ jobs: fail-fast: false matrix: include: - - macos-version: "10.15" + - macos-version: "11.0" target: x86_64-apple-darwin - py-platform: macosx-10_15_x86_64 + py-platform: macosx-11_0_x86_64 - macos-version: "11.0" target: aarch64-apple-darwin py-platform: macosx-11_0_arm64 diff --git a/Cargo.toml b/Cargo.toml index fb57d349..780874f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,9 @@ documentation = "https://docs.rs/pyroscope" repository = "https://github.com/pyroscope-io/pyroscope-rs" readme = "README.md" autobins = false -autoexamples = true -autotests = true -autobenches = true +autoexamples = true +autotests = true +autobenches = true [workspace] members = [ @@ -57,7 +57,6 @@ names = "0.13.0" reqwest = { version = "0.11", features = ["blocking", "rustls-tls-native-roots"]} url = "2.2.2" libc = "^0.2.124" -regex = "1" [dev-dependencies] tokio = { version = "1.18", features = ["full"] } diff --git a/pyroscope_ffi/ruby/ext/rbspy/Cargo.toml b/pyroscope_ffi/ruby/ext/rbspy/Cargo.toml index 6207cb65..2f321dd3 100644 --- a/pyroscope_ffi/ruby/ext/rbspy/Cargo.toml +++ b/pyroscope_ffi/ruby/ext/rbspy/Cargo.toml @@ -14,7 +14,6 @@ pyroscope = { path = "../../../../" } pyroscope_rbspy = { path = "../../../../pyroscope_backends/pyroscope_rbspy" } ffikit = { path = "../../../ffikit" } pretty_env_logger = "0.4.0" -regex = "1" [patch.crates-io] read-process-memory = {git = "https://github.com/omarabid/read-process-memory.git", branch = "0.1.4-fix"} diff --git a/pyroscope_ffi/ruby/ext/rbspy/src/lib.rs b/pyroscope_ffi/ruby/ext/rbspy/src/lib.rs index 9fb6817b..6f045a66 100644 --- a/pyroscope_ffi/ruby/ext/rbspy/src/lib.rs +++ b/pyroscope_ffi/ruby/ext/rbspy/src/lib.rs @@ -6,8 +6,12 @@ use std::collections::hash_map::DefaultHasher; use std::ffi::CStr; use std::hash::Hasher; use std::os::raw::c_char; +use std::env; pub fn transform_report(report: Report) -> Report { + let cwd = env::current_dir().unwrap(); + let cwd = cwd.to_str().unwrap_or(""); + let data = report .data .iter() @@ -17,18 +21,41 @@ pub fn transform_report(report: Report) -> Report { .iter() .map(|frame| { let frame = frame.to_owned(); - let regex = regex::Regex::new(r"(.+?/gems/|.+?/ruby/)").unwrap(); - let new_filename = Some( - regex - .replace_all(frame.filename.unwrap().as_str(), "") - .to_string(), - ); + let mut s = frame.filename.unwrap(); + match s.find(cwd) { + Some(i) => { + s = s[(i+cwd.len()+1)..].to_string(); + } + None => { + match s.find("/gems/") { + Some(i) => { + s = s[(i+1)..].to_string(); + } + None => { + match s.find("/ruby/") { + Some(i) => { + s = s[(i+6)..].to_string(); + match s.find("/") { + Some(i) => { + s = s[(i+1)..].to_string(); + } + None => { + } + } + } + None => { + } + } + } + } + } + } // something StackFrame::new( frame.module, frame.name, - new_filename, + Some(s.to_string()), frame.relative_path, frame.absolute_path, frame.line, @@ -52,8 +79,8 @@ pub fn transform_report(report: Report) -> Report { #[no_mangle] pub extern "C" fn initialize_agent( application_name: *const c_char, server_address: *const c_char, auth_token: *const c_char, - directory_name: *const c_char, sample_rate: u32, detect_subprocesses: bool, on_cpu: bool, - report_pid: bool, report_thread_id: bool, tags: *const c_char, + sample_rate: u32, detect_subprocesses: bool, on_cpu: bool, report_pid: bool, + report_thread_id: bool, tags: *const c_char, ) -> bool { // Initialize FFIKit let recv = ffikit::initialize_ffi().unwrap(); @@ -73,11 +100,6 @@ pub extern "C" fn initialize_agent( .unwrap() .to_string(); - let directory_name = unsafe { CStr::from_ptr(directory_name) } - .to_str() - .unwrap() - .to_string(); - let tags_string = unsafe { CStr::from_ptr(tags) } .to_str() .unwrap() @@ -97,17 +119,9 @@ pub extern "C" fn initialize_agent( let tags = string_to_tags(tags_ref); let rbspy = rbspy_backend(rbspy_config); - let mut regex_pattern = String::from(r""); - - if directory_name != String::from(".") { - regex_pattern.push_str(directory_name.as_str()); - regex_pattern.push_str(r"/"); - } - let mut agent_builder = PyroscopeAgent::builder(server_address, application_name) .backend(rbspy) .func(transform_report) - .regex(regex::Regex::new(regex_pattern.as_str()).unwrap()) .tags(tags); if auth_token != "" { diff --git a/pyroscope_ffi/ruby/lib/pyroscope.rb b/pyroscope_ffi/ruby/lib/pyroscope.rb index 8f8b0327..11929703 100644 --- a/pyroscope_ffi/ruby/lib/pyroscope.rb +++ b/pyroscope_ffi/ruby/lib/pyroscope.rb @@ -4,7 +4,7 @@ module Pyroscope module Rust extend FFI::Library ffi_lib File.expand_path(File.dirname(__FILE__)) + "/rbspy/rbspy.#{RbConfig::CONFIG["DLEXT"]}" - attach_function :initialize_agent, [:string, :string, :string, :string, :int, :bool, :bool, :bool, :bool, :string], :bool + attach_function :initialize_agent, [:string, :string, :string, :int, :bool, :bool, :bool, :bool, :string], :bool attach_function :add_thread_tag, [:uint64, :string, :string], :bool attach_function :remove_thread_tag, [:uint64, :string, :string], :bool attach_function :add_global_tag, [:string, :string], :bool @@ -18,12 +18,11 @@ module Utils attach_function :thread_id, [], :uint64 end - 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 + 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 def initialize(*) self.application_name = '' self.server_address = 'http://localhost:4040' self.auth_token = '' - self.directory_name = File.dirname($0) self.sample_rate = 100 self.detect_subprocesses = false self.on_cpu = true @@ -46,7 +45,6 @@ def configure @config.app_name || @config.application_name || "", @config.server_address || "", @config.auth_token || "", - @config.directory_name || File.dirname($0), @config.sample_rate || 100, @config.detect_subprocesses || false, @config.on_cpu || false, diff --git a/src/pyroscope.rs b/src/pyroscope.rs index dc083070..08127a42 100644 --- a/src/pyroscope.rs +++ b/src/pyroscope.rs @@ -1,4 +1,3 @@ -use regex::Regex; use std::{ collections::HashMap, marker::PhantomData, @@ -43,8 +42,6 @@ pub struct PyroscopeConfig { pub spy_name: String, /// Authentication Token pub auth_token: Option, - /// Regex to apply - pub regex: Option, /// Function to apply pub func: Option Report>, } @@ -61,7 +58,6 @@ impl Default for PyroscopeConfig { sample_rate: 100u32, spy_name: "undefined".to_string(), auth_token: None, - regex: None, func: None, } } @@ -83,7 +79,6 @@ impl PyroscopeConfig { sample_rate: 100u32, // Default sample rate spy_name: String::from("undefined"), // Spy Name should be set by the backend auth_token: None, // No authentication token - regex: None, // No regex func: None, // No function } } @@ -125,14 +120,6 @@ impl PyroscopeConfig { } } - /// Set the Regex. - pub fn regex(self, regex: Regex) -> Self { - Self { - regex: Some(regex), - ..self - } - } - /// Set the Function. pub fn func(self, func: fn(Report) -> Report) -> Self { Self { @@ -268,22 +255,6 @@ impl PyroscopeAgentBuilder { } } - /// Set Regex. - /// This is optional. If not set, the agent will not apply any regex. - /// #Example - /// ```ignore - /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app") - /// .regex(Regex::new("^my-app.*").unwrap()) - /// .build() - /// ?; - /// ``` - pub fn regex(self, regex: Regex) -> Self { - Self { - config: self.config.regex(regex), - ..self - } - } - /// Set the Function. /// This is optional. If not set, the agent will not apply any function. /// #Example diff --git a/src/session.rs b/src/session.rs index c9022ba6..cea971a2 100644 --- a/src/session.rs +++ b/src/session.rs @@ -159,13 +159,7 @@ impl Session { } // Convert a report to a byte array - let mut report_string = report_owned.to_string(); - - // Apply Regex to the report - if let Some(regex) = self.config.regex.clone() { - report_string = regex.replace_all(&report_string, "").to_string(); - } - + let report_string = report_owned.to_string(); let report_u8 = report_string.into_bytes(); // Check if the report is empty