-
Notifications
You must be signed in to change notification settings - Fork 27
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
How to test for spans? #4
Comments
This is how the "mock subscriber" is initialized: https://github.com/dbrgn/tracing-test/blob/main/tracing-test/src/subscriber.rs#L56-L66 It's been a while since I wrote that code, but we'd have to check how the span information is passed on to the subscriber. I'm currently a bit short on time, so if someone else wants to find out the reason for this behavior, contributions would be welcome! |
Related to #2. |
Ok, I see. I admit I have been refraining from writing my own tracing subscriber because I find it a very complicated API. Most of the time I just want the kind of output One possibility is to use |
Ah, sorry. I didn't realize that you were already using |
Ok, I found the first issue. The env filter restricts the log events to the current crate, but in case of integration tests that's the current file. In my case above that's "tracing". So no log events from my library that I am testing are in the output.
|
I changed the subscriber code to: // .with_env_filter(env_filter)
.with_max_level(tracing_core::Level::TRACE) After this my test is still failing, but this is the output:
The text I'm looking for is literally in there, so I don't yet know why |
The reason my test was still failing is that So in conclusion, I think a more robust method is needed. It might be a solution to just keep track of the index in the buffer when the test starts and and ends, or log a specific string like For the |
Hi @najamelan, thanks for your investigations! Yeah, the fact that scope matching is a bit wonky is the reason why this library is still marked as experimental... 😕 Disallowing parallel test runs is a bad idea in my view. We should try to find a solution that does not rely on non-interleaved tests.
Ahh, this statement confused me a bit, since here we have a test that spawns an async task on another thread. Being able to capture logs that are logged on other threads are precisely why this library exists. However, right now this only works for async logs on another thread (within the Tokio runtime), but not if the logs are in sync code on another thread. I'll have to investigate what our possibilities are. Right now, being able to apply a span to the code-under-test is important for the functionality we have (as you noticed).
That might be a valid workaround for now. |
For the record, to quote this reply:
|
For the record, in #3 @mooreniemi contributed a reproducer test: #[traced_test]
#[test]
fn annotate_target_sync_test() {
assert!(!logs_contain("Logging from a non-async test"));
info!(target: "target", "Logging from a non-async but targeted test");
assert!(logs_contain("Logging from a non-async but targeted test"));
assert!(!logs_contain("This was never logged"));
} |
I instrument futures with spans and would like to verify the correct spans are on my logs. This is my log without tracing-test:
In my integration test gives:
When replacing that with tracing-test macro, the assertion with
logs_contain
fails and the output shows:It looks like it both lost a whole lot of the log lines, as well as the spans.
The text was updated successfully, but these errors were encountered: