Skip to content

Commit

Permalink
docs and cleanup the type parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
guswynn committed Jun 13, 2022
1 parent 51a0b8c commit 71743c4
Showing 1 changed file with 57 additions and 36 deletions.
93 changes: 57 additions & 36 deletions tracing-subscriber/src/reload.rs
@@ -1,15 +1,26 @@
//! Wrapper for a `Collect` or `Subscribe` to allow it to be dynamically reloaded.
//!
//! This module provides a type implementing [`Subscribe`] which wraps another type implementing
//! the [`Subscribe`] trait, allowing the wrapped type to be replaced with another
//! This module provides a type implementing [`Subscribe`] and [`Filter`]
//! which wraps another type implementing the corresponding trait. This
//! allows the wrapped type to be replaced with another
//! instance of that type at runtime.
//!
//! This can be used in cases where a subset of `Collect` functionality
//! This can be used in cases where a subset of `Collect` or `Filter` functionality
//! should be dynamically reconfigured, such as when filtering directives may
//! change at runtime. Note that this subscriber introduces a (relatively small)
//! amount of overhead, and should thus only be used as needed.
//!
//! ## Note
//!
//! The [`Subscribe`] implementation is unable to implement downcasting functionality,
//! so certain `Subscribers` fail to function properly if wrapped in a
//! `reload::Subscriber`.
//!
//! If you only want to be able to dynamically change the
//! `Filter` on your layer, prefer wrapping that `Filter` in the `reload::Subscriber`.
//!
//! [`Subscribe`]: crate::Subscribe
//! [`Filter`]: crate::subscribe::Filter
use crate::subscribe;
use crate::sync::RwLock;

Expand All @@ -23,7 +34,10 @@ use tracing_core::{
span, Event, Metadata,
};

/// Wraps a `Collect` or `Subscribe`, allowing it to be reloaded dynamically at runtime.
/// Wraps a `Filter` or `Subscribe`, allowing it to be reloaded dynamically at runtime.
///
/// [`Filter`]: crate::subscribe::Filter
/// [`Subscribe`]: crate::Subscribe
#[derive(Debug)]
pub struct Subscriber<S> {
// TODO(eliza): this once used a `crossbeam_util::ShardedRwLock`. We may
Expand Down Expand Up @@ -119,25 +133,6 @@ where
}
}

impl<S> Subscriber<S> {
/// Wraps the given `Subscribe`, returning a subscriber and a `Handle` that allows
/// the inner type to be modified at runtime.
pub fn new(inner: S) -> (Self, Handle<S>) {
let this = Self {
inner: Arc::new(RwLock::new(inner)),
};
let handle = this.handle();
(this, handle)
}

/// Returns a `Handle` that can be used to reload the wrapped `Subscribe`.
pub fn handle(&self) -> Handle<S> {
Handle {
inner: Arc::downgrade(&self.inner),
}
}
}

#[cfg(all(feature = "registry", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))]
impl<S, C> crate::subscribe::Filter<C> for Subscriber<S>
Expand Down Expand Up @@ -191,24 +186,50 @@ where
}
}

impl<T> Subscriber<T> {
/// Wraps the given `Subscribe` or `Filter`,
/// returning a subscriber or filter and a `Handle` that allows
/// the inner type to be modified at runtime.
///
/// [`Filter`]: crate::subscribe::Filter
/// [`Subscribe`]: crate::Subscribe
pub fn new(inner: T) -> (Self, Handle<T>) {
let this = Self {
inner: Arc::new(RwLock::new(inner)),
};
let handle = this.handle();
(this, handle)
}

/// Returns a `Handle` that can be used to reload the wrapped `Subscribe`.
pub fn handle(&self) -> Handle<T> {
Handle {
inner: Arc::downgrade(&self.inner),
}
}
}

// ===== impl Handle =====

impl<S> Handle<S> {
/// Replace the current subscriber with the provided `new_subscriber`.
impl<T> Handle<T> {
/// Replace the current subscriber or filter with the provided `new_object`.
///
/// **Warning:** The [`Filtered`](crate::filter::Filtered) type currently can't be changed
/// **Warning:** If this `Handle` is being used with the [`Filtered`](crate::filter::Filtered)
/// subscriber, it currently can't be changed
/// at runtime via the [`Handle::reload`] method.
/// Use the [`Handle::modify`] method to change the filter instead.
/// (see <https://github.com/tokio-rs/tracing/issues/1629>)
pub fn reload(&self, new_subscriber: impl Into<S>) -> Result<(), Error> {
self.modify(|subscriber| {
*subscriber = new_subscriber.into();
/// (see <https://github.com/tokio-rs/tracing/issues/1629>) However, if you only want to change
/// the underlying [`Filter`](crate::subscribe::Filter), then use the `reload::Subscriber` to
/// wrap the `Filter` itself.
pub fn reload(&self, new_object: impl Into<T>) -> Result<(), Error> {
self.modify(|object| {
*object = new_object.into();
})
}

/// Invokes a closure with a mutable reference to the current subscriber,
/// allowing it to be modified in place.
pub fn modify(&self, f: impl FnOnce(&mut S)) -> Result<(), Error> {
pub fn modify(&self, f: impl FnOnce(&mut T)) -> Result<(), Error> {
let inner = self.inner.upgrade().ok_or(Error {
kind: ErrorKind::CollectorGone,
})?;
Expand All @@ -235,16 +256,16 @@ impl<S> Handle<S> {

/// Returns a clone of the subscriber's current value if it still exists.
/// Otherwise, if the collector has been dropped, returns `None`.
pub fn clone_current(&self) -> Option<S>
pub fn clone_current(&self) -> Option<T>
where
S: Clone,
T: Clone,
{
self.with_current(S::clone).ok()
self.with_current(T::clone).ok()
}

/// Invokes a closure with a borrowed reference to the current subscriber,
/// returning the result (or an error if the collector no longer exists).
pub fn with_current<T>(&self, f: impl FnOnce(&S) -> T) -> Result<T, Error> {
pub fn with_current<Output>(&self, f: impl FnOnce(&T) -> Output) -> Result<Output, Error> {
let inner = self.inner.upgrade().ok_or(Error {
kind: ErrorKind::CollectorGone,
})?;
Expand All @@ -253,7 +274,7 @@ impl<S> Handle<S> {
}
}

impl<S> Clone for Handle<S> {
impl<T> Clone for Handle<T> {
fn clone(&self) -> Self {
Handle {
inner: self.inner.clone(),
Expand Down

0 comments on commit 71743c4

Please sign in to comment.