mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-07 00:00:08 +02:00
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:
@@ -1,6 +1,6 @@
|
||||
use tokio_trace::{
|
||||
callsite::Callsite,
|
||||
field::{self, Field, Record, Value},
|
||||
field::{self, Field, Value, Visit},
|
||||
};
|
||||
|
||||
use std::{collections::HashMap, fmt};
|
||||
@@ -106,8 +106,8 @@ impl Expect {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn checker<'a>(&'a mut self, ctx: String) -> CheckRecorder<'a> {
|
||||
CheckRecorder { expect: self, ctx }
|
||||
pub fn checker<'a>(&'a mut self, ctx: String) -> CheckVisitor<'a> {
|
||||
CheckVisitor { expect: self, ctx }
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
@@ -128,12 +128,12 @@ impl fmt::Display for MockValue {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct CheckRecorder<'a> {
|
||||
pub struct CheckVisitor<'a> {
|
||||
expect: &'a mut Expect,
|
||||
ctx: String,
|
||||
}
|
||||
|
||||
impl<'a> Record for CheckRecorder<'a> {
|
||||
impl<'a> Visit for CheckVisitor<'a> {
|
||||
fn record_i64(&mut self, field: &Field, value: i64) {
|
||||
self.expect
|
||||
.compare_or_panic(field.name(), &value, &self.ctx[..])
|
||||
@@ -160,7 +160,7 @@ impl<'a> Record for CheckRecorder<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> CheckRecorder<'a> {
|
||||
impl<'a> CheckVisitor<'a> {
|
||||
pub fn finish(self) {
|
||||
assert!(
|
||||
self.expect.fields.is_empty(),
|
||||
@@ -177,7 +177,7 @@ impl<'a> From<&'a Value> for MockValue {
|
||||
value: Option<MockValue>,
|
||||
}
|
||||
|
||||
impl Record for MockValueBuilder {
|
||||
impl Visit for MockValueBuilder {
|
||||
fn record_i64(&mut self, _: &Field, value: i64) {
|
||||
self.value = Some(MockValue::I64(value));
|
||||
}
|
||||
|
||||
@@ -13,8 +13,7 @@ use std::{
|
||||
},
|
||||
};
|
||||
use tokio_trace::{
|
||||
field,
|
||||
span::{Attributes, Id},
|
||||
span::{self, Attributes, Id},
|
||||
Event, Metadata, Subscriber,
|
||||
};
|
||||
|
||||
@@ -25,7 +24,7 @@ enum Expect {
|
||||
Exit(MockSpan),
|
||||
CloneSpan(MockSpan),
|
||||
DropSpan(MockSpan),
|
||||
Record(MockSpan, mock_field::Expect),
|
||||
Visit(MockSpan, mock_field::Expect),
|
||||
NewSpan(NewSpan),
|
||||
Nothing,
|
||||
}
|
||||
@@ -95,7 +94,7 @@ where
|
||||
where
|
||||
I: Into<mock_field::Expect>,
|
||||
{
|
||||
self.expected.push_back(Expect::Record(span, fields.into()));
|
||||
self.expected.push_back(Expect::Visit(span, fields.into()));
|
||||
self
|
||||
}
|
||||
|
||||
@@ -144,21 +143,20 @@ where
|
||||
(self.filter)(meta)
|
||||
}
|
||||
|
||||
fn record(&self, id: &Id, values: &field::ValueSet) {
|
||||
fn record(&self, id: &Id, values: &span::Record) {
|
||||
let spans = self.spans.lock().unwrap();
|
||||
let mut expected = self.expected.lock().unwrap();
|
||||
let span = spans
|
||||
.get(id)
|
||||
.unwrap_or_else(|| panic!("no span for ID {:?}", id));
|
||||
println!("record: {}; id={:?}; values={:?};", span.name, id, values);
|
||||
let was_expected = if let Some(Expect::Record(_, _)) = expected.front() {
|
||||
let was_expected = if let Some(Expect::Visit(_, _)) = expected.front() {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if was_expected {
|
||||
if let Expect::Record(expected_span, mut expected_values) =
|
||||
expected.pop_front().unwrap()
|
||||
if let Expect::Visit(expected_span, mut expected_values) = expected.pop_front().unwrap()
|
||||
{
|
||||
if let Some(name) = expected_span.name() {
|
||||
assert_eq!(name, span.name);
|
||||
@@ -210,7 +208,7 @@ where
|
||||
.metadata
|
||||
.check(meta, format_args!("span `{}`", name));
|
||||
let mut checker = expected.fields.checker(format!("{}", name));
|
||||
values.record(&mut checker);
|
||||
span.record(&mut checker);
|
||||
checker.finish();
|
||||
match expected.parent {
|
||||
Some(Parent::ExplicitRoot) => {
|
||||
@@ -390,7 +388,7 @@ impl Expect {
|
||||
Expect::Exit(e) => panic!("expected to exit {} but {} instead", e, what,),
|
||||
Expect::CloneSpan(e) => panic!("expected to clone {} but {} instead", e, what,),
|
||||
Expect::DropSpan(e) => panic!("expected to drop {} but {} instead", e, what,),
|
||||
Expect::Record(e, fields) => {
|
||||
Expect::Visit(e, fields) => {
|
||||
panic!("expected {} to record {} but {} instead", e, fields, what,)
|
||||
}
|
||||
Expect::NewSpan(e) => panic!("expected {} but {} instead", e, what),
|
||||
|
||||
Reference in New Issue
Block a user