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

add an environment-configured console_subscriber::init #53

Merged
merged 18 commits into from
Jun 22, 2021

Conversation

jbr
Copy link
Contributor

@jbr jbr commented Jun 18, 2021

closes #49

@jbr jbr force-pushed the console-subscriber-init branch from 103021e to f8edc47 Compare June 18, 2021 19:30
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.

overall, this looks like the right general direction; i had some minor style and documentation suggestions.

Comment on lines 108 to 117
/**
Configures this builder from a standard set of environment variables:

* `TOKIO_CONSOLE_RETENTION`: The number of seconds to accumulate
completed tracing data. Default: 60s.
* `TOKIO_CONSOLE_BIND`: a HOST:PORT description, such as
localhost:1234 or similar. Default: 127.0.0.1:6669
* `TOKIO_CONSOLE_PUBLISH_INTERVAL`: The number of milliseconds to wait
between sending updates to the console. Default: 1000ms (1s)
*/
Copy link
Member

Choose a reason for hiding this comment

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

style nit: elsewhere we use ///-style doc comments exclusively...i could be convinced to switch to using block comments for doc comments, but i'd prefer to be consistent about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm a big fan of /** / /*! for doc comments because it's less visually distracting, but in this case it was just habit and I should have checked for project convention

console-subscriber/src/builder.rs Outdated Show resolved Hide resolved
console-subscriber/src/builder.rs Outdated Show resolved Hide resolved
console-subscriber/src/builder.rs Outdated Show resolved Hide resolved
console-subscriber/src/builder.rs Outdated Show resolved Hide resolved
Comment on lines 27 to 31
Starts the console subscriber server on its own thread

This function represents the easiest way to get started using
tokio-console.

Copy link
Member

Choose a reason for hiding this comment

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

these docs should probably state explicitly that this function also sets the default tracing subscriber (and maybe include a link to the tracing docs?). otherwise, i can see people being surprised if they try to set their own subscriber and it fails because the default subscriber is already set.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in f2b6de5

/// localhost:1234 or similar. Default: 127.0.0.1:6669
/// * `TOKIO_CONSOLE_PUBLISH_INTERVAL`: The number of milliseconds to wait
/// between sending updates to the console. Default: 1000ms (1s)

Copy link
Member

Choose a reason for hiding this comment

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

take it or leave it: it would be nice if the docs for this also included an example of using the builder to configure a console layer from the default environment variables , and then using it to build a subscriber, spawn the background task, etc? but, we can come back and add that later.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before I saw your review on this PR, I attempted to address this use case by adding a single in-code extension point: init_with_layer, with the hope that we can accommodate all common initialization patterns and discourage anyone who would need docs on how to eject from doing so. With the addition of init_with_layer, have we covered all of those use cases? An advantage of this approach is that if the majority of users are going through init or init_with_layer, future changes to those functions wouldn't require app code change, whereas if we encourage a copy-paste initializer through documentation, we have no way of pushing out changes to that "ejected" code.

console-subscriber/src/builder.rs Outdated Show resolved Hide resolved
Comment on lines 38 to 47
* `TOKIO_CONSOLE_RETENTION`: The number of seconds to accumulate
completed tracing data. Default: 60s.
* `TOKIO_CONSOLE_BIND`: a HOST:PORT description, such as
localhost:1234 or similar. Default: 127.0.0.1:6669
* `TOKIO_CONSOLE_PUBLISH_INTERVAL`: The number of milliseconds to wait
between sending updates to the console. Default: 1000ms (1s)
* `RUST_LOG`: configure the tracing filter. Default: `tokio=trace`,
and any additional filtering directives will be appended to this
default. See [`EnvFilter`] for further information on the format
for this variable.
Copy link
Member

Choose a reason for hiding this comment

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

take it or leave it: similarly, this might want to be a table?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in f2b6de5

console-subscriber/src/lib.rs Outdated Show resolved Hide resolved

/// Starts the console subscriber server with an additional subscriber
/// layer, for example a log layer. This interface can be combined
/// with any of the environment configuration documented at [`init`].
Copy link
Member

Choose a reason for hiding this comment

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

it would be nice to show an example here, but it's okay to add that in a follow-up branch if you prefer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's a good example layer that people might want to add on here? My inexperience with tracing means I don't have a good intuition for what people might want

