From 5b2f1cdfd54a073596645a6d02d58164ee6c2cee Mon Sep 17 00:00:00 2001 From: Kirill Fomichev Date: Thu, 22 Apr 2021 02:31:06 +0300 Subject: [PATCH] Add journald support through tracing-journald (#2034) * Add journald support through tracing-journald * change journald to use_journald * more fixes --- Cargo.lock | 11 ++++++++++ book/src/user/tracing.md | 11 ++++++++-- zebrad/Cargo.toml | 1 + zebrad/src/components/tracing/component.rs | 24 ++++++++++++++++------ zebrad/src/config.rs | 5 +++++ 5 files changed, 44 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a7284c35..affa54fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3738,6 +3738,16 @@ dependencies = [ "tracing", ] +[[package]] +name = "tracing-journald" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fe1f0ed2b7a5fcb6da2bc9e783587d9a0c8b9535e50224afe04e543eae8a2d6" +dependencies = [ + "tracing-core", + "tracing-subscriber 0.2.17", +] + [[package]] name = "tracing-log" version = "0.1.2" @@ -4505,6 +4515,7 @@ dependencies = [ "tracing-error", "tracing-flame", "tracing-futures", + "tracing-journald", "tracing-subscriber 0.2.17", "vergen", "zebra-chain", diff --git a/book/src/user/tracing.md b/book/src/user/tracing.md index be3acf93..620ef406 100644 --- a/book/src/user/tracing.md +++ b/book/src/user/tracing.md @@ -12,10 +12,17 @@ if the config had `endpoint_addr = '127.0.0.1:3000'`, then See the [`filter`][filter] documentation for more details. -Zebra also has support for generating [flamegraphs] of tracing spans, -configured using the [`flamegraph`][flamegraph] option. +Zebra also has support for: + +* Generating [flamegraphs] of tracing spans, configured using the +[`flamegraph`][flamegraph] option. +* Sending tracing spans and events to [systemd-journald][systemd_journald], +on Linux distributions that use `systemd`. Configured using the +[`use_journald`][use_journald] option. [tracing_section]: https://doc.zebra.zfnd.org/zebrad/config/struct.TracingSection.html [filter]: https://doc.zebra.zfnd.org/zebrad/config/struct.TracingSection.html#structfield.filter [flamegraph]: https://doc.zebra.zfnd.org/zebrad/config/struct.TracingSection.html#structfield.flamegraph [flamegraphs]: http://www.brendangregg.com/flamegraphs.html +[systemd_journald]: https://www.freedesktop.org/software/systemd/man/systemd-journald.service.html +[use_journald]: https://doc.zebra.zfnd.org/zebrad/config/struct.TracingSection.html#structfield.use_journald diff --git a/zebrad/Cargo.toml b/zebrad/Cargo.toml index a5e2cb35..48db2736 100644 --- a/zebrad/Cargo.toml +++ b/zebrad/Cargo.toml @@ -34,6 +34,7 @@ thiserror = "1" tracing = "0.1" tracing-futures = "0.2" tracing-flame = "0.1.0" +tracing-journald = "0.1.0" tracing-subscriber = { version = "0.2.17", features = ["tracing-log"] } tracing-error = "0.1.2" metrics = "0.13.0-alpha.8" diff --git a/zebrad/src/components/tracing/component.rs b/zebrad/src/components/tracing/component.rs index c0ac420e..f717787e 100644 --- a/zebrad/src/components/tracing/component.rs +++ b/zebrad/src/components/tracing/component.rs @@ -31,17 +31,29 @@ impl Tracing { .with_filter_reloading(); let filter_handle = builder.reload_handle(); - let subscriber = builder.finish().with(ErrorLayer::default()); - - let flamegrapher = if let Some(path) = flame_root { + let (flamelayer, flamegrapher) = if let Some(path) = flame_root { let (flamelayer, flamegrapher) = flame::layer(path); - subscriber.with(flamelayer).init(); - Some(flamegrapher) + (Some(flamelayer), Some(flamegrapher)) + } else { + (None, None) + }; + + let journaldlayer = if config.use_journald { + let layer = tracing_journald::layer() + .map_err(|e| FrameworkErrorKind::ComponentError.context(e))?; + Some(layer) } else { - subscriber.init(); None }; + let subscriber = builder.finish().with(ErrorLayer::default()); + match (flamelayer, journaldlayer) { + (None, None) => subscriber.init(), + (Some(layer1), None) => subscriber.with(layer1).init(), + (None, Some(layer2)) => subscriber.with(layer2).init(), + (Some(layer1), Some(layer2)) => subscriber.with(layer1).with(layer2).init(), + }; + Ok(Self { filter_handle, flamegrapher, diff --git a/zebrad/src/config.rs b/zebrad/src/config.rs index d836be15..963c37d4 100644 --- a/zebrad/src/config.rs +++ b/zebrad/src/config.rs @@ -105,6 +105,10 @@ pub struct TracingSection { /// If you provide a path with an extension the extension will be ignored and /// replaced with `.folded` and `.svg` for the respective files. pub flamegraph: Option, + + /// The use_journald flag sends tracing events to systemd-journald, on Linux + /// distributions that use systemd. + pub use_journald: bool, } impl Default for TracingSection { @@ -114,6 +118,7 @@ impl Default for TracingSection { filter: None, endpoint_addr: None, flamegraph: None, + use_journald: false, } } }