-
Notifications
You must be signed in to change notification settings - Fork 141
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
Add tracing-subscriber
to itf
#1430
base: main
Are you sure you want to change the base?
Add tracing-subscriber
to itf
#1430
Conversation
2ba08cc
to
0e5079f
Compare
@@ -16,6 +17,9 @@ use roles_logic_sv2::{ | |||
// Pool will connect to the Sniffer, and the Sniffer will connect to the Template Provider. | |||
#[tokio::test] | |||
async fn success_pool_template_provider_connection() { | |||
tracing_subscriber::fmt() |
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.
This is initialized in a single test in each file. This way it works for all tests in the file.. it is repetitive but could not think about a better way..
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.
We can set up a static logger in lib/mod.rs
, so every time the module is used, logger initialization runs automatically. Using once_cell::sync::Lazy
, we make sure it only initializes once. This should work I hope. Something like this
static LOGGER: Lazy<()> = Lazy::new(|| {
once_cell.call_once(|| {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();;
});
});
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.
deleted my previous comments here as I just realized I was writing nonsense... apologies for the noise!
please ignore them in case you're reading notifications on Discord
anyways, I could not get the suggestion from @Shourya742 above to work, as there seems to be no call_once
API on the once_cell
crate:
error[E0423]: expected value, found crate `once_cell`
--> tests-integration/lib/mod.rs:24:5
|
24 | once_cell.call_once(|| {
| ^^^^^^^^^- help: use the path separator to refer to an item: `::`
I suspect @Shourya742 mixed this with std::sync::Once
.
but indeed, the problem with the current approach is:
- it's repetitive, as we're forced to initialize the subscriber on each file
- it's a bit counter-intuitive, because if we initialize the subscriber more than 1x on each file, it breaks with:
Unable to install global subscriber: SetGlobalDefaultError("a global default trace dispatcher has already been set")
not the end of the world... just something we would have to be careful with...
but one middle-ground approach (that I validated locally) could be to add this to lib/mod.rs
:
use tracing_subscriber::EnvFilter;
use std::sync::Once;
static INIT: Once = Once::new();
pub fn init_tracing() {
INIT.call_once(|| {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
});
}
then call init_tracing()
on the test.
it should be sufficient to call init_tracing()
only 1x per file and it will enable logging on all tests of such file, just like it's currently done on this PR.
however, the advantage here is:
if for some reason init_tracing()
is called on multiple tests on the same file, it will not break with the error mentioned above.
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.
I just wrote psuedo code, as API's were not on top of my head. Let me check with concrete implementation of what meant and revert
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.
Sorry for the late response. I received the notification when @jbesraa made changes. We can add the ctor
crate so that whenever the integration_tests_sv2
module is referenced, the logger is automatically called without any manual intervention.
I've added commit dba3624 on my fork for cherry-picking.
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.
@jbesraa the ctor
approach above seems like an interesting alternative to the current approach of initializing the logger 1x per file
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.
not entirely sold on this. adding new dep just for this feels too much, and current approach is not too bad IMO. If you both think otherwise, happy to add it though.
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.
I don’t have a strong opinion on this either. It’s a nice feature to have, but it would have been even better if we could achieve something similar without an external dependency. Apparently, lazy initialization does not automatically trigger invocation upon module reference. The main reason this would make sense is to prevent confusion for new contributors regarding logger_initialization
. Either we avoid initializing the logger in tests altogether or ensure it is initialized in every test.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #1430 +/- ##
==========================================
- Coverage 18.06% 18.05% -0.02%
==========================================
Files 127 127
Lines 9555 9555
==========================================
- Hits 1726 1725 -1
- Misses 7829 7830 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
I think for local execution it's fine to leave this default, but we should consider doing |
f3e58c7
to
8b0da8e
Compare
Made changes based on the discussion above. Thank you. |
8b0da8e
to
33f58d1
Compare
tracing_subscriber::fmt() | ||
.with_env_filter(EnvFilter::from_default_env()) | ||
.init(); |
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.
tracing_subscriber::fmt() | |
.with_env_filter(EnvFilter::from_default_env()) | |
.init(); | |
tracing_subscriber::registry() | |
.with(fmt::layer()) | |
.with( | |
EnvFilter::builder() | |
.with_default_directive(LevelFilter::DEBUG.into()) | |
.from_env_lossy(), | |
) | |
.init(); |
Should we switch to this? Instead of relying on the env variables, we can override the log levels using an EnvFilter
layer in the tracing registry.
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.
Hmm current approach seems more versatile - allowing us to have different level in CI and locally. I prefer to keep it simple as is.
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.
This sets a default log level, eliminating the need to specify the RUST_LOG
environment variable every time we run a test. However, if RUST_LOG
is set, its value will take precedence over the default DEBUG
level. The logger will still be versatile but with a default log level to fallback on.
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.
current approach does not require setting RUST_LOG, if it is not set it will fallback to ERROR
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.
Ah!!!... I thought it might not set any level... works...
This should allow us to examine the logs from the roles that are using `tracing` crate for logging. As a default only ERROR logs will be shown, in order to examine a different level of logging, `RUST_LOG` env variable should be modified. For example `RUST_LOG=info cargo test --test '*'`
..previously musl were used
.. for more verbosity
..to more descriptive one
33f58d1
to
af7bd32
Compare
Rebased without further changes |
This should allow us to examine the logs from the roles that are using
tracing
crate for logging. As a default only ERROR logs will be shown, in order to examine a different level of logging,RUST_LOG
env variable should be modified.For example
RUST_LOG=info cargo test --test '*'
resolves #1370