]> git.proxmox.com Git - rustc.git/blob - vendor/tracing-core/src/subscriber.rs
New upstream version 1.60.0+dfsg1
[rustc.git] / vendor / tracing-core / src / subscriber.rs
1 //! Subscribers collect and record trace data.
2 use crate::{span, Event, LevelFilter, Metadata};
3
4 use crate::stdlib::{
5 any::{Any, TypeId},
6 boxed::Box,
7 sync::Arc,
8 };
9
10 /// Trait representing the functions required to collect trace data.
11 ///
12 /// Crates that provide implementations of methods for collecting or recording
13 /// trace data should implement the `Subscriber` interface. This trait is
14 /// intended to represent fundamental primitives for collecting trace events and
15 /// spans — other libraries may offer utility functions and types to make
16 /// subscriber implementations more modular or improve the ergonomics of writing
17 /// subscribers.
18 ///
19 /// A subscriber is responsible for the following:
20 /// - Registering new spans as they are created, and providing them with span
21 /// IDs. Implicitly, this means the subscriber may determine the strategy for
22 /// determining span equality.
23 /// - Recording the attachment of field values and follows-from annotations to
24 /// spans.
25 /// - Filtering spans and events, and determining when those filters must be
26 /// invalidated.
27 /// - Observing spans as they are entered, exited, and closed, and events as
28 /// they occur.
29 ///
30 /// When a span is entered or exited, the subscriber is provided only with the
31 /// [ID] with which it tagged that span when it was created. This means
32 /// that it is up to the subscriber to determine whether and how span _data_ —
33 /// the fields and metadata describing the span — should be stored. The
34 /// [`new_span`] function is called when a new span is created, and at that
35 /// point, the subscriber _may_ choose to store the associated data if it will
36 /// be referenced again. However, if the data has already been recorded and will
37 /// not be needed by the implementations of `enter` and `exit`, the subscriber
38 /// may freely discard that data without allocating space to store it.
39 ///
40 /// ## Overriding default impls
41 ///
42 /// Some trait methods on `Subscriber` have default implementations, either in
43 /// order to reduce the surface area of implementing `Subscriber`, or for
44 /// backward-compatibility reasons. However, many subscribers will likely want
45 /// to override these default implementations.
46 ///
47 /// The following methods are likely of interest:
48 ///
49 /// - [`register_callsite`] is called once for each callsite from which a span
50 /// event may originate, and returns an [`Interest`] value describing whether or
51 /// not the subscriber wishes to see events or spans from that callsite. By
52 /// default, it calls [`enabled`], and returns `Interest::always()` if
53 /// `enabled` returns true, or `Interest::never()` if enabled returns false.
54 /// However, if the subscriber's interest can change dynamically at runtime,
55 /// it may want to override this function to return `Interest::sometimes()`.
56 /// Additionally, subscribers which wish to perform a behaviour once for each
57 /// callsite, such as allocating storage for data related to that callsite,
58 /// can perform it in `register_callsite`.
59 /// - [`clone_span`] is called every time a span ID is cloned, and [`try_close`]
60 /// is called when a span ID is dropped. By default, these functions do
61 /// nothing. However, they can be used to implement reference counting for
62 /// spans, allowing subscribers to free storage for span data and to determine
63 /// when a span has _closed_ permanently (rather than being exited).
64 /// Subscribers which store per-span data or which need to track span closures
65 /// should override these functions together.
66 ///
67 /// [ID]: ../span/struct.Id.html
68 /// [`new_span`]: trait.Subscriber.html#method.new_span
69 /// [`register_callsite`]: trait.Subscriber.html#method.register_callsite
70 /// [`Interest`]: struct.Interest.html
71 /// [`enabled`]: trait.Subscriber.html#method.enabled
72 /// [`clone_span`]: trait.Subscriber.html#method.clone_span
73 /// [`try_close`]: trait.Subscriber.html#method.try_close
74 pub trait Subscriber: 'static {
75 // === Span registry methods ==============================================
76
77 /// Registers a new callsite with this subscriber, returning whether or not
78 /// the subscriber is interested in being notified about the callsite.
79 ///
80 /// By default, this function assumes that the subscriber's [filter]
81 /// represents an unchanging view of its interest in the callsite. However,
82 /// if this is not the case, subscribers may override this function to
83 /// indicate different interests, or to implement behaviour that should run
84 /// once for every callsite.
85 ///
86 /// This function is guaranteed to be called at least once per callsite on
87 /// every active subscriber. The subscriber may store the keys to fields it
88 /// cares about in order to reduce the cost of accessing fields by name,
89 /// preallocate storage for that callsite, or perform any other actions it
90 /// wishes to perform once for each callsite.
91 ///
92 /// The subscriber should then return an [`Interest`], indicating
93 /// whether it is interested in being notified about that callsite in the
94 /// future. This may be `Always` indicating that the subscriber always
95 /// wishes to be notified about the callsite, and its filter need not be
96 /// re-evaluated; `Sometimes`, indicating that the subscriber may sometimes
97 /// care about the callsite but not always (such as when sampling), or
98 /// `Never`, indicating that the subscriber never wishes to be notified about
99 /// that callsite. If all active subscribers return `Never`, a callsite will
100 /// never be enabled unless a new subscriber expresses interest in it.
101 ///
102 /// `Subscriber`s which require their filters to be run every time an event
103 /// occurs or a span is entered/exited should return `Interest::sometimes`.
104 /// If a subscriber returns `Interest::sometimes`, then its [`enabled`] method
105 /// will be called every time an event or span is created from that callsite.
106 ///
107 /// For example, suppose a sampling subscriber is implemented by
108 /// incrementing a counter every time `enabled` is called and only returning
109 /// `true` when the counter is divisible by a specified sampling rate. If
110 /// that subscriber returns `Interest::always` from `register_callsite`, then
111 /// the filter will not be re-evaluated once it has been applied to a given
112 /// set of metadata. Thus, the counter will not be incremented, and the span
113 /// or event that corresponds to the metadata will never be `enabled`.
114 ///
115 /// `Subscriber`s that need to change their filters occasionally should call
116 /// [`rebuild_interest_cache`] to re-evaluate `register_callsite` for all
117 /// callsites.
118 ///
119 /// Similarly, if a `Subscriber` has a filtering strategy that can be
120 /// changed dynamically at runtime, it would need to re-evaluate that filter
121 /// if the cached results have changed.
122 ///
123 /// A subscriber which manages fanout to multiple other subscribers
124 /// should proxy this decision to all of its child subscribers,
125 /// returning `Interest::never` only if _all_ such children return
126 /// `Interest::never`. If the set of subscribers to which spans are
127 /// broadcast may change dynamically, the subscriber should also never
128 /// return `Interest::Never`, as a new subscriber may be added that _is_
129 /// interested.
130 ///
131 /// # Notes
132 /// This function may be called again when a new subscriber is created or
133 /// when the registry is invalidated.
134 ///
135 /// If a subscriber returns `Interest::never` for a particular callsite, it
136 /// _may_ still see spans and events originating from that callsite, if
137 /// another subscriber expressed interest in it.
138 ///
139 /// [filter]: #method.enabled
140 /// [metadata]: ../metadata/struct.Metadata.html
141 /// [`Interest`]: struct.Interest.html
142 /// [`enabled`]: #method.enabled
143 /// [`rebuild_interest_cache`]: ../callsite/fn.rebuild_interest_cache.html
144 fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
145 if self.enabled(metadata) {
146 Interest::always()
147 } else {
148 Interest::never()
149 }
150 }
151
152 /// Returns true if a span or event with the specified [metadata] would be
153 /// recorded.
154 ///
155 /// By default, it is assumed that this filter needs only be evaluated once
156 /// for each callsite, so it is called by [`register_callsite`] when each
157 /// callsite is registered. The result is used to determine if the subscriber
158 /// is always [interested] or never interested in that callsite. This is intended
159 /// primarily as an optimization, so that expensive filters (such as those
160 /// involving string search, et cetera) need not be re-evaluated.
161 ///
162 /// However, if the subscriber's interest in a particular span or event may
163 /// change, or depends on contexts only determined dynamically at runtime,
164 /// then the `register_callsite` method should be overridden to return
165 /// [`Interest::sometimes`]. In that case, this function will be called every
166 /// time that span or event occurs.
167 ///
168 /// [metadata]: ../metadata/struct.Metadata.html
169 /// [interested]: struct.Interest.html
170 /// [`Interest::sometimes`]: struct.Interest.html#method.sometimes
171 /// [`register_callsite`]: #method.register_callsite
172 fn enabled(&self, metadata: &Metadata<'_>) -> bool;
173
174 /// Returns the highest [verbosity level][level] that this `Subscriber` will
175 /// enable, or `None`, if the subscriber does not implement level-based
176 /// filtering or chooses not to implement this method.
177 ///
178 /// If this method returns a [`Level`][level], it will be used as a hint to
179 /// determine the most verbose level that will be enabled. This will allow
180 /// spans and events which are more verbose than that level to be skipped
181 /// more efficiently. Subscribers which perform filtering are strongly
182 /// encouraged to provide an implementation of this method.
183 ///
184 /// If the maximum level the subscriber will enable can change over the
185 /// course of its lifetime, it is free to return a different value from
186 /// multiple invocations of this method. However, note that changes in the
187 /// maximum level will **only** be reflected after the callsite [`Interest`]
188 /// cache is rebuilt, by calling the [`callsite::rebuild_interest_cache`][rebuild]
189 /// function. Therefore, if the subscriber will change the value returned by
190 /// this method, it is responsible for ensuring that
191 /// [`rebuild_interest_cache`][rebuild] is called after the value of the max
192 /// level changes.
193 ///
194 /// [level]: ../struct.Level.html
195 /// [`Interest`]: struct.Interest.html
196 /// [rebuild]: ../callsite/fn.rebuild_interest_cache.html
197 fn max_level_hint(&self) -> Option<LevelFilter> {
198 None
199 }
200
201 /// Visit the construction of a new span, returning a new [span ID] for the
202 /// span being constructed.
203 ///
204 /// The provided [`Attributes`] contains any field values that were provided
205 /// when the span was created. The subscriber may pass a [visitor] to the
206 /// `Attributes`' [`record` method] to record these values.
207 ///
208 /// IDs are used to uniquely identify spans and events within the context of a
209 /// subscriber, so span equality will be based on the returned ID. Thus, if
210 /// the subscriber wishes for all spans with the same metadata to be
211 /// considered equal, it should return the same ID every time it is given a
212 /// particular set of metadata. Similarly, if it wishes for two separate
213 /// instances of a span with the same metadata to *not* be equal, it should
214 /// return a distinct ID every time this function is called, regardless of
215 /// the metadata.
216 ///
217 /// Note that the subscriber is free to assign span IDs based on whatever
218 /// scheme it sees fit. Any guarantees about uniqueness, ordering, or ID
219 /// reuse are left up to the subscriber implementation to determine.
220 ///
221 /// [span ID]: ../span/struct.Id.html
222 /// [`Attributes`]: ../span/struct.Attributes.html
223 /// [visitor]: ../field/trait.Visit.html
224 /// [`record` method]: ../span/struct.Attributes.html#method.record
225 fn new_span(&self, span: &span::Attributes<'_>) -> span::Id;
226
227 // === Notification methods ===============================================
228
229 /// Record a set of values on a span.
230 ///
231 /// This method will be invoked when value is recorded on a span.
232 /// Recording multiple values for the same field is possible,
233 /// but the actual behaviour is defined by the subscriber implementation.
234 ///
235 /// Keep in mind that a span might not provide a value
236 /// for each field it declares.
237 ///
238 /// The subscriber is expected to provide a [visitor] to the `Record`'s
239 /// [`record` method] in order to record the added values.
240 ///
241 /// # Example
242 /// "foo = 3" will be recorded when [`record`] is called on the
243 /// `Attributes` passed to `new_span`.
244 /// Since values are not provided for the `bar` and `baz` fields,
245 /// the span's `Metadata` will indicate that it _has_ those fields,
246 /// but values for them won't be recorded at this time.
247 ///
248 /// ```rust,ignore
249 /// # use tracing::span;
250 ///
251 /// let mut span = span!("my_span", foo = 3, bar, baz);
252 ///
253 /// // `Subscriber::record` will be called with a `Record`
254 /// // containing "bar = false"
255 /// span.record("bar", &false);
256 ///
257 /// // `Subscriber::record` will be called with a `Record`
258 /// // containing "baz = "a string""
259 /// span.record("baz", &"a string");
260 /// ```
261 ///
262 /// [visitor]: ../field/trait.Visit.html
263 /// [`record`]: ../span/struct.Attributes.html#method.record
264 /// [`record` method]: ../span/struct.Record.html#method.record
265 fn record(&self, span: &span::Id, values: &span::Record<'_>);
266
267 /// Adds an indication that `span` follows from the span with the id
268 /// `follows`.
269 ///
270 /// This relationship differs somewhat from the parent-child relationship: a
271 /// span may have any number of prior spans, rather than a single one; and
272 /// spans are not considered to be executing _inside_ of the spans they
273 /// follow from. This means that a span may close even if subsequent spans
274 /// that follow from it are still open, and time spent inside of a
275 /// subsequent span should not be included in the time its precedents were
276 /// executing. This is used to model causal relationships such as when a
277 /// single future spawns several related background tasks, et cetera.
278 ///
279 /// If the subscriber has spans corresponding to the given IDs, it should
280 /// record this relationship in whatever way it deems necessary. Otherwise,
281 /// if one or both of the given span IDs do not correspond to spans that the
282 /// subscriber knows about, or if a cyclical relationship would be created
283 /// (i.e., some span _a_ which proceeds some other span _b_ may not also
284 /// follow from _b_), it may silently do nothing.
285 fn record_follows_from(&self, span: &span::Id, follows: &span::Id);
286
287 /// Records that an [`Event`] has occurred.
288 ///
289 /// This method will be invoked when an Event is constructed by
290 /// the `Event`'s [`dispatch` method]. For example, this happens internally
291 /// when an event macro from `tracing` is called.
292 ///
293 /// The key difference between this method and `record` is that `record` is
294 /// called when a value is recorded for a field defined by a span,
295 /// while `event` is called when a new event occurs.
296 ///
297 /// The provided `Event` struct contains any field values attached to the
298 /// event. The subscriber may pass a [visitor] to the `Event`'s
299 /// [`record` method] to record these values.
300 ///
301 /// [`Event`]: ../event/struct.Event.html
302 /// [visitor]: ../field/trait.Visit.html
303 /// [`record` method]: ../event/struct.Event.html#method.record
304 /// [`dispatch` method]: ../event/struct.Event.html#method.dispatch
305 fn event(&self, event: &Event<'_>);
306
307 /// Records that a span has been entered.
308 ///
309 /// When entering a span, this method is called to notify the subscriber
310 /// that the span has been entered. The subscriber is provided with the
311 /// [span ID] of the entered span, and should update any internal state
312 /// tracking the current span accordingly.
313 ///
314 /// [span ID]: ../span/struct.Id.html
315 fn enter(&self, span: &span::Id);
316
317 /// Records that a span has been exited.
318 ///
319 /// When exiting a span, this method is called to notify the subscriber
320 /// that the span has been exited. The subscriber is provided with the
321 /// [span ID] of the exited span, and should update any internal state
322 /// tracking the current span accordingly.
323 ///
324 /// Exiting a span does not imply that the span will not be re-entered.
325 ///
326 /// [span ID]: ../span/struct.Id.html
327 fn exit(&self, span: &span::Id);
328
329 /// Notifies the subscriber that a [span ID] has been cloned.
330 ///
331 /// This function is guaranteed to only be called with span IDs that were
332 /// returned by this subscriber's `new_span` function.
333 ///
334 /// Note that the default implementation of this function this is just the
335 /// identity function, passing through the identifier. However, it can be
336 /// used in conjunction with [`try_close`] to track the number of handles
337 /// capable of `enter`ing a span. When all the handles have been dropped
338 /// (i.e., `try_close` has been called one more time than `clone_span` for a
339 /// given ID), the subscriber may assume that the span will not be entered
340 /// again. It is then free to deallocate storage for data associated with
341 /// that span, write data from that span to IO, and so on.
342 ///
343 /// For more unsafe situations, however, if `id` is itself a pointer of some
344 /// kind this can be used as a hook to "clone" the pointer, depending on
345 /// what that means for the specified pointer.
346 ///
347 /// [span ID]: ../span/struct.Id.html
348 /// [`try_close`]: trait.Subscriber.html#method.try_close
349 fn clone_span(&self, id: &span::Id) -> span::Id {
350 id.clone()
351 }
352
353 /// **This method is deprecated.**
354 ///
355 /// Using `drop_span` may result in subscribers composed using
356 /// `tracing-subscriber` crate's `Layer` trait from observing close events.
357 /// Use [`try_close`] instead.
358 ///
359 /// The default implementation of this function does nothing.
360 ///
361 /// [`try_close`]: trait.Subscriber.html#method.try_close
362 #[deprecated(since = "0.1.2", note = "use `Subscriber::try_close` instead")]
363 fn drop_span(&self, _id: span::Id) {}
364
365 /// Notifies the subscriber that a [span ID] has been dropped, and returns
366 /// `true` if there are now 0 IDs that refer to that span.
367 ///
368 /// Higher-level libraries providing functionality for composing multiple
369 /// subscriber implementations may use this return value to notify any
370 /// "layered" subscribers that this subscriber considers the span closed.
371 ///
372 /// The default implementation of this method calls the subscriber's
373 /// [`drop_span`] method and returns `false`. This means that, unless the
374 /// subscriber overrides the default implementation, close notifications
375 /// will never be sent to any layered subscribers. In general, if the
376 /// subscriber tracks reference counts, this method should be implemented,
377 /// rather than `drop_span`.
378 ///
379 /// This function is guaranteed to only be called with span IDs that were
380 /// returned by this subscriber's `new_span` function.
381 ///
382 /// It's guaranteed that if this function has been called once more than the
383 /// number of times `clone_span` was called with the same `id`, then no more
384 /// handles that can enter the span with that `id` exist. This means that it
385 /// can be used in conjunction with [`clone_span`] to track the number of
386 /// handles capable of `enter`ing a span. When all the handles have been
387 /// dropped (i.e., `try_close` has been called one more time than
388 /// `clone_span` for a given ID), the subscriber may assume that the span
389 /// will not be entered again, and should return `true`. It is then free to
390 /// deallocate storage for data associated with that span, write data from
391 /// that span to IO, and so on.
392 ///
393 /// **Note**: since this function is called when spans are dropped,
394 /// implementations should ensure that they are unwind-safe. Panicking from
395 /// inside of a `try_close` function may cause a double panic, if the span
396 /// was dropped due to a thread unwinding.
397 ///
398 /// [span ID]: ../span/struct.Id.html
399 /// [`clone_span`]: trait.Subscriber.html#method.clone_span
400 /// [`drop_span`]: trait.Subscriber.html#method.drop_span
401 fn try_close(&self, id: span::Id) -> bool {
402 #[allow(deprecated)]
403 self.drop_span(id);
404 false
405 }
406
407 /// Returns a type representing this subscriber's view of the current span.
408 ///
409 /// If subscribers track a current span, they should override this function
410 /// to return [`Current::new`] if the thread from which this method is
411 /// called is inside a span, or [`Current::none`] if the thread is not
412 /// inside a span.
413 ///
414 /// By default, this returns a value indicating that the subscriber
415 /// does **not** track what span is current. If the subscriber does not
416 /// implement a current span, it should not override this method.
417 ///
418 /// [`Current::new`]: ../span/struct.Current.html#tymethod.new
419 /// [`Current::none`]: ../span/struct.Current.html#tymethod.none
420 fn current_span(&self) -> span::Current {
421 span::Current::unknown()
422 }
423
424 // === Downcasting methods ================================================
425
426 /// If `self` is the same type as the provided `TypeId`, returns an untyped
427 /// `*const` pointer to that type. Otherwise, returns `None`.
428 ///
429 /// If you wish to downcast a `Subscriber`, it is strongly advised to use
430 /// the safe API provided by [`downcast_ref`] instead.
431 ///
432 /// This API is required for `downcast_raw` to be a trait method; a method
433 /// signature like [`downcast_ref`] (with a generic type parameter) is not
434 /// object-safe, and thus cannot be a trait method for `Subscriber`. This
435 /// means that if we only exposed `downcast_ref`, `Subscriber`
436 /// implementations could not override the downcasting behavior
437 ///
438 /// This method may be overridden by "fan out" or "chained" subscriber
439 /// implementations which consist of multiple composed types. Such
440 /// subscribers might allow `downcast_raw` by returning references to those
441 /// component if they contain components with the given `TypeId`.
442 ///
443 /// # Safety
444 ///
445 /// The [`downcast_ref`] method expects that the pointer returned by
446 /// `downcast_raw` is non-null and points to a valid instance of the type
447 /// with the provided `TypeId`. Failure to ensure this will result in
448 /// undefined behaviour, so implementing `downcast_raw` is unsafe.
449 ///
450 /// [`downcast_ref`]: #method.downcast_ref
451 unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> {
452 if id == TypeId::of::<Self>() {
453 Some(self as *const Self as *const ())
454 } else {
455 None
456 }
457 }
458 }
459
460 impl dyn Subscriber {
461 /// Returns `true` if this `Subscriber` is the same type as `T`.
462 pub fn is<T: Any>(&self) -> bool {
463 self.downcast_ref::<T>().is_some()
464 }
465
466 /// Returns some reference to this `Subscriber` value if it is of type `T`,
467 /// or `None` if it isn't.
468 pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
469 unsafe {
470 let raw = self.downcast_raw(TypeId::of::<T>())?;
471 if raw.is_null() {
472 None
473 } else {
474 Some(&*(raw as *const _))
475 }
476 }
477 }
478 }
479
480 /// Indicates a [`Subscriber`]'s interest in a particular callsite.
481 ///
482 /// `Subscriber`s return an `Interest` from their [`register_callsite`] methods
483 /// in order to determine whether that span should be enabled or disabled.
484 ///
485 /// [`Subscriber`]: ../trait.Subscriber.html
486 /// [`register_callsite`]: ../trait.Subscriber.html#method.register_callsite
487 #[derive(Clone, Debug)]
488 pub struct Interest(InterestKind);
489
490 #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
491 enum InterestKind {
492 Never = 0,
493 Sometimes = 1,
494 Always = 2,
495 }
496
497 impl Interest {
498 /// Returns an `Interest` indicating that the subscriber is never interested
499 /// in being notified about a callsite.
500 ///
501 /// If all active subscribers are `never()` interested in a callsite, it will
502 /// be completely disabled unless a new subscriber becomes active.
503 #[inline]
504 pub fn never() -> Self {
505 Interest(InterestKind::Never)
506 }
507
508 /// Returns an `Interest` indicating the subscriber is sometimes interested
509 /// in being notified about a callsite.
510 ///
511 /// If all active subscribers are `sometimes` or `never` interested in a
512 /// callsite, the currently active subscriber will be asked to filter that
513 /// callsite every time it creates a span. This will be the case until a new
514 /// subscriber expresses that it is `always` interested in the callsite.
515 #[inline]
516 pub fn sometimes() -> Self {
517 Interest(InterestKind::Sometimes)
518 }
519
520 /// Returns an `Interest` indicating the subscriber is always interested in
521 /// being notified about a callsite.
522 ///
523 /// If any subscriber expresses that it is `always()` interested in a given
524 /// callsite, then the callsite will always be enabled.
525 #[inline]
526 pub fn always() -> Self {
527 Interest(InterestKind::Always)
528 }
529
530 /// Returns `true` if the subscriber is never interested in being notified
531 /// about this callsite.
532 #[inline]
533 pub fn is_never(&self) -> bool {
534 matches!(self.0, InterestKind::Never)
535 }
536
537 /// Returns `true` if the subscriber is sometimes interested in being notified
538 /// about this callsite.
539 #[inline]
540 pub fn is_sometimes(&self) -> bool {
541 matches!(self.0, InterestKind::Sometimes)
542 }
543
544 /// Returns `true` if the subscriber is always interested in being notified
545 /// about this callsite.
546 #[inline]
547 pub fn is_always(&self) -> bool {
548 matches!(self.0, InterestKind::Always)
549 }
550
551 /// Returns the common interest between these two Interests.
552 ///
553 /// If both interests are the same, this propagates that interest.
554 /// Otherwise, if they differ, the result must always be
555 /// `Interest::sometimes` --- if the two subscribers differ in opinion, we
556 /// will have to ask the current subscriber what it thinks, no matter what.
557 pub(crate) fn and(self, rhs: Interest) -> Self {
558 if self.0 == rhs.0 {
559 self
560 } else {
561 Interest::sometimes()
562 }
563 }
564 }
565
566 /// A no-op [`Subscriber`].
567 ///
568 /// [`NoSubscriber`] implements the [`Subscriber`] trait by never being enabled,
569 /// never being interested in any callsite, and dropping all spans and events.
570 #[derive(Copy, Clone, Debug, Default)]
571 pub struct NoSubscriber(());
572
573 impl Subscriber for NoSubscriber {
574 #[inline]
575 fn register_callsite(&self, _: &'static Metadata<'static>) -> Interest {
576 Interest::never()
577 }
578
579 fn new_span(&self, _: &span::Attributes<'_>) -> span::Id {
580 span::Id::from_u64(0xDEAD)
581 }
582
583 fn event(&self, _event: &Event<'_>) {}
584
585 fn record(&self, _span: &span::Id, _values: &span::Record<'_>) {}
586
587 fn record_follows_from(&self, _span: &span::Id, _follows: &span::Id) {}
588
589 #[inline]
590 fn enabled(&self, _metadata: &Metadata<'_>) -> bool {
591 false
592 }
593
594 fn enter(&self, _span: &span::Id) {}
595 fn exit(&self, _span: &span::Id) {}
596 }
597
598 impl Subscriber for Box<dyn Subscriber + Send + Sync + 'static> {
599 #[inline]
600 fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
601 self.as_ref().register_callsite(metadata)
602 }
603
604 #[inline]
605 fn enabled(&self, metadata: &Metadata<'_>) -> bool {
606 self.as_ref().enabled(metadata)
607 }
608
609 #[inline]
610 fn max_level_hint(&self) -> Option<LevelFilter> {
611 self.as_ref().max_level_hint()
612 }
613
614 #[inline]
615 fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
616 self.as_ref().new_span(span)
617 }
618
619 #[inline]
620 fn record(&self, span: &span::Id, values: &span::Record<'_>) {
621 self.as_ref().record(span, values)
622 }
623
624 #[inline]
625 fn record_follows_from(&self, span: &span::Id, follows: &span::Id) {
626 self.as_ref().record_follows_from(span, follows)
627 }
628
629 #[inline]
630 fn event(&self, event: &Event<'_>) {
631 self.as_ref().event(event)
632 }
633
634 #[inline]
635 fn enter(&self, span: &span::Id) {
636 self.as_ref().enter(span)
637 }
638
639 #[inline]
640 fn exit(&self, span: &span::Id) {
641 self.as_ref().exit(span)
642 }
643
644 #[inline]
645 fn clone_span(&self, id: &span::Id) -> span::Id {
646 self.as_ref().clone_span(id)
647 }
648
649 #[inline]
650 fn try_close(&self, id: span::Id) -> bool {
651 self.as_ref().try_close(id)
652 }
653
654 #[inline]
655 #[allow(deprecated)]
656 fn drop_span(&self, id: span::Id) {
657 self.as_ref().try_close(id);
658 }
659
660 #[inline]
661 fn current_span(&self) -> span::Current {
662 self.as_ref().current_span()
663 }
664
665 #[inline]
666 unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> {
667 if id == TypeId::of::<Self>() {
668 return Some(self as *const Self as *const _);
669 }
670
671 self.as_ref().downcast_raw(id)
672 }
673 }
674
675 impl Subscriber for Arc<dyn Subscriber + Send + Sync + 'static> {
676 #[inline]
677 fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest {
678 self.as_ref().register_callsite(metadata)
679 }
680
681 #[inline]
682 fn enabled(&self, metadata: &Metadata<'_>) -> bool {
683 self.as_ref().enabled(metadata)
684 }
685
686 #[inline]
687 fn max_level_hint(&self) -> Option<LevelFilter> {
688 self.as_ref().max_level_hint()
689 }
690
691 #[inline]
692 fn new_span(&self, span: &span::Attributes<'_>) -> span::Id {
693 self.as_ref().new_span(span)
694 }
695
696 #[inline]
697 fn record(&self, span: &span::Id, values: &span::Record<'_>) {
698 self.as_ref().record(span, values)
699 }
700
701 #[inline]
702 fn record_follows_from(&self, span: &span::Id, follows: &span::Id) {
703 self.as_ref().record_follows_from(span, follows)
704 }
705
706 #[inline]
707 fn event(&self, event: &Event<'_>) {
708 self.as_ref().event(event)
709 }
710
711 #[inline]
712 fn enter(&self, span: &span::Id) {
713 self.as_ref().enter(span)
714 }
715
716 #[inline]
717 fn exit(&self, span: &span::Id) {
718 self.as_ref().exit(span)
719 }
720
721 #[inline]
722 fn clone_span(&self, id: &span::Id) -> span::Id {
723 self.as_ref().clone_span(id)
724 }
725
726 #[inline]
727 fn try_close(&self, id: span::Id) -> bool {
728 self.as_ref().try_close(id)
729 }
730
731 #[inline]
732 #[allow(deprecated)]
733 fn drop_span(&self, id: span::Id) {
734 self.as_ref().try_close(id);
735 }
736
737 #[inline]
738 fn current_span(&self) -> span::Current {
739 self.as_ref().current_span()
740 }
741
742 #[inline]
743 unsafe fn downcast_raw(&self, id: TypeId) -> Option<*const ()> {
744 if id == TypeId::of::<Self>() {
745 return Some(self as *const Self as *const _);
746 }
747
748 self.as_ref().downcast_raw(id)
749 }
750 }