Skip to content

Commit 51a1e26

Browse files
committed
Add --trace-json option that outputs a JSON trace file
If `--trace-json` option is used, a `trace-TIMESTAMP.json` file will be created which can be opened by tools like ui.perfetto.dev Signed-off-by: Marcel Guzik <marcel.guzik@cumulocity.com>
1 parent c4a9fb2 commit 51a1e26

File tree

7 files changed

+38
-6
lines changed

7 files changed

+38
-6
lines changed

Cargo.lock

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ tokio-util = { version = "0.7", features = ["codec"] }
182182
toml = "0.8"
183183
tower = "0.4"
184184
tracing = { version = "0.1", features = ["attributes", "log"] }
185+
tracing-chrome = "0.7.2"
185186
tracing-subscriber = { version = "0.3", features = ["time", "env-filter"] }
186187
try-traits = "0.1"
187188
tungstenite = "0.20"

crates/common/tedge_config/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ tedge_utils = { workspace = true, features = ["timestamp"] }
3535
thiserror = { workspace = true }
3636
toml = { workspace = true }
3737
tracing = { workspace = true }
38+
tracing-chrome = { workspace = true }
3839
tracing-subscriber = { workspace = true }
3940
url = { workspace = true }
4041
which = { workspace = true }

crates/common/tedge_config/src/cli.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ pub struct LogConfigArgs {
4141
#[clap(long, global = true)]
4242
#[clap(add(ArgValueCandidates::new(log_level_completions)))]
4343
pub log_level: Option<tracing::Level>,
44+
45+
/// Emit a JSON trace file that can be opened by ui.perfetto.dev
46+
#[clap(long, global = true)]
47+
pub trace_json: bool,
4448
}
4549

4650
fn log_level_completions() -> Vec<CompletionCandidate> {

crates/common/tedge_config/src/system_services/log_config.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use camino::Utf8Path;
2+
use tracing_chrome::ChromeLayerBuilder;
23
use tracing_subscriber::fmt::format::FmtSpan;
34
use tracing_subscriber::prelude::*;
45
use tracing_subscriber::EnvFilter;
@@ -21,7 +22,7 @@ pub fn log_init(
2122
sname: &str,
2223
flags: &LogConfigArgs,
2324
config_dir: &Utf8Path,
24-
) -> Result<(), SystemServiceError> {
25+
) -> Result<Option<tracing_chrome::FlushGuard>, SystemServiceError> {
2526
let print_file_and_line = std::env::var("RUST_LOG").is_ok();
2627
let file_level = get_log_level(sname, config_dir)?;
2728

@@ -37,9 +38,20 @@ pub fn log_init(
3738
.with_line_number(print_file_and_line)
3839
.with_filter(filter_layer);
3940

40-
tracing_subscriber::registry().with(fmt_layer).init();
41+
// chrome layer if `--trace-json`
42+
let (chrome_layer, guard) = if flags.trace_json {
43+
let (chrome_layer, guard) = ChromeLayerBuilder::new().include_args(true).build();
44+
(Some(chrome_layer), Some(guard))
45+
} else {
46+
(None, None)
47+
};
48+
49+
tracing_subscriber::registry()
50+
.with(chrome_layer)
51+
.with(fmt_layer)
52+
.init();
4153

42-
Ok(())
54+
Ok(guard)
4355
}
4456

4557
fn filter_layer(flags: &LogConfigArgs, file_level: tracing::Level) -> EnvFilter {

crates/core/tedge_mapper/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ pub async fn run(mapper_opt: MapperOpt) -> anyhow::Result<()> {
126126
tedge_config::TEdgeConfigLocation::from_custom_root(&mapper_opt.common.config_dir);
127127
let config = tedge_config::TEdgeConfig::try_new(tedge_config_location.clone())?;
128128

129-
log_init(
129+
let _guard = log_init(
130130
"tedge-mapper",
131131
&mapper_opt.common.log_args,
132132
&tedge_config_location.tedge_config_root_path,

plugins/tedge_apt_plugin/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ mod tests {
410410
log_args: LogConfigArgs {
411411
debug: false,
412412
log_level: None,
413+
trace_json: false,
413414
},
414415
config_dir: "".into(),
415416
},

0 commit comments

Comments
 (0)