1
1
//! Wrapper for a `Collect` or `Subscribe` to allow it to be dynamically reloaded.
2
2
//!
3
- //! This module provides a type implementing [`Subscribe`] which wraps another type implementing
4
- //! the [`Subscribe`] trait, allowing the wrapped type to be replaced with another
3
+ //! This module provides a type implementing [`Subscribe`] and [`Filter`]
4
+ //! which wraps another type implementing the corresponding trait. This
5
+ //! allows the wrapped type to be replaced with another
5
6
//! instance of that type at runtime.
6
7
//!
7
- //! This can be used in cases where a subset of `Collect` functionality
8
+ //! This can be used in cases where a subset of `Collect` or `Filter` functionality
8
9
//! should be dynamically reconfigured, such as when filtering directives may
9
10
//! change at runtime. Note that this subscriber introduces a (relatively small)
10
11
//! amount of overhead, and should thus only be used as needed.
11
12
//!
13
+ //! ## Note
14
+ //!
15
+ //! //! The [`Subscribe`] implementation is unable to implement downcasting functionality,
16
+ //! so certain `Subscribers` will fail to reload if wrapped in a `reload::Subscriber`.
17
+ //!
18
+ //! If you only want to be able to dynamically change the
19
+ //! `Filter` on your layer, prefer wrapping that `Filter` in the `reload::Subscriber`.
20
+ //!
12
21
//! [`Subscribe`]: crate::Subscribe
22
+ //! [`Filter`]: crate::subscribe::Filter
13
23
use crate :: subscribe;
14
24
use crate :: sync:: RwLock ;
15
25
@@ -23,7 +33,10 @@ use tracing_core::{
23
33
span, Event , Metadata ,
24
34
} ;
25
35
26
- /// Wraps a `Collect` or `Subscribe`, allowing it to be reloaded dynamically at runtime.
36
+ /// Wraps a `Filter` or `Subscribe`, allowing it to be reloaded dynamically at runtime.
37
+ ///
38
+ /// [`Filter`]: crate::subscribe::Filter
39
+ /// [`Subscribe`]: crate::Subscribe
27
40
#[ derive( Debug ) ]
28
41
pub struct Subscriber < S > {
29
42
// TODO(eliza): this once used a `crossbeam_util::ShardedRwLock`. We may
@@ -119,25 +132,6 @@ where
119
132
}
120
133
}
121
134
122
- impl < S > Subscriber < S > {
123
- /// Wraps the given `Subscribe`, returning a subscriber and a `Handle` that allows
124
- /// the inner type to be modified at runtime.
125
- pub fn new ( inner : S ) -> ( Self , Handle < S > ) {
126
- let this = Self {
127
- inner : Arc :: new ( RwLock :: new ( inner) ) ,
128
- } ;
129
- let handle = this. handle ( ) ;
130
- ( this, handle)
131
- }
132
-
133
- /// Returns a `Handle` that can be used to reload the wrapped `Subscribe`.
134
- pub fn handle ( & self ) -> Handle < S > {
135
- Handle {
136
- inner : Arc :: downgrade ( & self . inner ) ,
137
- }
138
- }
139
- }
140
-
141
135
#[ cfg( all( feature = "registry" , feature = "std" ) ) ]
142
136
#[ cfg_attr( docsrs, doc( cfg( all( feature = "registry" , feature = "std" ) ) ) ) ]
143
137
impl < S , C > crate :: subscribe:: Filter < C > for Subscriber < S >
@@ -191,24 +185,50 @@ where
191
185
}
192
186
}
193
187
188
+ impl < T > Subscriber < T > {
189
+ /// Wraps the given `Subscribe` or `Filter`,
190
+ /// returning a subscriber or filter and a `Handle` that allows
191
+ /// the inner type to be modified at runtime.
192
+ ///
193
+ /// [`Filter`]: crate::subscribe::Filter
194
+ /// [`Subscribe`]: crate::Subscribe
195
+ pub fn new ( inner : T ) -> ( Self , Handle < T > ) {
196
+ let this = Self {
197
+ inner : Arc :: new ( RwLock :: new ( inner) ) ,
198
+ } ;
199
+ let handle = this. handle ( ) ;
200
+ ( this, handle)
201
+ }
202
+
203
+ /// Returns a `Handle` that can be used to reload the wrapped `Subscribe`.
204
+ pub fn handle ( & self ) -> Handle < T > {
205
+ Handle {
206
+ inner : Arc :: downgrade ( & self . inner ) ,
207
+ }
208
+ }
209
+ }
210
+
194
211
// ===== impl Handle =====
195
212
196
- impl < S > Handle < S > {
197
- /// Replace the current subscriber with the provided `new_subscriber`.
213
+ impl < T > Handle < T > {
214
+ /// Replace the current subscriber or filter with the provided `new_value`.
215
+ ///
216
+ /// **Warning:** [`Handle::reload`] cannot be used with the [`Filtered`](crate::filter::Filtered)
217
+ /// subscriber; use [`Handle::modify`] instead (this [this issue] for additional details).
218
+ ///
219
+ /// However, if the _only_ the [`Filter`] needs to be modified, use `reload::Subscriber` to
220
+ /// to wrap the `Filter` directly.
198
221
///
199
- /// **Warning:** The [`Filtered`](crate::filter::Filtered) type currently can't be changed
200
- /// at runtime via the [`Handle::reload`] method.
201
- /// Use the [`Handle::modify`] method to change the filter instead.
202
- /// (see <https://github.com/tokio-rs/tracing/issues/1629>)
203
- pub fn reload ( & self , new_subscriber : impl Into < S > ) -> Result < ( ) , Error > {
204
- self . modify ( |subscriber| {
205
- * subscriber = new_subscriber. into ( ) ;
222
+ /// [this issue]: https://github.com/tokio-rs/tracing/issues/1629
223
+ pub fn reload ( & self , new_value : impl Into < T > ) -> Result < ( ) , Error > {
224
+ self . modify ( |object| {
225
+ * object = new_value. into ( ) ;
206
226
} )
207
227
}
208
228
209
229
/// Invokes a closure with a mutable reference to the current subscriber,
210
230
/// allowing it to be modified in place.
211
- pub fn modify ( & self , f : impl FnOnce ( & mut S ) ) -> Result < ( ) , Error > {
231
+ pub fn modify ( & self , f : impl FnOnce ( & mut T ) ) -> Result < ( ) , Error > {
212
232
let inner = self . inner . upgrade ( ) . ok_or ( Error {
213
233
kind : ErrorKind :: CollectorGone ,
214
234
} ) ?;
@@ -235,16 +255,16 @@ impl<S> Handle<S> {
235
255
236
256
/// Returns a clone of the subscriber's current value if it still exists.
237
257
/// Otherwise, if the collector has been dropped, returns `None`.
238
- pub fn clone_current ( & self ) -> Option < S >
258
+ pub fn clone_current ( & self ) -> Option < T >
239
259
where
240
- S : Clone ,
260
+ T : Clone ,
241
261
{
242
- self . with_current ( S :: clone) . ok ( )
262
+ self . with_current ( T :: clone) . ok ( )
243
263
}
244
264
245
265
/// Invokes a closure with a borrowed reference to the current subscriber,
246
266
/// returning the result (or an error if the collector no longer exists).
247
- pub fn with_current < T > ( & self , f : impl FnOnce ( & S ) -> T ) -> Result < T , Error > {
267
+ pub fn with_current < T2 > ( & self , f : impl FnOnce ( & T ) -> T2 ) -> Result < T2 , Error > {
248
268
let inner = self . inner . upgrade ( ) . ok_or ( Error {
249
269
kind : ErrorKind :: CollectorGone ,
250
270
} ) ?;
@@ -253,7 +273,7 @@ impl<S> Handle<S> {
253
273
}
254
274
}
255
275
256
- impl < S > Clone for Handle < S > {
276
+ impl < T > Clone for Handle < T > {
257
277
fn clone ( & self ) -> Self {
258
278
Handle {
259
279
inner : self . inner . clone ( ) ,
0 commit comments