-
Notifications
You must be signed in to change notification settings - Fork 61
feat: UserEvents LogExporter - add callback to control event_name #275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
feat: UserEvents LogExporter - add callback to control event_name #275
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #275 +/- ##
=======================================
- Coverage 51.4% 51.3% -0.1%
=======================================
Files 64 64
Lines 8781 8870 +89
=======================================
+ Hits 4516 4557 +41
- Misses 4265 4313 +48 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
@@ -11,13 +11,18 @@ use std::{cell::RefCell, str, time::SystemTime}; | |||
|
|||
thread_local! { static EBW: RefCell<EventBuilder> = RefCell::new(EventBuilder::new());} | |||
|
|||
/// Type alias for the event name callback function | |||
pub(crate) type EventNameCallback = | |||
Box<dyn Fn(&opentelemetry_sdk::logs::SdkLogRecord) -> &str + Send + Sync + 'static>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should it return String
or Cow<'a, str>
, allowing user to return the dynamic string from the callback? With current approach, the use can only use the static str, or the references to data owned by LogRecord.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@psandana also.
I think Cow would be nice, so users relying on something already from logrecord(like event name) won't have to allocate.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why use Cow
? We only need &str
. The user_events API does not require us to provide them an owned string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if we use &str, then it won't be possible to generate a totally new string inside the callback and returns & to it.
We'll be restricted to returning either log record.eventname or target, but not a new string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But then we would be forcing them to always allocate a string. Let's say a user wants the result to be event_name
appended by some characters, for example "123", wouldn't we be forcing them to always create a new string on the hot path? That would be bad for performance.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes that is correct. If creating a totally new string, then it'll allocate. If using existing event_name or target (or a sub-slice of it), then it won't need allocation.
Is there an alternative to return &str from the callback? Compiler won't allow returning a &str for a newly created String inside the callback right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this feature should provide some mechanism to avoid allocations. For example, a user should be able to use a HashMap to cache the required mappings for event_name
, target
etc. and reuse those allocations for subsequent calls.
Maybe we can look at achieving this through a trait instead of a closure-based callback. The implementation of the trait could then hold any state (Hashmap etc.) and use it along with &LogRecord
to return a &str
. It might be possible to do it using closure-based callback as well but that might not be as straightforward.
pub trait EventNameProvider: Send + Sync {
fn get_event_name(&self, record: &LogRecord) -> &str;
}
pub struct UserEventsExporter<EP: EventNameProvider> {
...
provider: EP,
}
impl<EP: EventNameProvider> UserEventsExporter<EP> {
pub fn new(provider: EP) -> Self {
UserEventsExporter { ..., provider }
}
pub fn export(&self, record: &LogRecord) {
...
let event_name = self.provider.get_event_name(record);
...
}
}
pub struct MapBasedProvider {
map: HashMap<String, String>,
}
impl EventNameProvider for MapBasedProvider {
fn get_event_name(&self, record: &LogRecord) -> &str {
self.map
.get(&record.event_name)
.map(|s| s.as_str())
.unwrap_or("Unknown event")
}
}
Fixes #
Design discussion issue (if applicable) #
Changes
Please provide a brief description of the changes here.
Merge requirement checklist
CHANGELOG.md
files updated for non-trivial, user-facing changes