Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Implement tracing::Event handling & parent_id for spans and events #6672

Merged
28 commits merged into from
Aug 12, 2020
Merged
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
e43faeb
implement events handling, implement parent_id for spans & events
mattrutherford Jul 16, 2020
5d0d25a
add events to sp_io::storage
mattrutherford Jul 16, 2020
0b7f4f8
update test
mattrutherford Jul 16, 2020
f7981fc
add tests
mattrutherford Jul 16, 2020
f892b57
adjust limit
mattrutherford Jul 16, 2020
159ac53
Merge remote-tracking branch 'origin/master' into tracing-events
gnunicorn Jul 22, 2020
ffb8713
let tracing crate handle parent_ids
gnunicorn Jul 22, 2020
cc3e5e3
re-enable current-id tracking
gnunicorn Jul 22, 2020
89a9b73
Merge branch 'master' into tracing-events
mattrutherford Jul 22, 2020
38b412c
Merge branch 'tracing-events' of github.com:paritytech/substrate into…
mattrutherford Jul 22, 2020
1855a9a
add test for threads with CurrentSpan
mattrutherford Jul 22, 2020
2081e5d
fix log level
mattrutherford Jul 22, 2020
8da1d7a
remove redundant check for non wasm traces
mattrutherford Jul 22, 2020
64d9863
remove duplicate definition in test
mattrutherford Jul 22, 2020
4dfc683
Adding conditional events API
gnunicorn Jul 23, 2020
c4bac2d
prefer explicit parent_id over current,
mattrutherford Jul 23, 2020
c6d3b5c
Merge branch 'tracing-events' of github.com:paritytech/substrate into…
mattrutherford Jul 23, 2020
f4ee909
limit changes to client::tracing event implementation
mattrutherford Jul 23, 2020
0e14ca5
remove From impl due to fallback required on parent_id
mattrutherford Jul 23, 2020
0b7b16e
Merge branch 'master' into tracing-events
mattrutherford Jul 23, 2020
b8a105d
Merge branch 'master' into tracing-events
mattrutherford Aug 3, 2020
912b4d5
implement SPAN_LIMIT
mattrutherford Aug 3, 2020
7fa7f26
change version of tracing-core
mattrutherford Aug 3, 2020
b42464b
Merge branch 'master' into tracing-events
mattrutherford Aug 10, 2020
9e42b31
update dependancies
mattrutherford Aug 10, 2020
8f16d7b
revert limit
mattrutherford Aug 11, 2020
4717247
remove duplicate dependency
mattrutherford Aug 11, 2020
9dc17e3
Apply suggestions from code review
mattrutherford Aug 12, 2020
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
84 changes: 47 additions & 37 deletions client/tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,31 @@ pub struct TraceEvent {
pub name: &'static str,
pub target: String,
pub level: Level,
pub visitor: Values,
pub parent_id: Option<u64>,
pub values: Values,
pub parent_id: Option<Id>,
}

impl From<&Event<'_>> for TraceEvent {
fn from(event: &Event<'_>) -> TraceEvent {
let mut values = Values::new();
event.record(&mut values);
TraceEvent {
name: event.metadata().name(),
target: event.metadata().target().to_owned(),
level: event.metadata().level().clone(),
parent_id: event.parent().cloned(),
values,
}
}
}

