Skip to content

Commit

Permalink
Merge pull request #16 from microsoft/dashmap
Browse files Browse the repository at this point in the history
Remove dashmap and replace with static array
  • Loading branch information
Robo210 authored Dec 3, 2024
2 parents 45f4fc6 + 89ced8c commit 56d6fcb
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 69 deletions.
7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"
resolver = "2"
license = "MIT"
repository = "https://github.com/microsoft/tracing-etw"
rust-version = "1.78"
rust-version = "1.80"
authors = ["Kyle Sabo", "Microsoft"]
description = "ETW or Linux user_events output for tokio-tracing"

Expand All @@ -22,10 +22,9 @@ tracing = {version = "0.1", default-features = false, features = ["std"]}
tracing-core = {version = "0.1", default-features = false}
tracing-subscriber = {version="0.3", default-features = false, features=["std", "fmt", "registry"]}
chrono = {version="0.4", default-features = false, features=["std"]}
once_cell = ">=1.18"
dashmap = "6"
paste = "1"
thiserror = "1"
thiserror = {version="2", default-features = false}
hashers = "1"

[target.'cfg(not(target_os = "linux"))'.dependencies]
tracelogging = ">= 1.2.0"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ encoding; requires a Linux 6.4+ kernel).

*Note*: Linux kernels without user_events support will not log any events.

# MSV

Minimum supported Rust version is 1.80 (released July 2024).

[etw]: https://learn.microsoft.com/windows/win32/etw/about-event-tracing
[eventheader]: https://github.com/microsoft/LinuxTracepoints/tree/main/libeventheader-tracepoint
[`tracing`]: https://crates.io/crates/tracing
Expand Down
13 changes: 11 additions & 2 deletions examples/etw_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ use tracing_subscriber::{self, fmt::format::FmtSpan, prelude::*};

fn main() {
tracing_subscriber::registry()
.with(LayerBuilder::new_common_schema_events("ExampleProvEtwEvent_CS").build().unwrap())
.with(LayerBuilder::new("ExampleProvEtwEvent").build().unwrap())
.with(tracing_subscriber::fmt::layer().with_span_events(FmtSpan::ACTIVE))
.init();

etw_event!(name: "EtwEventName", Level::ERROR, 5, "An event with a name and keyword!");
etw_event!(name: "EtwEventName1", Level::ERROR, 1, "An event with a name and keyword!");
etw_event!(name: "EtwEventName2", Level::WARN, 2, "An event with a name and keyword!");
etw_event!(name: "EtwEventName3", Level::INFO, 3, "An event with a name and keyword!");
etw_event!(name: "EtwEventName4", Level::DEBUG, 4, "An event with a name and keyword!");
etw_event!(name: "EtwEventName5", Level::TRACE, 5, "An event with a name and keyword!");
etw_event!(name: "EtwEventName6", Level::TRACE, 6, "An event with a name and keyword!");
etw_event!(name: "EtwEventName7", Level::DEBUG, 7, "An event with a name and keyword!");
etw_event!(name: "EtwEventName8", Level::INFO, 8, "An event with a name and keyword!");
etw_event!(name: "EtwEventName9", Level::WARN, 9, "An event with a name and keyword!");
etw_event!(name: "EtwEventName10", Level::ERROR, 10, "An event with a name and keyword!");
}
10 changes: 8 additions & 2 deletions src/_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
//
// Implementations for these structs are contained in other files.



// Public with public fields because the `etw_event!` macro needs to create it at invocation site.
#[doc(hidden)]
#[repr(C)]
pub struct EventMetadata {
pub kw: u64,
pub identity: tracing::callsite::Identifier,
pub event_tag: u32,
}

// An EventMetadata with a hash, because Identity doesn't implement comparisons but we need a stable ordering.
#[derive(Clone)]
pub(crate) struct ParsedEventMetadata {
pub(crate) identity_hash: u64,
pub(crate) meta: &'static EventMetadata
}
4 changes: 2 additions & 2 deletions src/layer/filter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use tracing::Subscriber;
use tracing_subscriber::{layer::Filter, registry::LookupSpan};

use crate::{native::{EventWriter, ProviderTypes}, statics::EVENT_METADATA};
use crate::{native::{EventWriter, ProviderTypes}, statics::get_event_metadata};

use super::*;

