Skip to content

Commit

Permalink
optimized logging cost (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheroz committed Nov 16, 2023
1 parent 64434e8 commit cb59dd1
Show file tree
Hide file tree
Showing 12 changed files with 47 additions and 104 deletions.
2 changes: 1 addition & 1 deletion anor-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

* tracing used for logs
* using `tracing` for logs

## 0.1.1 (2023-11-03)

Expand Down
16 changes: 4 additions & 12 deletions anor-api/src/service/api_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ impl ApiService for Service {
}
*/
Err(e) => {
if tracing::enabled!(tracing::Level::TRACE) {
tracing::error!("couldn't get client: {e:?}")
}
tracing::error!("couldn't get client: {e:?}")
}
}
}
Expand Down Expand Up @@ -103,24 +101,18 @@ impl ApiService for Service {
}

fn handle_connection(mut stream: TcpStream, addr: SocketAddr, shutdown: Arc<AtomicBool>) {
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("Client connected: {}", addr);
}
tracing::debug!("Client connected: {}", addr);

let mut buf = [0; 1024];
let addr = stream.peer_addr().unwrap();
while !shutdown.load(Ordering::SeqCst) {
let count = stream.read(&mut buf).unwrap();
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("Received bytes count from {} : {}", addr, count);
}
tracing::trace!("Received bytes count from {} : {}", addr, count);

let mut vec = buf.to_vec();
vec.truncate(count);
let msg = String::from_utf8(vec).unwrap();

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("Received message from {} : {}", addr, msg);
}
tracing::trace!("Received message from {} : {}", addr, msg);
}
}
2 changes: 1 addition & 1 deletion anor-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

* tracing used for logs
* using `tracing` for logs
* project moved into anor workspace
* added http service

Expand Down
18 changes: 7 additions & 11 deletions anor-http/src/client/http_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ pub async fn request_url(
}
});

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!(
"File client connected to {}://{}:{}",
url.scheme().unwrap(),
host,
port
);
}
tracing::trace!(
"File client connected to {}://{}:{}",
url.scheme().unwrap(),
host,
port
);

let authority = url.authority().unwrap().clone();

Expand All @@ -89,9 +87,7 @@ pub async fn request_url(
);
}

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("Request:\n{:#?}", req);
}
tracing::trace!("Request:\n{:#?}", req);

let mut res = sender.send_request(req).await?;

Expand Down
40 changes: 12 additions & 28 deletions anor-http/src/service/http_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,7 @@ async fn start(
}

async fn file_service(req: Request<hyper::body::Incoming>) -> Result<Response<Full<Bytes>>> {
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("recevied request:{:#?}", req);
}
tracing::trace!("recevied request:{:#?}", req);