EnvFilter::from_default_env().add_directive("tokio=trace".parse().unwrap());

let console_subscriber = tracing_subscriber::registry()
.with(fmt::layer())
Copy link
Member

Choose a reason for hiding this comment

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

do we always want to include a fmt layer? this means that users can't customize how events are logged, even if they call init_with_layer. i might just only add the fmt layer in init.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm new to tracing, but layers seem to be order-dependent, and there's no way to prepend because everything is composed through wrapping, so I'm not sure how to extract the fmt layer. Any suggestions?

Do you imagine replacing the fmt layer to be a commonly-needed extension point? I'm trying to figure out how to balance keeping the usage simple with providing the right sort of escape hatches for common customization

Copy link
Member

Choose a reason for hiding this comment

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

I'm new to tracing, but layers seem to be order-dependent, and there's no way to prepend because everything is composed through wrapping, so I'm not sure how to extract the fmt layer. Any suggestions?

layers are not order dependent :)

Do you imagine replacing the fmt layer to be a commonly-needed extension point? I'm trying to figure out how to balance keeping the usage simple with providing the right sort of escape hatches for common customization

the fmt layer is very configurable. i think wanting to change how events are formatted by providing your own fmt layer is probably going to be one of the most common use-cases for overriding the default configuration. for example, if you want to change the log format from textual to JSON, the current API means that you would have to use the builder and do everything manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in bb2c2db — there's now init and build and build doesn't add the fmt layer. this does involve a bit of heavy-handed documentation because that might be surprising

Comment on lines 42 to 44
pub fn init_with_layer<AdditionalLayer>(additional_layer: AdditionalLayer)
where
AdditionalLayer: Layer<ConsoleSubscriberLayer> + Send + Sync + 'static,
Copy link
Member

Choose a reason for hiding this comment

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

an alternative API design is to have a function that spawns the background thread, and just returns a Registry with the EnvFilter and TasksLayer layers already added. that way, the user can call any methods on that Registry to add additional layers, and set it as the default subscriber themselves. the init function could just call that and then immediately call init() on the returned registry.

i feel like this design could be more intuitive than taking a layer as an argument, but it might be slightly wordier to use...i could be convinced either way.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That does feel more flexible. What if instead of init_with_layer it was build() -> impl Layer + Into<Dispatch>, with #[must_use].

console_subscriber::build().with(…).init()
console_subscriber::init(); // shorthand for console_subscriber::build().init();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

addressed in bb2c2db — build and init

console-subscriber/src/init.rs Show resolved Hide resolved
console-subscriber/src/init.rs Outdated Show resolved Hide resolved
console-subscriber/src/init.rs Outdated Show resolved Hide resolved
console-subscriber/src/init.rs Outdated Show resolved Hide resolved
use tokio::runtime;
use tracing_subscriber::{fmt, layer::Layered, prelude::*, EnvFilter, Registry};

type ConsoleSubscriberLayer = Layered<TasksLayer, Layered<EnvFilter, Registry>>;
Copy link
Member

Choose a reason for hiding this comment

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

nit (and this isn't that important, since this isn't part of the public API), i would just call this

Suggested change
type ConsoleSubscriberLayer = Layered<TasksLayer, Layered<EnvFilter, Registry>>;
type ConsoleSubscriber = Layered<TasksLayer, Layered<EnvFilter, Registry>>;

this type isn't actually a Layer, since it includes a subscriber --- it doesn't implement the Layer trait, it implements the Subscriber trait.

Comment on lines +75 to +76
/// ```

Copy link
Member

Choose a reason for hiding this comment

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

nit: extra whitespace here

Suggested change
/// ```
/// ```

console-subscriber/src/init.rs Outdated Show resolved Hide resolved
/// ```

#[must_use = "build() without init() will not attach a tracing subscriber"]
pub fn build() -> ConsoleSubscriberLayer {
Copy link
Member

Choose a reason for hiding this comment

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

take it or leave it: i'm not totally sold on the name build() for this --- it doesn't feel super clear to me how this differs from init().. i might have called this subscriber() or something, because it returns a Subscriber. but, i'm not attached to that, this is fine.

console-subscriber/src/init.rs Outdated Show resolved Hide resolved
@hawkw hawkw merged commit 5142a60 into tokio-rs:main Jun 22, 2021
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.

subscriber: provide a simpler 'init' function
2 participants