Skip to content
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

debug_annotations: Add support for fully suppressing an event via a field #10

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
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
37 changes: 28 additions & 9 deletions crates/layer/src/debug_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ use tracing_perfetto_sdk_schema::track_event;
use tracing_perfetto_sdk_sys::ffi;

const COUNTER_FIELD_PREFIX: &str = "counter.";
const SUPPRESS_EVENT_FIELD: &str = "perfetto.suppress_event";

#[derive(Default)]
pub struct FFIDebugAnnotations {
counters: Vec<Counter>,
suppress_event: bool,
strings: Vec<ffi::DebugStringAnnotation>,
bools: Vec<ffi::DebugBoolAnnotation>,
ints: Vec<ffi::DebugIntAnnotation>,
Expand All @@ -23,6 +25,7 @@ pub struct FFIDebugAnnotations {
#[derive(Clone, Debug, Default, PartialEq)]
pub struct ProtoDebugAnnotations {
counters: Vec<Counter>,
suppress_event: bool,
annotations: Vec<schema::DebugAnnotation>,
}

Expand All @@ -48,6 +51,10 @@ impl FFIDebugAnnotations {
doubles: self.doubles.as_slice(),
}
}

pub fn suppress_event(&self) -> bool {
self.suppress_event
}
}

impl field::Visit for FFIDebugAnnotations {
Expand Down Expand Up @@ -104,10 +111,14 @@ impl field::Visit for FFIDebugAnnotations {
}

fn record_bool(&mut self, field: &field::Field, value: bool) {
self.bools.push(ffi::DebugBoolAnnotation {
key: field.name(),
value,
});
if field.name() == SUPPRESS_EVENT_FIELD {
self.suppress_event = value;
} else {
self.bools.push(ffi::DebugBoolAnnotation {
key: field.name(),
value,
});
}
}

fn record_str(&mut self, field: &field::Field, value: &str) {
Expand Down Expand Up @@ -141,6 +152,10 @@ impl ProtoDebugAnnotations {
mem::take(&mut self.counters)
}

pub fn suppress_event(&self) -> bool {
self.suppress_event
}

fn name_field(field: &field::Field) -> Option<debug_annotation::NameField> {
Some(debug_annotation::NameField::Name(field.name().to_string()))
}
Expand Down Expand Up @@ -206,11 +221,15 @@ impl field::Visit for ProtoDebugAnnotations {
}

fn record_bool(&mut self, field: &field::Field, value: bool) {
self.annotations.push(schema::DebugAnnotation {
name_field: Self::name_field(field),
value: Some(debug_annotation::Value::BoolValue(value)),
..Default::default()
});
if field.name() == SUPPRESS_EVENT_FIELD {
self.suppress_event = value;
} else {
self.annotations.push(schema::DebugAnnotation {
name_field: Self::name_field(field),
value: Some(debug_annotation::Value::BoolValue(value)),
..Default::default()
});
}
}

fn record_str(&mut self, field: &field::Field, value: &str) {
Expand Down
10 changes: 10 additions & 0 deletions crates/layer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@
//! **NOTE**: at the time of writing, counters are only implemented by the
//! [`NativeLayer`].
//!
//! ## Suppressing events
//!
//! Sometimes, you want to log a tracing event but have it be suppressed in the
//! Perfetto trace in particular. You can set the `perfetto.suppress_event =
//! true` field for that to happen:
//!
//! ```no_run
//! tracing::info!(perfetto.suppress_event=true, "won't be included in the Perfetto trace");
//! ```
//!
//! ## Controlling output destinations
//!
//! For [`NativeLayer`], traces can be written to anything that implements the
Expand Down
6 changes: 4 additions & 2 deletions crates/layer/src/native_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,8 +775,10 @@ where
event.record(&mut debug_annotations);
self.report_counters(meta, debug_annotations.take_counters());

let (track_uuid, sequence_id, _) = self.pick_trace_track_sequence();
self.report_event(meta, debug_annotations, track_uuid, sequence_id);
if !debug_annotations.suppress_event() {
let (track_uuid, sequence_id, _) = self.pick_trace_track_sequence();
self.report_event(meta, debug_annotations, track_uuid, sequence_id);
}
}

fn on_enter(&self, id: &span::Id, ctx: layer::Context<'_, S>) {
Expand Down
20 changes: 11 additions & 9 deletions crates/layer/src/sdk_layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,17 @@ where
let mut debug_annotations = debug_annotations::FFIDebugAnnotations::default();
event.record(&mut debug_annotations);

let meta = event.metadata();
let (track_uuid, _) = self.pick_trace_track();
ffi::trace_track_event_instant(
track_uuid.as_raw(),
meta.name(),
meta.file().unwrap_or_default(),
meta.line().unwrap_or_default(),
&debug_annotations.as_ffi(),
);
if !debug_annotations.suppress_event() {
let meta = event.metadata();
let (track_uuid, _) = self.pick_trace_track();
ffi::trace_track_event_instant(
track_uuid.as_raw(),
meta.name(),
meta.file().unwrap_or_default(),
meta.line().unwrap_or_default(),
&debug_annotations.as_ffi(),
);
}
}

fn on_enter(&self, id: &span::Id, ctx: layer::Context<'_, S>) {
Expand Down