trace: Add arguments struct to subscriber::Record (#955)

This branch changes the `Subscriber::record` method to take a new
arguments struct, `span::Record`. The `field::Record` trait was renamed
to `field::Visit` to prevent name conflicts.

In addition, the `ValueSet::is_empty`, `ValueSet::contains`, and
`ValueSet::record` methods were made crate-private, as they are exposed
on the `Attributes` and `Record` types. 

Signed-off-by: Eliza Weisman <[email protected]>
This commit is contained in:
Eliza Weisman
2019-03-07 12:41:10 -08:00
committed by GitHub
parent 6fbef0a528
commit d88aba8d1c
12 changed files with 186 additions and 143 deletions
+7 -7
View File
@@ -2,7 +2,7 @@
extern crate tokio_trace;
use tokio_trace::{
field::{self, Field, Record},
field::{self, Field, Visit},
span,
subscriber::{self, Subscriber},
Event, Id, Metadata,
@@ -29,7 +29,7 @@ struct Count<'a> {
counters: RwLockReadGuard<'a, HashMap<String, AtomicUsize>>,
}
impl<'a> Record for Count<'a> {
impl<'a> Visit for Count<'a> {
fn record_i64(&mut self, field: &Field, value: i64) {
if let Some(counter) = self.counters.get(field.name()) {
if value > 0 {
@@ -52,7 +52,7 @@ impl<'a> Record for Count<'a> {
}
impl CounterSubscriber {
fn recorder(&self) -> Count {
fn visitor(&self) -> Count {
Count {
counters: self.counters.0.read().unwrap(),
}
@@ -78,7 +78,7 @@ impl Subscriber for CounterSubscriber {
}
fn new_span(&self, new_span: &span::Attributes) -> Id {
new_span.values().record(&mut self.recorder());
new_span.record(&mut self.visitor());
let id = self.ids.fetch_add(1, Ordering::SeqCst);
Id::from_u64(id as u64)
}
@@ -87,12 +87,12 @@ impl Subscriber for CounterSubscriber {
// unimplemented
}
fn record(&self, _: &Id, values: &field::ValueSet) {
values.record(&mut self.recorder())
fn record(&self, _: &Id, values: &span::Record) {
values.record(&mut self.visitor())
}
fn event(&self, event: &Event) {
event.record(&mut self.recorder())
event.record(&mut self.visitor())
}
fn enabled(&self, metadata: &Metadata) -> bool {
@@ -15,7 +15,7 @@ extern crate humantime;
use self::ansi_term::{Color, Style};
use super::tokio_trace::{
self,
field::{Field, Record},
field::{Field, Visit},
Id, Level, Subscriber,
};
@@ -103,27 +103,23 @@ impl<'a> fmt::Display for ColorLevel<'a> {
}
impl Span {
fn new(
parent: Option<Id>,
_meta: &tokio_trace::Metadata,
values: &tokio_trace::field::ValueSet,
) -> Self {
fn new(parent: Option<Id>, attrs: &tokio_trace::span::Attributes) -> Self {
let mut span = Self {
parent,
kvs: Vec::new(),
};
values.record(&mut span);
attrs.record(&mut span);
span
}
}
impl Record for Span {
impl Visit for Span {
fn record_debug(&mut self, field: &Field, value: &fmt::Debug) {
self.kvs.push((field.name(), format!("{:?}", value)))
}
}
impl<'a> Record for Event<'a> {
impl<'a> Visit for Event<'a> {
fn record_debug(&mut self, field: &Field, value: &fmt::Debug) {
write!(
&mut self.stderr,
@@ -207,16 +203,14 @@ impl Subscriber for SloggishSubscriber {
}
fn new_span(&self, span: &tokio_trace::span::Attributes) -> tokio_trace::Id {
let meta = span.metadata();
let values = span.values();
let next = self.ids.fetch_add(1, Ordering::SeqCst) as u64;
let id = tokio_trace::Id::from_u64(next);
let span = Span::new(self.current.id(), meta, values);
let span = Span::new(self.current.id(), span);
self.spans.lock().unwrap().insert(id.clone(), span);
id
}
fn record(&self, span: &tokio_trace::Id, values: &tokio_trace::field::ValueSet) {
fn record(&self, span: &tokio_trace::Id, values: &tokio_trace::span::Record) {
let mut spans = self.spans.lock().expect("mutex poisoned!");
if let Some(span) = spans.get_mut(span) {
values.record(span);
@@ -271,12 +265,12 @@ impl Subscriber for SloggishSubscriber {
target = &event.metadata().target(),
)
.unwrap();
let mut recorder = Event {
let mut visitor = Event {
stderr,
comma: false,
};
event.record(&mut recorder);
write!(&mut recorder.stderr, "\n").unwrap();
event.record(&mut visitor);
write!(&mut visitor.stderr, "\n").unwrap();
}
#[inline]