mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-19 00:00:09 +02:00
## Motivation To ease the implementation of `Subscriber::register_callsite`, a field should be added to `Metadata` to indicate if this callsite is an event or a span. ## Solution A new struct, `Kind`, is added to the `metadata` module in `tokio-trace-core`, and a `Kind` field is added to the `Metadata` struct. Macros which construct `metadata` now require a `Kind`. `Kind` is represented as a struct with a private inner enum to allow new `Kind`s to be added without breaking changes. However, the _addition_ of the kind field _is_ a breaking change. While this could be done in a backward-compatible way, it would permit the construction of metadata with unknown kinds, and since the next `tokio-trace-core` release will be a breaking change, I opted to make the breaking change instead. New API tests for the `callsite!` and `metadata!` macros have been added to guard against future API breakage. Fixes: #986 Closes: #1008 Co-Authored-By: csmoe <[email protected]>
179 lines
4.2 KiB
Rust
179 lines
4.2 KiB
Rust
#![allow(missing_docs)]
|
|
use super::{field, metadata};
|
|
use std::fmt;
|
|
|
|
/// A mock span.
|
|
///
|
|
/// This is intended for use with the mock subscriber API in the
|
|
/// `subscriber` module.
|
|
#[derive(Debug, Default, Eq, PartialEq)]
|
|
pub struct MockSpan {
|
|
pub(in support) metadata: metadata::Expect,
|
|
}
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
|
pub(in support) enum Parent {
|
|
ContextualRoot,
|
|
Contextual(String),
|
|
ExplicitRoot,
|
|
Explicit(String),
|
|
}
|
|
|
|
#[derive(Debug, Default, Eq, PartialEq)]
|
|
pub struct NewSpan {
|
|
pub(in support) span: MockSpan,
|
|
pub(in support) fields: field::Expect,
|
|
pub(in support) parent: Option<Parent>,
|
|
}
|
|
|
|
pub fn mock() -> MockSpan {
|
|
MockSpan {
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
impl MockSpan {
|
|
pub fn named<I>(self, name: I) -> Self
|
|
where
|
|
I: Into<String>,
|
|
{
|
|
Self {
|
|
metadata: metadata::Expect {
|
|
name: Some(name.into()),
|
|
..self.metadata
|
|
},
|
|
..self
|
|
}
|
|
}
|
|
|
|
pub fn at_level(self, level: tokio_trace::Level) -> Self {
|
|
Self {
|
|
metadata: metadata::Expect {
|
|
level: Some(level),
|
|
..self.metadata
|
|
},
|
|
..self
|
|
}
|
|
}
|
|
|
|
pub fn with_target<I>(self, target: I) -> Self
|
|
where
|
|
I: Into<String>,
|
|
{
|
|
Self {
|
|
metadata: metadata::Expect {
|
|
target: Some(target.into()),
|
|
..self.metadata
|
|
},
|
|
..self
|
|
}
|
|
}
|
|
|
|
pub fn with_explicit_parent(self, parent: Option<&str>) -> NewSpan {
|
|
let parent = match parent {
|
|
Some(name) => Parent::Explicit(name.into()),
|
|
None => Parent::ExplicitRoot,
|
|
};
|
|
NewSpan {
|
|
parent: Some(parent),
|
|
span: self,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
pub fn with_contextual_parent(self, parent: Option<&str>) -> NewSpan {
|
|
let parent = match parent {
|
|
Some(name) => Parent::Contextual(name.into()),
|
|
None => Parent::ContextualRoot,
|
|
};
|
|
NewSpan {
|
|
parent: Some(parent),
|
|
span: self,
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
pub fn name(&self) -> Option<&str> {
|
|
self.metadata.name.as_ref().map(String::as_ref)
|
|
}
|
|
|
|
pub fn with_field<I>(self, fields: I) -> NewSpan
|
|
where
|
|
I: Into<field::Expect>,
|
|
{
|
|
NewSpan {
|
|
span: self,
|
|
fields: fields.into(),
|
|
..Default::default()
|
|
}
|
|
}
|
|
|
|
pub(in support) fn check_metadata(&self, actual: &tokio_trace::Metadata) {
|
|
self.metadata.check(actual, format_args!("span {}", self));
|
|
assert!(actual.is_span(), "expected a span but got {:?}", actual);
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for MockSpan {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
if self.metadata.name.is_some() {
|
|
write!(f, "a span{}", self.metadata)
|
|
} else {
|
|
write!(f, "any span{}", self.metadata)
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Into<NewSpan> for MockSpan {
|
|
fn into(self) -> NewSpan {
|
|
NewSpan {
|
|
span: self,
|
|
..Default::default()
|
|
}
|
|
}
|
|
}
|
|
|
|
impl NewSpan {
|
|
pub fn with_explicit_parent(self, parent: Option<&str>) -> NewSpan {
|
|
let parent = match parent {
|
|
Some(name) => Parent::Explicit(name.into()),
|
|
None => Parent::ExplicitRoot,
|
|
};
|
|
NewSpan {
|
|
parent: Some(parent),
|
|
..self
|
|
}
|
|
}
|
|
|
|
pub fn with_contextual_parent(self, parent: Option<&str>) -> NewSpan {
|
|
let parent = match parent {
|
|
Some(name) => Parent::Contextual(name.into()),
|
|
None => Parent::ContextualRoot,
|
|
};
|
|
NewSpan {
|
|
parent: Some(parent),
|
|
..self
|
|
}
|
|
}
|
|
|
|
pub fn with_field<I>(self, fields: I) -> NewSpan
|
|
where
|
|
I: Into<field::Expect>,
|
|
{
|
|
NewSpan {
|
|
fields: fields.into(),
|
|
..self
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for NewSpan {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(f, "a new span{}", self.span.metadata)?;
|
|
if !self.fields.is_empty() {
|
|
write!(f, " with {}", self.fields)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
}
|