feat: integrating tokio-console - #110
Conversation
hawkw
left a comment
There was a problem hiding this comment.
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(())
}
No description provided.