/// Represents a single instance of a tracing span
#[derive(Debug)]
pub struct SpanDatum {
/// id for this span
pub id: u64,
pub id: Id,
/// id of the parent span, if any
pub parent_id: Option<u64>,
pub parent_id: Option<Id>,
/// Name of this span
pub name: String,
/// Target, typically module
Expand All @@ -101,8 +115,6 @@ pub struct SpanDatum {
pub overall_time: Duration,
/// Values recorded to this span
pub values: Values,
/// Events belonging directly to this span
pub events: Vec<TraceEvent>,
}

/// Holds associated values for a tracing span
Expand Down Expand Up @@ -214,7 +226,7 @@ pub struct ProfilingSubscriber {
next_id: AtomicU64,
targets: Vec<(String, Level)>,
trace_handler: Box<dyn TraceHandler>,
span_data: Mutex<FxHashMap<u64, SpanDatum>>,
span_data: Mutex<FxHashMap<Id, SpanDatum>>,
current_span: CurrentSpan,
}

Expand Down Expand Up @@ -290,49 +302,48 @@ impl Subscriber for ProfilingSubscriber {
}

fn new_span(&self, attrs: &Attributes<'_>) -> Id {
let id = self.next_id.fetch_add(1, Ordering::Relaxed);
let id = Id::from_u64(self.next_id.fetch_add(1, Ordering::Relaxed));
let mut values = Values::default();
attrs.record(&mut values);
// If this is a wasm trace, check if target/level is enabled
if let Some(wasm_target) = values.string_values.get(WASM_TARGET_KEY) {
if !self.check_target(wasm_target, attrs.metadata().level()) {
return Id::from_u64(id);
return id
}
}
let span_datum = SpanDatum {
id,
parent_id: self.current_span.id().map(|p| p.into_u64()),
id: id.clone(),
parent_id: self.current_span.id(),
name: attrs.metadata().name().to_owned(),
target: attrs.metadata().target().to_owned(),
level: attrs.metadata().level().clone(),
line: attrs.metadata().line().unwrap_or(0),
start_time: Instant::now(),
overall_time: ZERO_DURATION,
values,
events: Vec::new(),
};
self.span_data.lock().insert(id, span_datum);
Id::from_u64(id)
self.span_data.lock().insert(id.clone(), span_datum);
id
}

fn record(&self, span: &Id, values: &Record<'_>) {
let mut span_data = self.span_data.lock();
if let Some(s) = span_data.get_mut(&span.into_u64()) {
if let Some(s) = span_data.get_mut(span) {
values.record(&mut s.values);
}
}

fn record_follows_from(&self, _span: &Id, _follows: &Id) {}

fn event(&self, event: &Event<'_>) {
let mut visitor = Values::default();
event.record(&mut visitor);
let mut values = Values::default();
event.record(&mut values);
let trace_event = TraceEvent {
name: event.metadata().name(),
target: event.metadata().target().to_owned(),
level: event.metadata().level().clone(),
visitor,
parent_id: self.current_span.id().map(|id| id.into_u64()),
values,
parent_id: self.current_span.id(),
};
self.trace_handler.handle_event(trace_event);
}
Expand All @@ -341,7 +352,7 @@ impl Subscriber for ProfilingSubscriber {
self.current_span.enter(span.clone());
let mut span_data = self.span_data.lock();
let start_time = Instant::now();
if let Some(mut s) = span_data.get_mut(&span.into_u64()) {
if let Some(mut s) = span_data.get_mut(&span) {
s.start_time = start_time;
}
}
Expand All @@ -350,15 +361,15 @@ impl Subscriber for ProfilingSubscriber {
self.current_span.exit();
mattrutherford marked this conversation as resolved.
Show resolved Hide resolved
let end_time = Instant::now();
let mut span_data = self.span_data.lock();
if let Some(mut s) = span_data.get_mut(&span.into_u64()) {
if let Some(mut s) = span_data.get_mut(&span) {
s.overall_time = end_time - s.start_time + s.overall_time;
}
mattrutherford marked this conversation as resolved.
Show resolved Hide resolved
}

fn try_close(&self, span: Id) -> bool {
let span_datum = {
let mut span_data = self.span_data.lock();
span_data.remove(&span.into_u64())
span_data.remove(&span)
};
if let Some(mut span_datum) = span_datum {
if span_datum.name == WASM_TRACE_IDENTIFIER {
Expand Down Expand Up @@ -396,25 +407,23 @@ impl TraceHandler for LogTraceHandler {
if span_datum.values.is_empty() {
log::log!(
log_level(span_datum.level),
"{}: {}, time: {}, id: {}, parent_id: {:?}, events: {:?}",
"{}: {}, time: {}, id: {}, parent_id: {:?}",
span_datum.target,
span_datum.name,
span_datum.overall_time.as_nanos(),
span_datum.id,
span_datum.parent_id,
span_datum.events,
span_datum.id.into_u64(),
span_datum.parent_id.map(|s| s.into_u64()),
);
} else {
log::log!(
log_level(span_datum.level),
"{}: {}, time: {}, id: {}, parent_id: {:?}, values: {}, events: {:?}",
"{}: {}, time: {}, id: {}, parent_id: {:?}, values: {}",
span_datum.target,
span_datum.name,
span_datum.overall_time.as_nanos(),
span_datum.id,
span_datum.parent_id,
span_datum.id.into_u64(),
span_datum.parent_id.map(|s| s.into_u64()),
span_datum.values,
span_datum.events,
);
}
}
Expand All @@ -425,8 +434,8 @@ impl TraceHandler for LogTraceHandler {
"{}: {}, parent_id: {:?}, values: {}",
event.name,
event.target,
event.parent_id,
event.visitor
event.parent_id.map(|s| s.into_u64()),
event.values
mattrutherford marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
Expand All @@ -442,8 +451,8 @@ impl TraceHandler for TelemetryTraceHandler {
"name" => span_datum.name,
"target" => span_datum.target,
"time" => span_datum.overall_time.as_nanos(),
"id" => span_datum.id,
"parent_id" => span_datum.parent_id,
"id" => span_datum.id.into_u64(),
"parent_id" => span_datum.parent_id.map(|i| i.into_u64()),
"values" => span_datum.values
);
}
Expand All @@ -452,8 +461,8 @@ impl TraceHandler for TelemetryTraceHandler {
telemetry!(SUBSTRATE_INFO; "tracing.event";
"name" => event.name,
"target" => event.target,
"parent_id" => event.parent_id,
"values" => event.visitor
"parent_id" => event.parent_id.map(|i| i.into_u64()),
"values" => event.values
);
}
}
Expand Down Expand Up @@ -565,7 +574,7 @@ mod tests {
let _sub_guard = tracing::subscriber::set_default(sub);
tracing::event!(target: "test_target", tracing::Level::INFO, "test_event");
let mut te1 = events.lock().remove(0);
assert_eq!(te1.visitor.string_values.remove(&"message".to_owned()).unwrap(), "test_event".to_owned());
assert_eq!(te1.values.string_values.remove(&"message".to_owned()).unwrap(), "test_event".to_owned());
}

#[test]
Expand All @@ -589,4 +598,5 @@ mod tests {

assert_eq!(sd1.id, te1.parent_id.unwrap());
}

}