Skip to content

Commit

Permalink
Let Subscribe::on_event hide event from lower layers
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Mar 21, 2022
1 parent 989fb62 commit 22dc571
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
6 changes: 4 additions & 2 deletions tracing-journald/src/lib.rs
Expand Up @@ -39,7 +39,7 @@
)]
#[cfg(unix)]
use std::os::unix::net::UnixDatagram;
use std::{fmt, io, io::Write};
use std::{fmt, io, io::Write, ops::ControlFlow};

use tracing_core::{
event::Event,
Expand Down Expand Up @@ -242,7 +242,7 @@ where
});
}

fn on_event(&self, event: &Event, ctx: Context<C>) {
fn on_event(&self, event: &Event, ctx: Context<C>) -> ControlFlow<()> {
let mut buf = Vec::with_capacity(256);

// Record span fields
Expand All @@ -269,6 +269,8 @@ where

// At this point we can't handle the error anymore so just ignore it.
let _ = self.send_payload(&buf);

ControlFlow::Continue(())
}
}

Expand Down
6 changes: 4 additions & 2 deletions tracing-opentelemetry/src/subscriber.rs
Expand Up @@ -6,7 +6,7 @@ use opentelemetry::{
use std::fmt;
use std::marker;
use std::time::{Instant, SystemTime};
use std::{any::TypeId, ptr::NonNull};
use std::{any::TypeId, ops::ControlFlow, ptr::NonNull};
use tracing_core::span::{self, Attributes, Id, Record};
use tracing_core::{field, Collect, Event};
#[cfg(feature = "tracing-log")]
Expand Down Expand Up @@ -541,7 +541,7 @@ where
/// [`Event`]: opentelemetry::trace::Event
/// [`ERROR`]: tracing::Level::ERROR
/// [`Error`]: opentelemetry::trace::StatusCode::Error
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) {
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) -> ControlFlow<()> {
// Ignore events that are not in the context of a span
if let Some(span) = ctx.lookup_current() {
// Performing read operations before getting a write lock to avoid a deadlock
Expand Down Expand Up @@ -613,6 +613,8 @@ where
}
}
};

ControlFlow::Continue(())
}

/// Exports an OpenTelemetry [`Span`] on close.
Expand Down
8 changes: 5 additions & 3 deletions tracing-subscriber/src/fmt/fmt_subscriber.rs
Expand Up @@ -6,8 +6,8 @@ use crate::{
};
use format::{FmtSpan, TimingDisplay};
use std::{
any::TypeId, cell::RefCell, fmt, io, marker::PhantomData, ops::Deref, ptr::NonNull,
time::Instant,
any::TypeId, cell::RefCell, fmt, io, marker::PhantomData, ops::ControlFlow, ops::Deref,
ptr::NonNull, time::Instant,
};
use tracing_core::{
field,
Expand Down Expand Up @@ -725,7 +725,7 @@ where
}
}

fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) {
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) -> ControlFlow<()> {
thread_local! {
static BUF: RefCell<String> = RefCell::new(String::new());
}
Expand Down Expand Up @@ -761,6 +761,8 @@ where

buf.clear();
});

ControlFlow::Continue(())
}

unsafe fn downcast_raw(&self, id: TypeId) -> Option<NonNull<()>> {
Expand Down
5 changes: 3 additions & 2 deletions tracing-subscriber/src/reload.rs
Expand Up @@ -15,6 +15,7 @@ use crate::sync::RwLock;

use std::{
error, fmt,
ops::ControlFlow,
sync::{Arc, Weak},
};
use tracing_core::{
Expand Down Expand Up @@ -94,8 +95,8 @@ where
}

#[inline]
fn on_event(&self, event: &Event<'_>, ctx: subscribe::Context<'_, C>) {
try_lock!(self.inner.read()).on_event(event, ctx)
fn on_event(&self, event: &Event<'_>, ctx: subscribe::Context<'_, C>) -> ControlFlow<()> {
try_lock!(self.inner.read(), else return ControlFlow::Continue(())).on_event(event, ctx)
}

#[inline]
Expand Down
23 changes: 14 additions & 9 deletions tracing-subscriber/src/subscribe.rs
Expand Up @@ -10,7 +10,7 @@ use tracing_core::{
#[cfg(all(feature = "std", feature = "registry"))]
use crate::registry::Registry;
use crate::registry::{self, LookupSpan, SpanRef};
use core::{any::TypeId, cmp, marker::PhantomData, ptr::NonNull};
use core::{any::TypeId, cmp, marker::PhantomData, ops::ControlFlow, ptr::NonNull};

feature! {
#![feature = "alloc"]
Expand Down Expand Up @@ -316,7 +316,9 @@ where
fn on_follows_from(&self, _span: &span::Id, _follows: &span::Id, _ctx: Context<'_, C>) {}

/// Notifies this subscriber that an event has occurred.
fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, C>) {}
fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, C>) -> ControlFlow<()> {
ControlFlow::Continue(())
}

/// Notifies this subscriber that a span with the given ID was entered.
fn on_enter(&self, _id: &span::Id, _ctx: Context<'_, C>) {}
Expand Down Expand Up @@ -755,9 +757,9 @@ where
}

#[inline]
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) {
self.inner.on_event(event, ctx.clone());
self.subscriber.on_event(event, ctx);
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) -> ControlFlow<()> {
self.subscriber.on_event(event, ctx.clone())?;
self.inner.on_event(event, ctx)
}

#[inline]
Expand Down Expand Up @@ -846,9 +848,11 @@ where
}

#[inline]
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) {
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) -> ControlFlow<()> {
if let Some(ref inner) = self {
inner.on_event(event, ctx);
inner.on_event(event, ctx)
} else {
ControlFlow::Continue(())
}
}

Expand Down Expand Up @@ -927,7 +931,7 @@ feature! {
}

#[inline]
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) {
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, C>) -> ControlFlow<()> {
self.deref().on_event(event, ctx)
}

Expand Down Expand Up @@ -1441,9 +1445,10 @@ pub(crate) mod tests {
where
S: Collect + for<'lookup> LookupSpan<'lookup>,
{
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) -> ControlFlow<()> {
let span = ctx.event_span(event);
*self.last_event_span.lock().unwrap() = span.map(|s| s.name());
ControlFlow::Continue(())
}
}

Expand Down

0 comments on commit 22dc571

Please sign in to comment.