Skip to content

Commit

Permalink
targets
Browse files Browse the repository at this point in the history
  • Loading branch information
Robo210 committed Jul 13, 2023
1 parent e0d699e commit abdab68
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 20 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ tracing = {version = "0.1", default-features = false, features = ["std"]}
tracing-subscriber = {version="0.3", default-features = false, features=["std", "fmt", "registry"]}
tracelogging = ">= 1.1.0"
tracelogging_dynamic = ">= 1.1.0"
eventheader = "0.2"
eventheader_dynamic = "0.2"
eventheader = "0.3.1"
eventheader_dynamic = "0.3.1"
chrono = {version="0.4", default-features = false, features=["std"]}

[dev-dependencies]
Expand Down
3 changes: 2 additions & 1 deletion benches/etw.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![allow(unused_imports, dead_code)]

use criterion::{criterion_group, criterion_main, Criterion};
#[cfg(target_os = "windows")]
use etw_helpers::{FileMode, SessionBuilder};
use tracing::{event, span, Level};
use tracing_etw::*;
Expand All @@ -11,7 +12,7 @@ pub fn etw_benchmark(c: &mut Criterion) {
let builder = LayerBuilder::new("etw_bench");
let provider_id = builder.get_provider_id().to_u128();
let _subscriber = tracing_subscriber::registry()
.with(builder.build_with_layer_filter())
.with(builder.build())
.init();

let etw_session = SessionBuilder::new_file_mode(
Expand Down
4 changes: 2 additions & 2 deletions benches/user_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ use tracing_subscriber::{self, prelude::*};

#[cfg(target_os = "linux")]
pub fn user_events_benchmark(c: &mut Criterion) {
let builder = EtwLayerBuilder::new("user_events_bench");
let builder = LayerBuilder::new("user_events_bench");
let _subscriber = tracing_subscriber::registry()
.with(builder.build_with_layer_filter())
.with(builder.build())
.init();

// Disabled provider
Expand Down
6 changes: 4 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ fn main() {
// .without_time();

let subscriber = tracing_subscriber::registry()
.with(LayerBuilder::new("test").build_with_layer_filter())
.with(LayerBuilder::new_common_schema_events("test2").build_with_layer_filter())
.with(LayerBuilder::new("test").build()) // Collects everything
.with(LayerBuilder::new_common_schema_events("test2").build_with_target("geneva"))
.with(tracing_subscriber::fmt::layer().with_span_events(FmtSpan::ACTIVE));
let _sub = subscriber.try_init();

Expand Down Expand Up @@ -39,4 +39,6 @@ fn main() {

drop(_enter);
drop(span);

event!(target: "geneva", Level::INFO, "Only for geneva!");
}
83 changes: 70 additions & 13 deletions src/layer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ use std::{pin::Pin, sync::Arc};

use tracelogging::Guid;
use tracing::{span, Subscriber};
use tracing_subscriber::filter::Filtered;
use tracing_subscriber::filter::{Filtered, FilterExt, Targets, combinator::And};
use tracing_subscriber::layer::Filter;
use tracing_subscriber::{registry::LookupSpan, Layer};
use tracing::metadata::LevelFilter;

use crate::native::ProviderGroup;

use crate::native::{EventMode, EventWriter};
use crate::values::*;
Expand Down Expand Up @@ -157,7 +160,7 @@ where
}

#[cfg(feature = "global_filter")]
pub fn build_with_global_filter<S>(self) -> EtwLayer<S, Mode>
pub fn build<S>(self) -> EtwLayer<S, Mode>
where
S: Subscriber + for<'a> LookupSpan<'a>,
{
Expand All @@ -175,17 +178,30 @@ where
}
}

#[cfg(not(feature = "global_filter"))]
pub fn build_with_layer_filter<S>(
self,
) -> Filtered<EtwLayer<S, Mode::Provider>, EtwFilter<S, Mode::Provider>, S>
fn build_target_filter(&self, target: &'static str,) -> Targets {
let mut targets = Targets::new().with_target(&self.provider_name, LevelFilter::TRACE);

match self.provider_group {
ProviderGroup::Windows(_guid) => {},
ProviderGroup::Linux(ref name) => {
targets = targets.with_target(name.clone(), LevelFilter::TRACE);
}
_ => {}
}

if !target.is_empty() {
targets = targets.with_target(target, LevelFilter::TRACE)
}

targets
}

fn build_layer<S>(&self) -> EtwLayer<S, Mode::Provider>
where
S: Subscriber + for<'a> LookupSpan<'a>,
Mode::Provider: EventWriter + 'static,
{
self.validate_config();

let layer = EtwLayer::<S, Mode::Provider> {
EtwLayer::<S, Mode::Provider> {
provider: Mode::Provider::new(
&self.provider_name,
&self.provider_id,
Expand All @@ -194,13 +210,54 @@ where
),
default_keyword: self.default_keyword,
_p: PhantomData,
};
}
}

let filter = EtwFilter::<S, _> {
provider: layer.provider.clone(),
fn build_filter<S, P>(&self, provider: Pin<Arc<P>>) -> EtwFilter<S, P>
where
S: Subscriber + for<'a> LookupSpan<'a>,
P: EventWriter + 'static,
{
EtwFilter::<S, _> {
provider,
default_keyword: self.default_keyword,
_p: PhantomData,
};
}
}

#[cfg(not(feature = "global_filter"))]
pub fn build_with_target<S>(
self,
target: &'static str,
) -> Filtered<EtwLayer<S, Mode::Provider>, And<EtwFilter<S, Mode::Provider>, Targets, S>, S>
where
S: Subscriber + for<'a> LookupSpan<'a>,
Mode::Provider: EventWriter + 'static,
{
self.validate_config();

let layer = self.build_layer();

let filter = self.build_filter(layer.provider.clone());

let targets = self.build_target_filter(target);

layer.with_filter(filter.and(targets))
}

#[cfg(not(feature = "global_filter"))]
pub fn build<S>(
self
) -> Filtered<EtwLayer<S, Mode::Provider>, EtwFilter<S, Mode::Provider>, S>
where
S: Subscriber + for<'a> LookupSpan<'a>,
Mode::Provider: EventWriter + 'static,
{
self.validate_config();

let layer = self.build_layer();

let filter = self.build_filter(layer.provider.clone());

layer.with_filter(filter)
}
Expand Down

0 comments on commit abdab68

Please sign in to comment.