Skip to content

Conversation

@Pothulapati
Copy link
Contributor

Motivation

Fixes #894

Solution

This PR implements Layer for Option, allowing users to wrap a Layer with
an Option, allowing it to be passed internally wherever Layer is used
there by allowing by allowing layers to be enabled or disabled.

Using this with reload further allows a Layer to be dynamically
toggled based by using handle.modify

This PR also consists of a basic example.

Signed-off-by: Tarun Pothulapati tarunpothulapati@outlook.com

Fixes tokio-rs#894

This PR implements Layer for Option, allowing users to wrap a Layer with
an Option, allowing it to be passed internally wherever Layer is used
there by allowing by allowing layers to be enabled or disabled.

Using this with `reload` further allows a Layer to be dynamically
toggled based by using `handle.modify`

This PR also consists of a basic example.

Signed-off-by: Tarun Pothulapati <tarunpothulapati@outlook.com>
@Pothulapati Pothulapati requested review from a team and hawkw as code owners August 9, 2020 11:31
@Pothulapati Pothulapati changed the title Impl Layer for Option Impl Layer for Option Aug 9, 2020
Signed-off-by: Tarun Pothulapati <tarunpothulapati@outlook.com>
@Pothulapati Pothulapati changed the title Impl Layer for Option Impl Layer for Option<T:Layer> Aug 9, 2020
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 approach. I had some suggestions.

Comment on lines +841 to +844
match self {
Some(ref inner) => inner.max_level_hint(),
None => None,
}
Copy link
Member

Choose a reason for hiding this comment

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

Nit, take it or leave it: this could also be expressed using Option::and_then, like this:

Suggested change
match self {
Some(ref inner) => inner.max_level_hint(),
None => None,
}
self.and_then(Layer::max_level_hint)

Comment on lines +30 to +36
let (json, plain) = if matches.is_present("json") {
(Some(tracing_subscriber::fmt::layer().json()), None)
} else {
(None, Some(tracing_subscriber::fmt::layer()))
};

tracing_subscriber::registry().with(json).with(plain).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 have an abbreviated version of this example in the docs for the Layer trait. Maybe also a version showing runtime reloading in the layer::reload docs.

Signed-off-by: Tarun Pothulapati <tarunpothulapati@outlook.com>
Signed-off-by: Tarun Pothulapati <tarunpothulapati@outlook.com>
Signed-off-by: Tarun Pothulapati <tarunpothulapati@outlook.com>
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.

Great, this looks good to me, although I had a couple of non-blocking suggestions.

It might also be nice to have a variant of some of the benchmarks to measure how much overhead is introduced by skipping a None layer, but I'm not going to block merging on that either.

And, I'd still like to see examples in the API docs for Layer and for reload to demonstrate the use of Option layers. But, that can also be added later if necessary.

@hawkw hawkw merged commit e9b6645 into tokio-rs:master Aug 14, 2020
@Pothulapati
Copy link
Contributor Author

@hawkw I was planning to update this PR to show a layered reload example, along with the benchmarking suggestion over the weekend. Sorry!

I will try to open another PR on the same! :)

hawkw added a commit that referenced this pull request Sep 8, 2020
Fixed

- **env-filter**: Fixed a regression where `Option<Level>` lost its
  `Into<LevelFilter>` impl ([#966])
- **env-filter**: Fixed `EnvFilter` enabling spans that should not be
  enabled when multiple subscribers are in use ([#927])

Changed

- **json**: `format::Json` now outputs fields in a more readable order
  ([#892])
- Updated `tracing-core` dependency to 0.1.16

Added

- **fmt**: Add `BoxMakeWriter` for erasing the type of a `MakeWriter`
  implementation ([#958])
- **fmt**: Add `TestWriter` `MakeWriter` implementation to support
  libtest output capturing ([#938])
- **layer**: Add `Layer` impl for `Option<T> where T: Layer` ([#910])
- **env-filter**: Add `From<Level>` impl for `Directive` ([#918])
- Multiple documentation fixes and improvements

Thanks to @Pothulapati, @samrg472, @bryanburgers, @keetonian, and
@SriRamanujam for contributing to this release!

[#927]: #927
[#966]: #966
[#958]: #958
[#892]: #892
[#938]: #938
[#910]: #910
[#918]: #918
hawkw added a commit that referenced this pull request Sep 9, 2020
# 0.2.12 (September 8, 2020)

### Fixed

- **env-filter**: Fixed a regression where `Option<Level>` lost its
  `Into<LevelFilter>` impl ([#966])
- **env-filter**: Fixed `EnvFilter` enabling spans that should not be enabled
  when multiple subscribers are in use ([#927])
  
### Changed

- **json**: `format::Json` now outputs fields in a more readable order ([#892])
- Updated `tracing-core` dependency to 0.1.16

### Added

- **fmt**: Add `BoxMakeWriter` for erasing the type of a `MakeWriter`
  implementation ([#958])
- **fmt**: Add `TestWriter` `MakeWriter` implementation to support libtest
  output capturing ([#938])
- **layer**: Add `Layer` impl for `Option<T> where T: Layer` ([#910])
- **env-filter**: Add `From<Level>` impl for `Directive` ([#918])
- Multiple documentation fixes and improvements

Thanks to @Pothulapati, @samrg472, @bryanburgers, @keetonian, 
and @SriRamanujam for contributing to this release!

[#927]: #927
[#966]: #966
[#958]: #958
[#892]: #892
[#938]: #938
[#910]: #910
[#918]: #918
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: impl Layer for Option<T: Layer>

2 participants