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

feat: integrating tokio-console #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yhio
Copy link

@yhio yhio commented Oct 17, 2022

No description provided.

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this!

I notice that the console and otel features are not additive. If both are enabled, then we will define two separate versions of the set_up_logging function, which conflict with each other and cause a compiler error.

In idiomatic Rust, feature flags should always be additive and not conflict with each other. This is more of an issue for libraries, rather than application binaries like mini-redis, because multiple dependencies that both depend on a third crate may enable different sets of feature flags. However, since mini-redis is intended as an educational project, I think we should probably show what's considered the best practice.

I think we should change this code so that the console and otel features are not mutually exclusive. We would want to have a set_up_logging function that enables the Tokio console if the console feature is enabled, enables OpenTelemetry if the otel feature is enabled, and enables both if both features are enabled. We could do this by moving the feature flag cfg attributes inside the function, something like this:

fn set_up_logging() -> mini_redis::Result<()> {
    // layers which we apply the `EnvFilter` to (applying it to the
    // `ConsoleLayer` will break tokio-console.
    let filtered_layers = fmt::Layer::default();

    #[cfg(feature = "otel")];
    let filtered_layers = {
         // ... set up all the opentelemetry stuff ...
         let opentelemetry = /* ... */;
         // combine the `fmt` and `opentelemetry` layers so we
         // can apply the `EnvFilter` to both of them
         filtered_layers.and_then(opentelemetry)
    };

    // Parse an `EnvFilter` configuration from the `RUST_LOG`
    // environment variable.
    let filter = EnvFilter::from_default_env();
    let filtered_layers = filtered_layers.with_filter(filter);

    // Use the tracing subscriber `Registry`, or any other subscriber
    // that impls `LookupSpan`
    let registry = tracing_subscriber::registry().with(filtered_layers);

    // Add a `tokio-console` layer if enabled.
    #[cfg(feature = "console")]
    let registry = registry.with(console_subscriber::spawn());

    // set the subscriber as the default
    registry.try_init()?;

    Ok(())
}

@yhio yhio requested a review from hawkw October 18, 2022 02:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants