Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: disable tracing file log by default #48

Merged
merged 2 commits into from
Feb 1, 2025
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
2 changes: 1 addition & 1 deletion Cargo.lock

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

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,10 @@ To renew the SSL certificate, you can run the same command as above:
The gateway uses the [tracing](https://docs.rs/tracing) crate for logging. There are two tracing outputs configured:

- output to **stdout**, which has the `info` level and can be configured with the `RUST_LOG_STDOUT` env variable, see below;
- output to a **file**, which is saved in the `data/traces/` folder and has the default `trace` level. The file name is `gateway_{start-timestamp}.log`. It can be configured with the `RUST_LOG_FILE` env variable, see below.
- output to a **file**, which is saved in the `data/traces/` folder and is disabled by default. The file name is `gateway_{start-timestamp}.log`. It can be configured with the `RUST_LOG_FILE` env variable, see below.

The `RUST_LOG` environment variable enables to set different levels for each module. See the [EnvFilter](https://docs.rs/tracing-subscriber/0.3.17/tracing_subscriber/filter/struct.EnvFilter.html) documentation for more details.
For example, to set the tracing level to `debug`, you can run:
The `RUST_LOG_*` environment variable enables to set different levels for each module. See the [EnvFilter](https://docs.rs/tracing-subscriber/0.3.17/tracing_subscriber/filter/struct.EnvFilter.html) documentation for more details.
For example, to set the tracing level to `debug` for both `stdout` and `file`, you can run:

```
RUST_LOG_FILE=ic_websocket_gateway=debug RUST_LOG_STDOUT=ic_websocket_gateway=debug cargo run
Expand Down
12 changes: 6 additions & 6 deletions src/ic-websocket-gateway/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ic_websocket_gateway"
version = "1.4.5"
version = "1.4.6"
edition.workspace = true
rust-version.workspace = true
repository.workspace = true
Expand All @@ -24,11 +24,11 @@ futures-util = "0.3"
structopt = "0.3"
tracing = { workspace = true }
tracing-subscriber = { version = "0.3", features = [
"fmt",
"std",
"json",
"env-filter",
"registry",
"fmt",
"std",
"json",
"env-filter",
"registry",
] }
tracing-appender = "0.2"
tracing-opentelemetry = "0.22"
Expand Down
86 changes: 52 additions & 34 deletions src/ic-websocket-gateway/src/gateway_tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
path::Path,
time::{SystemTime, UNIX_EPOCH},
};
use tracing::level_filters::LevelFilter;
use tracing_appender::non_blocking::WorkerGuard;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{prelude::*, EnvFilter};
Expand All @@ -20,40 +21,57 @@ pub fn init_tracing(
opentelemetry_collector_endpoint: Option<String>,
gateway_principal: Principal,
) -> Result<InitTracingResult, String> {
if !Path::new("./data/traces").is_dir() {
fs::create_dir("./data/traces").map_err(|e| e.to_string())?;
}

let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| e.to_string())?;
let filename = format!("./data/traces/gateway_{:?}.log", timestamp.as_millis());

println!("Tracing to file: {}", filename);

let log_file = File::create(filename).map_err(|e| e.to_string())?;
let (non_blocking_file, guard_file) = tracing_appender::non_blocking(log_file);
let (non_blocking_stdout, guard_stdout) = tracing_appender::non_blocking(std::io::stdout());

let env_filter_file = EnvFilter::builder()
.with_env_var("RUST_LOG_FILE")
.try_from_env()
.unwrap_or_else(|_| EnvFilter::new("ic_websocket_gateway=trace"));

let file_tracing_layer = tracing_subscriber::fmt::layer()
.json()
.with_writer(non_blocking_file)
.with_thread_ids(true)
.with_filter(env_filter_file);

let env_filter_stdout = EnvFilter::builder()
.with_env_var("RUST_LOG_STDOUT")
.try_from_env()
.unwrap_or_else(|_| EnvFilter::new("ic_websocket_gateway=info"));
let stdout_tracing_layer = tracing_subscriber::fmt::layer()
.with_writer(non_blocking_stdout)
.pretty()
.with_filter(env_filter_stdout);
// stdout tracing
let (stdout_tracing_layer, guard_stdout) = {
let (non_blocking_stdout, guard_stdout) = tracing_appender::non_blocking(std::io::stdout());

let env_filter_stdout = EnvFilter::builder()
.with_env_var("RUST_LOG_STDOUT")
.try_from_env()
.unwrap_or_else(|_| EnvFilter::new("ic_websocket_gateway=info"));
let stdout_tracing_layer = tracing_subscriber::fmt::layer()
.with_writer(non_blocking_stdout)
.pretty()
.with_filter(env_filter_stdout);

(stdout_tracing_layer, guard_stdout)
};

// file tracing
let (file_tracing_layer, guard_file) = {
if !Path::new("./data/traces").is_dir() {
fs::create_dir("./data/traces").map_err(|e| e.to_string())?;
}

let timestamp = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map_err(|e| e.to_string())?;
let filename = format!("./data/traces/gateway_{:?}.log", timestamp.as_millis());

println!("Tracing to file: {}", filename);

let log_file = File::create(filename).map_err(|e| e.to_string())?;
let (non_blocking_file, guard_file) = tracing_appender::non_blocking(log_file);

let env_filter_file = EnvFilter::builder()
.with_env_var("RUST_LOG_FILE")
.try_from_env()
.unwrap_or_else(|_| {
// disable file tracing by default
EnvFilter::builder()
.with_default_directive(LevelFilter::OFF.into())
.parse("")
.expect("failed to parse default filter for file tracing")
});

let file_tracing_layer = tracing_subscriber::fmt::layer()
.json()
.with_writer(non_blocking_file)
.with_thread_ids(true)
.with_filter(env_filter_file);

(file_tracing_layer, guard_file)
};

let is_telemetry_enabled =
match opentelemetry_collector_endpoint
Expand Down