Skip to content

Commit af5e4d1

Browse files
committed
Merge remote-tracking branch 'origin/0.5.4' into 0.5.4
2 parents 160fa82 + cdf23fd commit af5e4d1

File tree

6 files changed

+96
-13
lines changed

6 files changed

+96
-13
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ log = "0.4"
5656
names = "0.13.0"
5757
reqwest = { version = "0.11", features = ["blocking", "rustls-tls-native-roots"]}
5858
url = "2.2.2"
59+
libflate = "1.2.0"
5960
libc = "^0.2.124"
6061

6162
[dev-dependencies]

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
use ffikit::Signal;
2-
use pyroscope::backend::{Report, StackFrame, Tag};
3-
use pyroscope::PyroscopeAgent;
4-
use pyroscope_rbspy::{rbspy_backend, RbspyConfig};
51
use std::collections::hash_map::DefaultHasher;
62
use std::env;
73
use std::ffi::CStr;
84
use std::hash::Hasher;
95
use std::os::raw::c_char;
6+
use std::str::FromStr;
7+
8+
use ffikit::Signal;
9+
use pyroscope_rbspy::{rbspy_backend, RbspyConfig};
10+
11+
use pyroscope::{pyroscope::Compression, PyroscopeAgent};
12+
use pyroscope::backend::{Report, StackFrame, Tag};
1013

1114
pub fn transform_report(report: Report) -> Report {
1215
let cwd = env::current_dir().unwrap();
@@ -104,7 +107,7 @@ pub extern "C" fn initialize_logging(logging_level: u32) -> bool {
104107
pub extern "C" fn initialize_agent(
105108
application_name: *const c_char, server_address: *const c_char, auth_token: *const c_char,
106109
sample_rate: u32, detect_subprocesses: bool, oncpu: bool, report_pid: bool,
107-
report_thread_id: bool, tags: *const c_char,
110+
report_thread_id: bool, tags: *const c_char, compression: *const c_char
108111
) -> bool {
109112
// Initialize FFIKit
110113
let recv = ffikit::initialize_ffi().unwrap();
@@ -129,6 +132,13 @@ pub extern "C" fn initialize_agent(
129132
.unwrap()
130133
.to_string();
131134

135+
let compression_string = unsafe { CStr::from_ptr(compression) }
136+
.to_str()
137+
.unwrap()
138+
.to_string();
139+
140+
let compression = Compression::from_str(&compression_string);
141+
132142
let pid = std::process::id();
133143

134144
let rbspy_config = RbspyConfig::new(pid.try_into().unwrap())
@@ -152,6 +162,10 @@ pub extern "C" fn initialize_agent(
152162
agent_builder = agent_builder.auth_token(auth_token);
153163
}
154164

165+
if let Ok(compression) = compression {
166+
agent_builder = agent_builder.compression(compression);
167+
}
168+
155169
let agent = agent_builder.build().unwrap();
156170

157171
let agent_running = agent.start().unwrap();

pyroscope_ffi/ruby/lib/pyroscope.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module Rust
88
extend FFI::Library
99
ffi_lib File.expand_path(File.dirname(__FILE__)) + "/rbspy/rbspy.#{RbConfig::CONFIG["DLEXT"]}"
1010
attach_function :initialize_logging, [:int], :bool
11-
attach_function :initialize_agent, [:string, :string, :string, :int, :bool, :bool, :bool, :bool, :string], :bool
11+
attach_function :initialize_agent, [:string, :string, :string, :int, :bool, :bool, :bool, :bool, :string, :string], :bool
1212
attach_function :add_thread_tag, [:uint64, :string, :string], :bool
1313
attach_function :remove_thread_tag, [:uint64, :string, :string], :bool
1414
attach_function :add_global_tag, [:string, :string], :bool
@@ -22,8 +22,10 @@ module Utils
2222
attach_function :thread_id, [], :uint64
2323
end
2424

25-
Config = Struct.new(:application_name, :app_name, :server_address, :auth_token, :log_level, :sample_rate, :detect_subprocesses, :oncpu, :report_pid, :report_thread_id, :tags) do
25+
Config = Struct.new(:application_name, :app_name, :server_address, :auth_token, :log_level, :sample_rate, :detect_subprocesses, :oncpu, :report_pid, :report_thread_id, :tags, :compression) do
2626
def initialize(*)
27+
super
28+
# defaults:
2729
self.application_name = ''
2830
self.server_address = 'http://localhost:4040'
2931
self.auth_token = ''
@@ -34,7 +36,7 @@ def initialize(*)
3436
self.report_thread_id = false
3537
self.log_level = 'error'
3638
self.tags = {}
37-
super
39+
self.compression = 'gzip'
3840
end
3941
end
4042

@@ -63,10 +65,11 @@ def configure
6365

6466
# Initialize Logging
6567
Rust.initialize_logging(@log_level)
66-
68+
6769

6870
# initialize Pyroscope Agent
6971
Rust.initialize_agent(
72+
# these are defaults in case user-provided values are nil:
7073
@config.app_name || @config.application_name || "",
7174
@config.server_address || "",
7275
@config.auth_token || "",
@@ -75,7 +78,8 @@ def configure
7578
@config.oncpu || false,
7679
@config.report_pid || false,
7780
@config.report_thread_id || false,
78-
tags_to_string(@config.tags || {})
81+
tags_to_string(@config.tags || {}),
82+
@config.compression || ""
7983
)
8084
end
8185

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Pyroscope
2-
VERSION = '0.3.2'.freeze
2+
VERSION = '0.4.0'.freeze
33
end

src/pyroscope.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::{
22
collections::HashMap,
33
marker::PhantomData,
4+
str::FromStr,
45
sync::{
56
mpsc::{self, Sender},
67
Arc, Condvar, Mutex,
@@ -18,6 +19,7 @@ use crate::{
1819
};
1920

2021
use crate::backend::BackendImpl;
22+
use crate::pyroscope::Compression::GZIP;
2123

2224
const LOG_TAG: &str = "Pyroscope::Agent";
2325

@@ -44,6 +46,8 @@ pub struct PyroscopeConfig {
4446
pub auth_token: Option<String>,
4547
/// Function to apply
4648
pub func: Option<fn(Report) -> Report>,
49+
/// Pyroscope http request body compression
50+
pub compression: Option<Compression>,
4751
}
4852

4953
impl Default for PyroscopeConfig {
@@ -59,6 +63,7 @@ impl Default for PyroscopeConfig {
5963
spy_name: "undefined".to_string(),
6064
auth_token: None,
6165
func: None,
66+
compression: None,
6267
}
6368
}
6469
}
@@ -80,6 +85,7 @@ impl PyroscopeConfig {
8085
spy_name: String::from("undefined"), // Spy Name should be set by the backend
8186
auth_token: None, // No authentication token
8287
func: None, // No function
88+
compression: None,
8389
}
8490
}
8591

@@ -150,6 +156,22 @@ impl PyroscopeConfig {
150156
..self
151157
}
152158
}
159+
160+
161+
/// Set the http request body compression.
162+
///
163+
/// # Example
164+
/// ```ignore
165+
/// use pyroscope::pyroscope::PyroscopeConfig;
166+
/// let config = PyroscopeConfig::new("http://localhost:8080", "my-app")
167+
/// .compression(GZIP);
168+
/// ```
169+
pub fn compression(self, compression: Compression) -> Self {
170+
Self {
171+
compression: Some(compression),
172+
..self
173+
}
174+
}
153175
}
154176

155177
/// PyroscopeAgent Builder
@@ -288,6 +310,22 @@ impl PyroscopeAgentBuilder {
288310
}
289311
}
290312

313+
/// Set the http request body compression.
314+
///
315+
/// # Example
316+
/// ```ignore
317+
/// use pyroscope::pyroscope::PyroscopeConfig;
318+
/// let agent = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
319+
/// .compression(GZIP)
320+
/// .build();
321+
/// ```
322+
pub fn compression(self, compression: Compression) -> Self {
323+
Self {
324+
config: self.config.compression(compression),
325+
..self
326+
}
327+
}
328+
291329
/// Initialize the backend, timer and return a PyroscopeAgent with Ready
292330
/// state. While you can call this method, you should call it through the
293331
/// `PyroscopeAgent.build()` method.
@@ -344,6 +382,21 @@ impl PyroscopeAgentBuilder {
344382
}
345383
}
346384