match *req.method() {
Method::HEAD => file_info(&req).await,
Expand Down Expand Up @@ -151,9 +149,7 @@ fn blank_response(status_code: StatusCode) -> Response<Full<Bytes>> {
async fn file_info(req: &Request<hyper::body::Incoming>) -> Result<Response<Full<Bytes>>> {
let path = req.uri().path().replace('/', "");
let file_path = Path::new(&path);
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("file path:{:?}", file_path);
}
tracing::debug!("file path:{:?}", file_path);

if file_path.file_name().is_none() {
tracing::error!("filename is empty");
Expand All @@ -168,9 +164,7 @@ async fn file_info(req: &Request<hyper::body::Incoming>) -> Result<Response<Full
.header(hyper::header::CONTENT_LENGTH, file_len)
.body(Full::new(Bytes::new()))
{
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("response:{:#?}", response);
}
tracing::trace!("response:{:#?}", response);
Ok(response)
} else {
tracing::error!("unable to build response");
Expand All @@ -189,13 +183,11 @@ async fn get_file_len(filename: &Path) -> ServiceResult<u64> {
let metadata = file.metadata().await?;
if metadata.is_file() {
let file_len = metadata.len();
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!(
"The length of the file {:?} is {} bytes",
filename,
file_len
)
}
tracing::trace!(
"The length of the file {:?} is {} bytes",
filename,
file_len
);
return Ok(file_len);
}
let err_msg = format!("Not a file: {:?}", filename);
Expand All @@ -208,9 +200,7 @@ async fn file_send(req: &Request<hyper::body::Incoming>) -> Result<Response<Full

let path = req.uri().path().replace('/', "");
let file_path = Path::new(&path);
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("file path: {:?}", file_path);
}
tracing::debug!("file path: {:?}", file_path);

if file_path.file_name().is_none() {
tracing::error!("filename is empty");
Expand Down Expand Up @@ -278,9 +268,7 @@ async fn send_file_range(
)
.body(Full::new(Bytes::new()))
{
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("Range Not Satisfiable (416). Requested range is out of existing content, {:?} > {}", http_range, content_length);
}
tracing::debug!("Range Not Satisfiable (416). Requested range is out of existing content, {:?} > {}", http_range, content_length);
return Ok(response);
} else {
tracing::error!("unable to build response");
Expand All @@ -300,13 +288,9 @@ async fn send_file_range(
let mut buffer = vec![0; capacity];
if let Ok(mut file) = tokio::fs::File::open(&filename).await {
if let Ok(_seek) = file.seek(SeekFrom::Start(range.start)).await {
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("seek result {}", _seek);
}
tracing::trace!("seek result {}", _seek);
if let Ok(read_count) = file.read_exact(&mut buffer).await {
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("read_count {}", read_count);
}
tracing::trace!("read_count {}", read_count);
let body: Bytes = buffer.into();
if let Ok(response) = Response::builder()
.status(StatusCode::PARTIAL_CONTENT)
Expand Down
2 changes: 1 addition & 1 deletion anor-server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

* tracing used for logs
* using `tracing` for logs
* project moved into anor workspace

## 0.1.0 (2023-10-26)
Expand Down
12 changes: 3 additions & 9 deletions anor-server/src/bin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ async fn main() {
.with(tracing_subscriber::fmt::layer())
.init();

if tracing::enabled!(tracing::Level::INFO) {
tracing::info!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
}
tracing::info!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));

// load the configuration
let config = config::load();
Expand Down Expand Up @@ -67,14 +65,10 @@ async fn main() {
let mut sigterm = signal(SignalKind::terminate()).unwrap();
tokio::select! {
_ = sigint.recv() => {
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("Recieved SIGINT");
}
tracing::debug!("Recieved SIGINT");
}
_ = sigterm.recv() => {
if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("Recieved SIGTERM");
}
tracing::debug!("Recieved SIGTERM");
},
};
graceful_shutdown(server_shutdown, config_cloned).await;
Expand Down
2 changes: 1 addition & 1 deletion anor-storage/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

* tracing used for logs
* using `tracing` for logs

## 0.1.3 (2023-11-03)

Expand Down
12 changes: 5 additions & 7 deletions anor-storage/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,11 @@ macro_rules! take_guard {
panic!("{}", err);
/*
let guard = poisoned.into_inner();
if tracing::log_enabled!(tracing::Level::Warn) {
tracing::warn!(
"{} recovered from poisoning: {:?}",
stringify!($g),
*guard
);
}
tracing::warn!(
"{} recovered from poisoning: {:?}",
stringify!($g),
*guard
);
guard
*/
}
Expand Down
2 changes: 1 addition & 1 deletion anor-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ All notable changes to this project will be documented in this file.

## Unreleased

* tracing used for logs
* using `tracing` for logs

## 0.1.4 (2023-11-03)

Expand Down
28 changes: 7 additions & 21 deletions anor-utils/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ pub fn load() -> Arc<Config> {
serde_yaml::from_str(&config_substituted)
.unwrap_or_else(|_| panic!("Could not parse {} file.", config_filename));

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("loaded config:\n{:#?}", config_map);
}
tracing::trace!("loaded config:\n{:#?}", config_map);

let mut config = Config {
storage: None,
Expand Down Expand Up @@ -118,9 +116,7 @@ pub fn load() -> Arc<Config> {
config.remote = Some(remote);
}

if tracing::enabled!(tracing::Level::DEBUG) {
tracing::debug!("parsed config:\n{:#?}", config);
}
tracing::debug!("parsed config:\n{:#?}", config);

Arc::new(config)
}
Expand All @@ -140,9 +136,7 @@ fn parse_listen_on(
vec![default_listen_address]
};

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("config: listen_addresses: {:?}", listen_addresses);
}
tracing::trace!("config: listen_addresses: {:?}", listen_addresses);

let node_key = "listen_port";
let port = if node.contains_key(node_key) {
Expand All @@ -151,9 +145,7 @@ fn parse_listen_on(
default_listen_port
};

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("config: listen_port: {}", port);
}
tracing::trace!("config: listen_port: {}", port);

let mut listen_on = Vec::<SocketAddr>::with_capacity(listen_addresses.len());
for listen_addres in listen_addresses {
Expand All @@ -162,9 +154,7 @@ fn parse_listen_on(
listen_on.push(socket_addres);
}

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("parsed: listen_on: {:?}", listen_on);
}
tracing::trace!("parsed: listen_on: {:?}", listen_on);

listen_on
}
Expand All @@ -191,19 +181,15 @@ fn parse_remote(node: &HashMap<String, String>) -> RemoteConfig {
vec![DEFAULT_REMOTE_NODE]
};

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("config: remote nodes: {:?}", remote_nodes);
}
tracing::trace!("config: remote nodes: {:?}", remote_nodes);

let mut nodes = Vec::<SocketAddr>::with_capacity(remote_nodes.len());
for node in remote_nodes {
let socket_addr: SocketAddr = node.parse().unwrap();
nodes.push(socket_addr);
}

if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("parsed: remote nodes: {:?}", nodes);
}
tracing::trace!("parsed: remote nodes: {:?}", nodes);

RemoteConfig { nodes }
}
Expand Down
15 changes: 4 additions & 11 deletions anor-utils/src/threadpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ impl Drop for ThreadPool {
drop(self.sender.take());

for worker in &mut self.workers {
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("Shutting down worker {}", worker.id);
}
tracing::trace!("Shutting down worker {}", worker.id);
if let Some(thread) = worker.thread.take() {
thread.join().unwrap();
}
Expand Down Expand Up @@ -82,16 +80,11 @@ impl Worker {

match message {
Ok(job) => {
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("Worker {id} got a job; executing.");
}

tracing::trace!("Worker {id} got a job; executing.");
job();
}
Err(_) => {
if tracing::enabled!(tracing::Level::TRACE) {
tracing::trace!("Worker {id} disconnected; shutting down.");
}
tracing::trace!("Worker {id} disconnected; shutting down.");
break;
}
}
Expand Down Expand Up @@ -133,7 +126,7 @@ pub mod test {
});
}
}

// wait for executed threads complete
// pool.wait_for_completion();
// drop(pool);
Expand Down

0 comments on commit cb59dd1

Please sign in to comment.