Expand All @@ -15,7 +15,7 @@ where
&self,
metadata: &'static tracing::Metadata<'static>,
) -> tracing::subscriber::Interest {
let etw_meta = EVENT_METADATA.get(&metadata.callsite());
let etw_meta = get_event_metadata(&metadata.callsite());
let keyword = if let Some(meta) = etw_meta {
meta.kw
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/layer/layer_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ where
&self,
metadata: &'static tracing::Metadata<'static>,
) -> tracing::subscriber::Interest {
let etw_meta = EVENT_METADATA.get(&metadata.callsite());
let etw_meta = get_event_metadata(&metadata.callsite());
let keyword = if let Some(meta) = etw_meta {
meta.kw
} else {
Expand Down Expand Up @@ -84,7 +84,7 @@ where
.event_span(event)
.map_or(0, |evt| evt.parent().map_or(0, |p| p.id().into_u64()));

let etw_meta = EVENT_METADATA.get(&event.metadata().callsite());
let etw_meta = get_event_metadata(&event.metadata().callsite());
let (name, keyword, tag) = if let Some(meta) = etw_meta {
(event.metadata().name(), meta.kw, meta.event_tag)
} else {
Expand Down Expand Up @@ -203,7 +203,7 @@ where
return;
};

let etw_meta = EVENT_METADATA.get(&metadata.callsite());
let etw_meta = get_event_metadata(&metadata.callsite());
let (keyword, tag) = if let Some(meta) = etw_meta {
(meta.kw, meta.event_tag)
} else {
Expand Down Expand Up @@ -244,7 +244,7 @@ where
return;
};

let etw_meta = EVENT_METADATA.get(&metadata.callsite());
let etw_meta = get_event_metadata(&metadata.callsite());
let (keyword, tag) = if let Some(meta) = etw_meta {
(meta.kw, meta.event_tag)
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/layer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use tracing::Subscriber;
use tracing_core::callsite;
use tracing_subscriber::registry::LookupSpan;

use crate::{native::{EventWriter, ProviderTypes}, statics::EVENT_METADATA};
use crate::{native::{EventWriter, ProviderTypes}, statics::get_event_metadata};

pub(crate) struct _EtwLayer<S, Mode: ProviderTypes>
where
Expand Down Expand Up @@ -59,7 +59,7 @@ where
Mode::Provider: EventWriter<Mode> + 'static,
{
fn is_enabled(&self, callsite: &callsite::Identifier, level: &tracing_core::Level) -> bool {
let etw_meta = EVENT_METADATA.get(callsite);
let etw_meta = get_event_metadata(callsite);
let keyword = if let Some(meta) = etw_meta {
meta.kw
} else {
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,22 @@ macro_rules! etw_event {

static ETW_META: $crate::_details::EventMetadata = $crate::_details::EventMetadata{
kw: $kw,
// TODO: Hash the callsite identity at compile time, or get Identifier to implement Ord
identity: tracing_core::identify_callsite!(&CALLSITE),
event_tag: $tags as u32
};

paste! {
#[cfg(target_os = "linux")]
#[link_section = "_etw_kw"]
#[allow(non_upper_case_globals)]
#[allow(non_upper_case_globals, dead_code)]
static mut [<ETW_META_PTR $name>]: *const $crate::_details::EventMetadata = &ETW_META;
}

paste! {
#[cfg(target_os = "windows")]
#[link_section = ".rsdata$zRSETW5"]
#[allow(non_upper_case_globals)]
#[allow(non_upper_case_globals, dead_code)]
static mut [<ETW_META_PTR $name>]: *const $crate::_details::EventMetadata = &ETW_META;
}

Expand Down
3 changes: 2 additions & 1 deletion src/native/common_schema/user_events_cs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ impl crate::native::EventWriter<CommonSchemaProvider> for CommonSchemaProvider {
}
let mut provider = eventheader_dynamic::Provider::new(provider_name, &options);

for event in &*crate::statics::EVENT_METADATA {
// Keywords are static, but levels are dynamic so we have to register them all
for event in crate::statics::event_metadata() {
provider.register_set(
Self::map_level(&tracing::Level::ERROR),
event.kw,
Expand Down
3 changes: 2 additions & 1 deletion src/native/user_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ impl crate::native::EventWriter<Provider> for Provider {
}
let mut provider = eventheader_dynamic::Provider::new(provider_name, &options);

for event in &*crate::statics::EVENT_METADATA {
// Keywords are static, but levels are dynamic so we have to register them all
for event in crate::statics::event_metadata() {
provider.register_set(
Self::map_level(&tracing::Level::ERROR),
event.kw,
Expand Down
Loading

0 comments on commit 56d6fcb

Please sign in to comment.