Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions proxy_agent/src/common/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@ fn log(log_level: LoggerLevel, message: String) {
if let Some(log_for_event) = config::get_file_log_level_for_events() {
if log_for_event >= log_level {
// write to event
let (module_name, caller_name) =
proxy_agent_shared::logger::get_caller_info("proxy_agent::common::logger");
event_logger::write_event_only(
log_level,
message.to_string(),
&caller_name,
&module_name,
"CommonLogger",
"ProxyAgent",
);
}
}
Expand Down
7 changes: 2 additions & 5 deletions proxy_agent/src/proxy/proxy_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,11 @@ impl ConnectionLogger {
if let Some(log_for_event) = crate::common::config::get_file_log_level_for_events() {
if log_for_event >= logger_level {
// write to event
let (module_name, caller_name) = proxy_agent_shared::logger::get_caller_info(
"proxy_agent::proxy::proxy_connection",
);
proxy_agent_shared::telemetry::event_logger::write_event_only(
logger_level,
message.to_string(),
&caller_name,
&module_name,
"ConnectionLogger",
"ProxyAgent",
);
}
}
Expand Down
1 change: 0 additions & 1 deletion proxy_agent_shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ thiserror = "1.0.64"
tokio = { version = "1", features = ["rt", "macros", "sync", "time"] }
log = { version = "0.4.26", features = ["std"] }
ctor = "0.3.6" # used for test setup and clean up
backtrace = "0.3" # used for get the caller module and function name

[target.'cfg(windows)'.dependencies]
windows-service = "0.7.0" # windows NT service
Expand Down
81 changes: 0 additions & 81 deletions proxy_agent_shared/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,74 +35,6 @@ fn get_log_header_with_length(
header
}

const ASYNC_FUNCTION_NAME: &str = "{closure";
const INTERNAL_BACKTRACE: &str = "backtrace";
const CURRENT_FUNCTION: &str = "::logger::get_caller_info";
const RUNTIME_TASK_SCHEDULER: &str = "::runtime::task::Schedule"; // The runtime scheduler function that runs the tasks
const TOKIO_RUNTIME_SCHEDULER: &str = "::runtime::scheduler"; // The runtime scheduler function that runs the tasks, found in command `cargo llvm-cov --target x86_64-unknown-linux-musl` with rust-1.88.0

#[cfg(test)]
const TEST_CODE_COVERAGE_FUNCTIONS: &str = "__covrec_"; // code coverage recording, generated by a code coverage tool such as llvm-cov, or a similar instrumentation framework
#[cfg(test)]
const TEST_INTERNAL_FUNCTION: &str = "test::run_test"; // The test framework's internal function that runs the tests

pub fn get_caller_info(module_to_skip: &str) -> (String, String) {
let bt = backtrace::Backtrace::new();

for frame in bt.frames().iter() {
for symbol in frame.symbols() {
if let Some(name) = symbol.name() {
let name_str = name.to_string();

let mut found_function: bool = true;
// Skip internal frames, current function frame and `module_to_skip` to find the first relevant caller
found_function = found_function
&& !name_str.contains(INTERNAL_BACKTRACE)
&& !name_str.contains(CURRENT_FUNCTION)
&& !name_str.contains(RUNTIME_TASK_SCHEDULER)
&& !name_str.contains(TOKIO_RUNTIME_SCHEDULER)
&& !name_str.contains(module_to_skip);

#[cfg(test)]
{
found_function = found_function
&& !name_str.contains(TEST_CODE_COVERAGE_FUNCTIONS)
&& !name_str.starts_with(TEST_INTERNAL_FUNCTION);
}

if found_function {
// If the name contains `{{closure}}`, it indicates an async function
// We need to find the first segment that contains the async function name
// Example: `azure_proxy_agent::proxy::proxy_server::ProxyServer::handle_new_tcp_connection::{{closure}}::{{closure}}::h537d19fb7a504d22`
let seg = name_str.split("::").collect::<Vec<_>>();
let seg_len = seg.len();
let mut function_last_index = 0;
for i in 0..seg_len {
if seg[seg_len - 1 - i].contains(ASYNC_FUNCTION_NAME) {
function_last_index = i + 1;
}
}
let caller_name = seg
.get(seg_len - 1 - function_last_index)
.unwrap_or(&"unknown")
.to_string();
// Get the module name from the first to `function_last_index` segment
let module_name = seg
.into_iter()
.map(String::from)
.collect::<Vec<_>>()
.into_iter()
.take(seg_len - 1 - function_last_index)
.collect::<Vec<_>>()
.join("::");
return (module_name, caller_name);
}
}
}
}
("unknown".to_string(), "unknown".to_string())
}

#[cfg(test)]
mod tests {
use log::Level;
Expand All @@ -126,19 +58,6 @@ mod tests {
);
}

#[tokio::test]
async fn invoke_get_caller_info_test() {
test_get_caller_info_test("invoke_get_caller_info_test");
}

fn test_get_caller_info_test(expected_caller_name: &str) {
let (module_name, caller_name) = super::get_caller_info("test_get_caller_info_test");
println!("Module Name: {}", module_name);
println!("Caller Name: {}", caller_name);
// Check if the caller name is as expected
assert_eq!(caller_name, expected_caller_name);
}

#[test]
fn get_log_header_with_length_test() {
let header = super::get_log_header_with_length(
Expand Down
Loading