Skip to content

perf: Suppress telemetry using ContextFlags(usize) instead of bool #2861

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions opentelemetry/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,13 @@ thread_local! {
/// assert_eq!(current.get::<ValueB>(), None);
/// ```
#[derive(Clone, Default)]
// 16-byte alignment for performance, which should be ok on 32-bit systems too
#[repr(align(16))]
pub struct Context {
#[cfg(feature = "trace")]
pub(crate) span: Option<Arc<SynchronizedSpan>>,
entries: Option<Arc<EntryMap>>,
suppress_telemetry: bool,
flags: ContextFlags,
}

type EntryMap = HashMap<TypeId, Arc<dyn Any + Sync + Send>, BuildHasherDefault<IdHasher>>;
Expand Down Expand Up @@ -245,7 +247,7 @@ impl Context {
entries,
#[cfg(feature = "trace")]
span: self.span.clone(),
suppress_telemetry: self.suppress_telemetry,
flags: self.flags,
}
}

Expand Down Expand Up @@ -335,7 +337,7 @@ impl Context {
/// Returns whether telemetry is suppressed in this context.
#[inline]
pub fn is_telemetry_suppressed(&self) -> bool {
self.suppress_telemetry
self.flags.is_telemetry_suppressed()
}

/// Returns a new context with telemetry suppression enabled.
Expand All @@ -344,7 +346,7 @@ impl Context {
entries: self.entries.clone(),
#[cfg(feature = "trace")]
span: self.span.clone(),
suppress_telemetry: true,
flags: self.flags.with_telemetry_suppressed(),
}
}

Expand Down Expand Up @@ -413,7 +415,7 @@ impl Context {
Self::map_current(|cx| Context {
span: Some(Arc::new(value)),
entries: cx.entries.clone(),
suppress_telemetry: cx.suppress_telemetry,
flags: cx.flags,
})
}

Expand All @@ -422,7 +424,7 @@ impl Context {
Context {
span: Some(Arc::new(value)),
entries: self.entries.clone(),
suppress_telemetry: self.suppress_telemetry,
flags: self.flags,
}
}
}
Expand All @@ -446,7 +448,7 @@ impl fmt::Debug for Context {
let entries = self.entries.as_ref().map_or(0, |e| e.len());

dbg.field("entries count", &entries)
.field("suppress_telemetry", &self.suppress_telemetry)
.field("suppress_telemetry", &self.flags.is_telemetry_suppressed())
.finish()
}
}
Expand Down Expand Up @@ -605,6 +607,23 @@ impl Default for ContextStack {
}
}

#[derive(Clone, Copy, Default)]
struct ContextFlags(usize);

impl ContextFlags {
const TELEMETRY_SUPPRESSED: usize = 1;

#[inline(always)]
fn is_telemetry_suppressed(&self) -> bool {
self.0 & Self::TELEMETRY_SUPPRESSED != 0
}

#[inline(always)]
fn with_telemetry_suppressed(&self) -> Self {
Self(self.0 | Self::TELEMETRY_SUPPRESSED)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading