|
|
@ -11,6 +11,8 @@ use tracing_subscriber::{ |
|
|
|
|
|
|
|
|
|
|
|
#[derive(Clone, Debug)] |
|
|
|
#[derive(Clone, Debug)] |
|
|
|
pub struct Filters { |
|
|
|
pub struct Filters { |
|
|
|
|
|
|
|
filter_target: Option<String>, |
|
|
|
|
|
|
|
filter_name: Option<String>, |
|
|
|
filter_message: Option<String>, |
|
|
|
filter_message: Option<String>, |
|
|
|
filter_field: Option<String>, |
|
|
|
filter_field: Option<String>, |
|
|
|
filter_field_value: Option<String>, |
|
|
|
filter_field_value: Option<String>, |
|
|
@ -19,27 +21,53 @@ pub struct Filters { |
|
|
|
impl Filters { |
|
|
|
impl Filters { |
|
|
|
pub fn new() -> Self { |
|
|
|
pub fn new() -> Self { |
|
|
|
Self { |
|
|
|
Self { |
|
|
|
|
|
|
|
filter_target: None, |
|
|
|
|
|
|
|
filter_name: None, |
|
|
|
filter_message: None, |
|
|
|
filter_message: None, |
|
|
|
filter_field: None, |
|
|
|
filter_field: None, |
|
|
|
filter_field_value: None, |
|
|
|
filter_field_value: None, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn filter_message(mut self, message: impl ToString) -> Self { |
|
|
|
pub fn target(mut self, target: impl ToString) -> Self { |
|
|
|
|
|
|
|
self.filter_target = Some(target.to_string()); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn name(mut self, name: impl ToString) -> Self { |
|
|
|
|
|
|
|
self.filter_name = Some(name.to_string()); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn message(mut self, message: impl ToString) -> Self { |
|
|
|
self.filter_message = Some(message.to_string()); |
|
|
|
self.filter_message = Some(message.to_string()); |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn filter_field(mut self, field: impl ToString) -> Self { |
|
|
|
pub fn field(mut self, field: impl ToString) -> Self { |
|
|
|
self.filter_field = Some(field.to_string()); |
|
|
|
self.filter_field = Some(field.to_string()); |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn filter_field_value(mut self, field: impl ToString, value: impl ToString) -> Self { |
|
|
|
pub fn field_value(mut self, field: impl ToString, value: impl ToString) -> Self { |
|
|
|
self.filter_field = Some(field.to_string()); |
|
|
|
self.filter_field = Some(field.to_string()); |
|
|
|
self.filter_field_value = Some(value.to_string()); |
|
|
|
self.filter_field_value = Some(value.to_string()); |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn matches_metadata(&self, metadata: &tracing::Metadata) -> bool { |
|
|
|
|
|
|
|
if let Some(target) = &self.filter_target { |
|
|
|
|
|
|
|
if metadata.target() != target { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if let Some(name) = &self.filter_name { |
|
|
|
|
|
|
|
if metadata.name() != name { |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
true |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct TraceListenerVisitor<'a> { |
|
|
|
struct TraceListenerVisitor<'a> { |
|
|
@ -104,6 +132,10 @@ impl EventListener { |
|
|
|
} |
|
|
|
} |
|
|
|
impl<S: Subscriber> Layer<S> for EventListener { |
|
|
|
impl<S: Subscriber> Layer<S> for EventListener { |
|
|
|
fn on_event(&self, ev: &tracing::Event, _: Context<S>) { |
|
|
|
fn on_event(&self, ev: &tracing::Event, _: Context<S>) { |
|
|
|
|
|
|
|
println!("event name: {}", ev.metadata().name()); |
|
|
|
|
|
|
|
if !self.filters.matches_metadata(ev.metadata()) { |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
let mut visit = TraceListenerVisitor::new(&self.filters); |
|
|
|
let mut visit = TraceListenerVisitor::new(&self.filters); |
|
|
|
ev.record(&mut visit); |
|
|
|
ev.record(&mut visit); |
|
|
|
if visit.did_match() { |
|
|
|
if visit.did_match() { |
|
|
@ -115,19 +147,36 @@ impl<S: Subscriber> Layer<S> for EventListener { |
|
|
|
#[derive(Clone, Debug)] |
|
|
|
#[derive(Clone, Debug)] |
|
|
|
pub struct SpanFilters { |
|
|
|
pub struct SpanFilters { |
|
|
|
filters: Filters, |
|
|
|
filters: Filters, |
|
|
|
filter_name: Option<String>, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl SpanFilters { |
|
|
|
impl SpanFilters { |
|
|
|
pub fn new() -> Self { |
|
|
|
pub fn new() -> Self { |
|
|
|
Self { |
|
|
|
Self { |
|
|
|
filters: Filters::new(), |
|
|
|
filters: Filters::new(), |
|
|
|
filter_name: None, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
pub fn target(mut self, target: impl ToString) -> Self { |
|
|
|
|
|
|
|
self.filters = self.filters.target(target); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pub fn filter_name(mut self, name: impl ToString) -> Self { |
|
|
|
pub fn name(mut self, name: impl ToString) -> Self { |
|
|
|
self.filter_name = Some(name.to_string()); |
|
|
|
self.filters = self.filters.name(name); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn message(mut self, message: impl ToString) -> Self { |
|
|
|
|
|
|
|
self.filters = self.filters.message(message); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn field(mut self, field: impl ToString) -> Self { |
|
|
|
|
|
|
|
self.filters = self.filters.field(field); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn field_value(mut self, field: impl ToString, value: impl ToString) -> Self { |
|
|
|
|
|
|
|
self.filters = self.filters.field_value(field, value); |
|
|
|
self |
|
|
|
self |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -137,10 +186,8 @@ impl SpanFilters { |
|
|
|
where |
|
|
|
where |
|
|
|
S: Subscriber + for<'lookup> LookupSpan<'lookup>, |
|
|
|
S: Subscriber + for<'lookup> LookupSpan<'lookup>, |
|
|
|
{ |
|
|
|
{ |
|
|
|
if let Some(name) = &self.filter_name { |
|
|
|
if !self.filters.matches_metadata(span.metadata()) { |
|
|
|
if span.name() != name { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
true |
|
|
|
true |
|
|
|
} |
|
|
|
} |
|
|
@ -212,15 +259,16 @@ mod test { |
|
|
|
|
|
|
|
|
|
|
|
#[test] |
|
|
|
#[test] |
|
|
|
fn test_event_listener() { |
|
|
|
fn test_event_listener() { |
|
|
|
let all_listener = EventListener::new(Filters::new()); |
|
|
|
let none_listener = EventListener::new(Filters::new().target("not my module path")); |
|
|
|
let msg_listener = EventListener::new(Filters::new().filter_message("filter message")); |
|
|
|
let all_listener = EventListener::new(Filters::new().target(std::module_path!())); |
|
|
|
let field_listener = EventListener::new(Filters::new().filter_field("field")); |
|
|
|
let msg_listener = EventListener::new(Filters::new().message("filter message")); |
|
|
|
|
|
|
|
let field_listener = EventListener::new(Filters::new().field("field")); |
|
|
|
let field_value_listener = |
|
|
|
let field_value_listener = |
|
|
|
EventListener::new(Filters::new().filter_field_value("field", 1234)); |
|
|
|
EventListener::new(Filters::new().field_value("field", 1234)); |
|
|
|
let msg_field_value_listener = EventListener::new( |
|
|
|
let msg_field_value_listener = EventListener::new( |
|
|
|
Filters::new() |
|
|
|
Filters::new() |
|
|
|
.filter_message("filter message") |
|
|
|
.message("filter message") |
|
|
|
.filter_field_value("field", 1234), |
|
|
|
.field_value("field", 1234), |
|
|
|
); |
|
|
|
); |
|
|
|
let subscriber = tracing_subscriber::registry() |
|
|
|
let subscriber = tracing_subscriber::registry() |
|
|
|
.with(all_listener.clone()) |
|
|
|
.with(all_listener.clone()) |
|
|
@ -230,6 +278,7 @@ mod test { |
|
|
|
.with(msg_field_value_listener.clone()); |
|
|
|
.with(msg_field_value_listener.clone()); |
|
|
|
let _sub = tracing::subscriber::set_default(subscriber); |
|
|
|
let _sub = tracing::subscriber::set_default(subscriber); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(none_listener.get_count(), 0); |
|
|
|
assert_eq!(all_listener.get_count(), 0); |
|
|
|
assert_eq!(all_listener.get_count(), 0); |
|
|
|
assert_eq!(msg_listener.get_count(), 0); |
|
|
|
assert_eq!(msg_listener.get_count(), 0); |
|
|
|
assert_eq!(field_listener.get_count(), 0); |
|
|
|
assert_eq!(field_listener.get_count(), 0); |
|
|
@ -270,6 +319,7 @@ mod test { |
|
|
|
|
|
|
|
|
|
|
|
info!(field = 1234, "filter message"); |
|
|
|
info!(field = 1234, "filter message"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assert_eq!(none_listener.get_count(), 0); |
|
|
|
assert_eq!(all_listener.get_count(), 5); |
|
|
|
assert_eq!(all_listener.get_count(), 5); |
|
|
|
assert_eq!(msg_listener.get_count(), 2); |
|
|
|
assert_eq!(msg_listener.get_count(), 2); |
|
|
|
assert_eq!(field_listener.get_count(), 3); |
|
|
|
assert_eq!(field_listener.get_count(), 3); |
|
|
|