385+
#[derive(Clone, Debug)]
386+
pub enum Compression {
387+
GZIP
388+
}
389+
390+
impl FromStr for Compression {
391+
type Err = ();
392+
fn from_str(input: &str) -> std::result::Result<Compression, Self::Err> {
393+
match input {
394+
"gzip" => Ok(GZIP),
395+
_ => Err(()),
396+
}
397+
}
398+
}
399+
347400
/// This trait is used to encode the state of the agent.
348401
pub trait PyroscopeAgentState {}
349402

src/session.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ use std::{
22
sync::mpsc::{sync_channel, Receiver, SyncSender},
33
thread::{self, JoinHandle},
44
time::Duration,
5+
io::Write,
56
};
67

78
use reqwest::Url;
9+
use libflate::gzip::Encoder;
810

911
use crate::{
1012
backend::Report,
11-
pyroscope::PyroscopeConfig,
13+
pyroscope::{PyroscopeConfig, Compression},
1214
utils::{get_time_range, merge_tags_with_app_name},
1315
Result,
1416
};
@@ -192,6 +194,15 @@ impl Session {
192194
if let Some(auth_token) = self.config.auth_token.clone() {
193195
req_builder = req_builder.bearer_auth(auth_token);
194196
}
197+
let body = match &self.config.compression {
198+
None => report_u8,
199+
Some(Compression::GZIP) => {
200+
req_builder = req_builder.header("Content-Encoding", "gzip");
201+
let mut encoder = Encoder::new(Vec::new()).unwrap();
202+
encoder.write_all(&report_u8).unwrap();
203+
encoder.finish().into_result().unwrap()
204+
}
205+
};
195206

196207
// Send the request
197208
req_builder
@@ -203,7 +214,7 @@ impl Session {
203214
("sampleRate", &format!("{}", self.config.sample_rate)),
204215
("spyName", self.config.spy_name.as_str()),
205216
])
206-
.body(report_u8)
217+
.body(body)
207218
.timeout(Duration::from_secs(10))
208219
.send()?;
209220
Ok(())

0 commit comments

Comments
 (0)