Skip to content

Commit

Permalink
core: add {Subscriber,Layer}::on_register_dispatch (#2269)
Browse files Browse the repository at this point in the history
The `on_register_dispatch` method is invoked when a `Subscriber` is
registered as a `Dispatch`. This method should be overridden to
perform actions upon the installation of a subscriber/layer;
for instance, to send a copy of the subscriber's `Dispatch` to a
worker thread.
  • Loading branch information
jswrenn authored and hawkw committed Sep 19, 2022
1 parent 6921d10 commit 14cfe55
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 14 deletions.
1 change: 1 addition & 0 deletions tracing-core/src/callsite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,7 @@ impl Callsites {

pub(crate) fn register_dispatch(dispatch: &Dispatch) {
let dispatchers = DISPATCHERS.register_dispatch(dispatch);
dispatch.subscriber().on_register_dispatch(dispatch);
CALLSITES.rebuild_interest(dispatchers);
}

Expand Down
19 changes: 17 additions & 2 deletions tracing-core/src/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,23 @@ impl Dispatch {
Registrar(Arc::downgrade(&self.subscriber))
}

/// Registers a new callsite with this subscriber, returning whether or not
/// the subscriber is interested in being notified about the callsite.
#[inline(always)]
#[cfg(feature = "alloc")]
pub(crate) fn subscriber(&self) -> &(dyn Subscriber + Send + Sync) {
match self.subscriber {
Kind::Scoped(ref s) => Arc::deref(s),
Kind::Global(s) => s,
}
}

#[inline(always)]
#[cfg(not(feature = "alloc"))]
pub(crate) fn subscriber(&self) -> &(dyn Subscriber + Send + Sync) {
&self.subscriber
}

/// Registers a new callsite with this collector, returning whether or not
/// the collector is interested in being notified about the callsite.
///
/// This calls the [`register_callsite`] function on the [`Subscriber`]
/// that this `Dispatch` forwards to.
Expand Down
9 changes: 6 additions & 3 deletions tracing-core/src/subscriber.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Subscribers collect and record trace data.
use crate::{span, Event, LevelFilter, Metadata};
//! Collectors collect and record trace data.
use crate::{span, Dispatch, Event, LevelFilter, Metadata};

use crate::stdlib::{
any::{Any, TypeId},
Expand Down Expand Up @@ -81,7 +81,10 @@ use crate::stdlib::{
/// [`event`]: Subscriber::event
/// [`event_enabled`]: Subscriber::event_enabled
pub trait Subscriber: 'static {
// === Span registry methods ==============================================
/// Invoked when this subscriber becomes a [`Dispatch`].
fn on_register_dispatch(&self, subscriber: &Dispatch) {
let _ = subscriber;
}

/// Registers a new [callsite] with this subscriber, returning whether or not
/// the subscriber is interested in being notified about the callsite.
Expand Down
6 changes: 5 additions & 1 deletion tracing-subscriber/src/filter/layer_filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use std::{
use tracing_core::{
span,
subscriber::{Interest, Subscriber},
Event, Metadata,
Dispatch, Event, Metadata,
};
pub mod combinator;

Expand Down Expand Up @@ -607,6 +607,10 @@ where
F: layer::Filter<S> + 'static,
L: Layer<S>,
{
fn on_register_dispatch(&self, collector: &Dispatch) {
self.layer.on_register_dispatch(collector);
}

fn on_layer(&mut self, subscriber: &mut S) {
self.id = MagicPlfDowncastMarker(subscriber.register_filter());
self.layer.on_layer(subscriber);
Expand Down
12 changes: 6 additions & 6 deletions tracing-subscriber/src/layer/layered.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use tracing_core::{
metadata::Metadata,
span,
subscriber::{Interest, Subscriber},
Event, LevelFilter,
};
use tracing_core::{metadata::Metadata, span, Dispatch, Event, Interest, LevelFilter, Subscriber};

use crate::{
filter,
Expand Down Expand Up @@ -245,6 +240,11 @@ where
B: Layer<S>,
S: Subscriber,
{
fn on_register_dispatch(&self, subscriber: &Dispatch) {
self.layer.on_register_dispatch(subscriber);
self.inner.on_register_dispatch(subscriber);
}

fn on_layer(&mut self, subscriber: &mut S) {
self.layer.on_layer(subscriber);
self.inner.on_layer(subscriber);
Expand Down
15 changes: 14 additions & 1 deletion tracing-subscriber/src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ use tracing_core::{
metadata::Metadata,
span,
subscriber::{Interest, Subscriber},
Event, LevelFilter,
Dispatch, Event, LevelFilter,
};

use core::any::TypeId;
Expand Down Expand Up @@ -711,6 +711,14 @@ where
S: Subscriber,
Self: 'static,
{
/// Performs late initialization when installing this layer as a
/// [subscriber].
///
/// [subscriber]: tracing_core::Subscriber
fn on_register_dispatch(&self, subscriber: &Dispatch) {
let _ = subscriber;
}

/// Performs late initialization when attaching a `Layer` to a
/// [`Subscriber`].
///
Expand Down Expand Up @@ -1591,6 +1599,11 @@ feature! {

macro_rules! layer_impl_body {
() => {
#[inline]
fn on_register_dispatch(&self, subscriber: &Dispatch) {
self.deref().on_register_dispatch(subscriber);
}

#[inline]
fn on_layer(&mut self, subscriber: &mut S) {
self.deref_mut().on_layer(subscriber);
Expand Down
6 changes: 5 additions & 1 deletion tracing-subscriber/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ use std::{
use tracing_core::{
callsite, span,
subscriber::{Interest, Subscriber},
Event, LevelFilter, Metadata,
Dispatch, Event, LevelFilter, Metadata,
};

/// Wraps a `Layer` or `Filter`, allowing it to be reloaded dynamically at runtime.
Expand Down Expand Up @@ -115,6 +115,10 @@ where
L: crate::Layer<S> + 'static,
S: Subscriber,
{
fn on_register_dispatch(&self, subscriber: &Dispatch) {
try_lock!(self.inner.read()).on_register_dispatch(subscriber);
}

fn on_layer(&mut self, subscriber: &mut S) {
try_lock!(self.inner.write(), else return).on_layer(subscriber);
}
Expand Down

0 comments on commit 14cfe55

Please sign in to comment.