-
Notifications
You must be signed in to change notification settings - Fork 421
Tracing PoC #4211
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
base: main
Are you sure you want to change the base?
Tracing PoC #4211
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,8 +46,12 @@ use crate::types::features::{Bolt11InvoiceFeatures, ChannelTypeFeatures}; | |
| use crate::types::payment::{PaymentHash, PaymentPreimage, PaymentSecret}; | ||
| use crate::types::string::UntrustedString; | ||
| use crate::util::errors::APIError; | ||
| use crate::util::logger::TracingToLogger; | ||
| use crate::util::ser::Writeable; | ||
| use crate::util::test_utils; | ||
| use crate::util::test_utils::{self, TestTracerLayer}; | ||
|
|
||
| use tracing_subscriber::fmt::format::FmtSpan; | ||
| use tracing_subscriber::{fmt, prelude::*}; | ||
|
|
||
| use bitcoin::hashes::sha256::Hash as Sha256; | ||
| use bitcoin::hashes::Hash; | ||
|
|
@@ -61,6 +65,7 @@ use crate::ln::functional_test_utils::*; | |
| use crate::routing::gossip::NodeId; | ||
|
|
||
| use core::cmp::Ordering; | ||
| use std::sync::Arc; | ||
| #[cfg(feature = "std")] | ||
| use std::thread; | ||
|
|
||
|
|
@@ -428,11 +433,25 @@ fn mpp_receive_timeout() { | |
|
|
||
| #[test] | ||
| fn test_keysend_payments() { | ||
| // tracing_subscriber::fmt().init(); | ||
| // tracing_subscriber::fmt() | ||
| // // .with_span_list(true) // <--- print parent span chain | ||
| // .with_span_events(FmtSpan::FULL) | ||
| // .init(); | ||
|
|
||
| do_test_keysend_payments(false); | ||
| do_test_keysend_payments(true); | ||
| } | ||
|
|
||
| fn do_test_keysend_payments(public_node: bool) { | ||
| let layer = TestTracerLayer {}; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Configure layer that produces |
||
| let subscriber = tracing_subscriber::registry().with(layer); | ||
| let _guard = tracing::subscriber::set_default(subscriber); | ||
| { | ||
| let _enter = tracing::info_span!("hello").entered(); | ||
| tracing::info!("Hello from tracing!"); | ||
| } | ||
|
|
||
| let chanmon_cfgs = create_chanmon_cfgs(2); | ||
| let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); | ||
| let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,11 @@ | |
| //! [`Level`] field. Each module may have its own Logger or share one. | ||
|
|
||
| use bitcoin::secp256k1::PublicKey; | ||
| use tracing::Event; | ||
| use tracing::Subscriber; | ||
| use tracing_subscriber::layer::Context; | ||
| use tracing_subscriber::registry::LookupSpan; | ||
| use tracing_subscriber::Layer; | ||
|
|
||
| use core::cmp; | ||
| use core::fmt; | ||
|
|
@@ -261,6 +266,81 @@ impl<T: fmt::Display, I: core::iter::Iterator<Item = T> + Clone> fmt::Display fo | |
| } | ||
| } | ||
|
|
||
| /// A tracing `Layer` that forwards tracing events to a given `Logger`. | ||
| pub struct TracingToLogger<L: Deref> | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently unused, but demonstrates how we can stay backwards compatible with a |
||
| where | ||
| L::Target: Logger, | ||
| { | ||
| logger: L, | ||
| } | ||
|
|
||
| impl<L: Deref> TracingToLogger<L> | ||
| where | ||
| L::Target: Logger, | ||
| { | ||
| pub fn new(logger: L) -> Self { | ||
| Self { logger } | ||
| } | ||
| } | ||
|
|
||
| impl<S, L> Layer<S> for TracingToLogger<L> | ||
| where | ||
| S: Subscriber + for<'lookup> LookupSpan<'lookup>, | ||
| L: Deref + 'static, | ||
| L::Target: Logger, | ||
| { | ||
| fn on_event(&self, event: &Event<'_>, _ctx: Context<'_, S>) { | ||
| let meta = event.metadata(); | ||
| let level = *meta.level(); | ||
|
|
||
| // Extract the "message" field from the event | ||
| let mut visitor = MessageVisitor::new(); | ||
| event.record(&mut visitor); | ||
| let message = visitor.message.unwrap_or_else(|| "<no message>".to_string()); | ||
|
|
||
| self.logger.log(Record::new( | ||
| match level { | ||
| tracing::Level::ERROR => Level::Error, | ||
| tracing::Level::WARN => Level::Warn, | ||
| tracing::Level::INFO => Level::Info, | ||
| tracing::Level::DEBUG => Level::Debug, | ||
| tracing::Level::TRACE => Level::Trace, | ||
| }, | ||
| None, | ||
| None, | ||
| format_args!("{}", message), | ||
| meta.module_path().unwrap_or("<unknown>"), | ||
| meta.file().unwrap_or("<unknown>"), | ||
| meta.line().unwrap_or(0), | ||
| None, | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| struct MessageVisitor { | ||
| message: Option<String>, | ||
| } | ||
|
|
||
| impl MessageVisitor { | ||
| fn new() -> Self { | ||
| Self { message: None } | ||
| } | ||
| } | ||
|
|
||
| impl tracing::field::Visit for MessageVisitor { | ||
| fn record_debug(&mut self, field: &tracing::field::Field, value: &dyn fmt::Debug) { | ||
| if field.name() == "message" { | ||
| self.message = Some(format!("{:?}", value)); | ||
| } | ||
| } | ||
|
|
||
| fn record_str(&mut self, field: &tracing::field::Field, value: &str) { | ||
| if field.name() == "message" { | ||
| self.message = Some(value.to_string()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use crate::ln::types::ChannelId; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -183,39 +183,39 @@ macro_rules! log_given_level { | |
| #[macro_export] | ||
| macro_rules! log_error { | ||
| ($logger: expr, $($arg:tt)*) => ( | ||
| $crate::log_given_level!($logger, $crate::util::logger::Level::Error, $($arg)*); | ||
| tracing::error!($($arg)*); | ||
| ) | ||
| } | ||
|
|
||
| /// Log at the `WARN` level. | ||
| #[macro_export] | ||
| macro_rules! log_warn { | ||
| ($logger: expr, $($arg:tt)*) => ( | ||
| $crate::log_given_level!($logger, $crate::util::logger::Level::Warn, $($arg)*); | ||
| tracing::warn!($($arg)*); | ||
| ) | ||
| } | ||
|
|
||
| /// Log at the `INFO` level. | ||
| #[macro_export] | ||
| macro_rules! log_info { | ||
| ($logger: expr, $($arg:tt)*) => ( | ||
| $crate::log_given_level!($logger, $crate::util::logger::Level::Info, $($arg)*); | ||
| tracing::info!($($arg)*); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No logger instance needed.... |
||
| ) | ||
| } | ||
|
|
||
| /// Log at the `DEBUG` level. | ||
| #[macro_export] | ||
| macro_rules! log_debug { | ||
| ($logger: expr, $($arg:tt)*) => ( | ||
| $crate::log_given_level!($logger, $crate::util::logger::Level::Debug, $($arg)*); | ||
| tracing::debug!($($arg)*); | ||
| ) | ||
| } | ||
|
|
||
| /// Log at the `TRACE` level. | ||
| #[macro_export] | ||
| macro_rules! log_trace { | ||
| ($logger: expr, $($arg:tt)*) => ( | ||
| $crate::log_given_level!($logger, $crate::util::logger::Level::Trace, $($arg)*) | ||
| tracing::trace!($($arg)*); | ||
| ) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default trace output, useful to see what's all that we have.