Skip to content

Commit

Permalink
ruby substitute function changes
Browse files Browse the repository at this point in the history
  • Loading branch information
petethepig committed Jul 22, 2022
1 parent 1f921cb commit ca93e1e
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 69 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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"] }
Expand Down
1 change: 0 additions & 1 deletion pyroscope_ffi/ruby/ext/rbspy/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
Expand Down
58 changes: 36 additions & 22 deletions pyroscope_ffi/ruby/ext/rbspy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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,
Expand All @@ -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();
Expand All @@ -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()
Expand All @@ -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 != "" {
Expand Down
6 changes: 2 additions & 4 deletions pyroscope_ffi/ruby/lib/pyroscope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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,
Expand Down
29 changes: 0 additions & 29 deletions src/pyroscope.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use regex::Regex;
use std::{
collections::HashMap,
marker::PhantomData,
Expand Down Expand Up @@ -43,8 +42,6 @@ pub struct PyroscopeConfig {
pub spy_name: String,
/// Authentication Token
pub auth_token: Option<String>,
/// Regex to apply
pub regex: Option<Regex>,
/// Function to apply
pub func: Option<fn(Report) -> Report>,
}
Expand All @@ -61,7 +58,6 @@ impl Default for PyroscopeConfig {
sample_rate: 100u32,
spy_name: "undefined".to_string(),
auth_token: None,
regex: None,
func: None,
}
}
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
8 changes: 1 addition & 7 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ca93e1e

Please sign in to comment.