Skip to content
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

Instrument attribute spans do not appear using log feature #2585

Closed
detly opened this issue May 10, 2023 · 2 comments
Closed

Instrument attribute spans do not appear using log feature #2585

detly opened this issue May 10, 2023 · 2 comments
Labels
crate/tracing Related to the `tracing` crate kind/bug Something isn't working

Comments

@detly
Copy link
Contributor

detly commented May 10, 2023

Bug Report

Version

0.1.37

Platform

Description

I would like to use tracing and its #[instrument] attribute while also using env_logger. We might eventually use a proper subscriber approach, but we've done some work to get env_logger working the way we want it in our system and this is a good way to incrementally migrate over to tracing.

The documentation says:

Additionally, log records are also generated when spans are entered, exited, and closed.

With this code:

/*
[dependencies]
env_logger = { version = "0.10" }
log = "0.4"
tracing = { version = "0.1", features = ["log"] }
*/

#[tracing::instrument]
fn check(x: u32) -> Option<u32> {
    log::trace!("{x}");
    Some(x)
}
    
fn main() {
    env_logger::Builder::new()
        .filter_level(log::LevelFilter::Trace)
        .filter_module("tracing::span", log::LevelFilter::Trace)
        .filter_module("tracing::span::active", log::LevelFilter::Trace)
        .format_timestamp(None)
        .init();

    let _s = tracing::span!(tracing::Level::TRACE, "main").entered();
    
    let r = check(5);
    log::trace!("Check {:?}", r);
}

I get this output (running with no Rust or Tracing related env vars set):

[TRACE tracing::span] main;
[TRACE tracing::span::active] -> main;
[TRACE playground] 5
[TRACE playground] Check Some(5)
[TRACE tracing::span::active] <- main;
[TRACE tracing::span] -- main;

Here's a demo in the Rust Explorer.

I am confused by seeing the main span but not the check span. I would expect both to be present or absent depending on the log level.

I also noted this in the docs:

Since these additional span lifecycle logs have the potential to be very verbose, and don’t include additional fields, they will always be emitted at the Trace level, rather than inheriting the level of the span that generated them. Furthermore, they are are categorized under a separate log target, “tracing::span” (and its sub-target, “tracing::span::active”, for the logs on entering and exiting a span), which may be enabled or disabled separately from other log records emitted by tracing.

Hence the extra filter_module() filters in there. I don't think they affect it though. It is also not a level problem, as the other trace messages appear.

The example works as expected using a fmt() subscriber.

@hawkw
Copy link
Member

hawkw commented May 12, 2023

Huh, that seems quite weird. I would also expect the check span to generate span activity logs. This definitely seems like a bug to me...

@hawkw hawkw added kind/bug Something isn't working crate/tracing Related to the `tracing` crate labels May 12, 2023
@miwig
Copy link
Contributor

miwig commented May 24, 2023

I believe the problem is this line. As I understand it, level_enabled! only returns true if a subscriber is set, but the log feature doesn't use a subscriber, as seen here in the span! macro. There, level_enabled! is equipped with an else-block that checks if_log_enabled!. But in the code generated by instrument, that check is never reached due to the problematic line.

@hawkw hawkw closed this as completed in 81ceb65 Sep 5, 2023
davidbarsky pushed a commit that referenced this issue Sep 26, 2023
## Motivation

The instrument macro currently doesn't work with the "log" crate
feature: #2585

## Solution

Change the generated code to create a span if either
`tracing::if_log_enabled!` or `tracing::level_enabled!`. I'm not sure
how to add a test for this or if this is the best solution.

Fixes #2585
davidbarsky pushed a commit that referenced this issue Sep 27, 2023
## Motivation

The instrument macro currently doesn't work with the "log" crate
feature: #2585

## Solution

Change the generated code to create a span if either
`tracing::if_log_enabled!` or `tracing::level_enabled!`. I'm not sure
how to add a test for this or if this is the best solution.

Fixes #2585
davidbarsky pushed a commit that referenced this issue Sep 27, 2023
## Motivation

The instrument macro currently doesn't work with the "log" crate
feature: #2585

## Solution

Change the generated code to create a span if either
`tracing::if_log_enabled!` or `tracing::level_enabled!`. I'm not sure
how to add a test for this or if this is the best solution.

Fixes #2585
davidbarsky pushed a commit that referenced this issue Sep 27, 2023
## Motivation

The instrument macro currently doesn't work with the "log" crate
feature: #2585

## Solution

Change the generated code to create a span if either
`tracing::if_log_enabled!` or `tracing::level_enabled!`. I'm not sure
how to add a test for this or if this is the best solution.

Fixes #2585
davidbarsky pushed a commit that referenced this issue Sep 27, 2023
## Motivation

The instrument macro currently doesn't work with the "log" crate
feature: #2585

## Solution

Change the generated code to create a span if either
`tracing::if_log_enabled!` or `tracing::level_enabled!`. I'm not sure
how to add a test for this or if this is the best solution.

Fixes #2585
davidbarsky pushed a commit that referenced this issue Sep 29, 2023
## Motivation

The instrument macro currently doesn't work with the "log" crate
feature: #2585

## Solution

Change the generated code to create a span if either
`tracing::if_log_enabled!` or `tracing::level_enabled!`. I'm not sure
how to add a test for this or if this is the best solution.

Fixes #2585
hawkw pushed a commit that referenced this issue Oct 1, 2023
## Motivation

The instrument macro currently doesn't work with the "log" crate
feature: #2585

## Solution

Change the generated code to create a span if either
`tracing::if_log_enabled!` or `tracing::level_enabled!`. I'm not sure
how to add a test for this or if this is the best solution.

Fixes #2585
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crate/tracing Related to the `tracing` crate kind/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants