Skip to content

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

cijothomas
Copy link
Member

Fixes #
Design discussion issue (if applicable) #

Changes

Please provide a brief description of the changes here.

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

@cijothomas cijothomas requested a review from a team as a code owner May 23, 2025 01:55
@cijothomas cijothomas changed the title Cijothomas/usereventlog callback UserEvents LogExporter - add callback to control event_name May 23, 2025
@cijothomas cijothomas changed the title UserEvents LogExporter - add callback to control event_name feat: UserEvents LogExporter - add callback to control event_name May 23, 2025
Copy link

codecov bot commented May 23, 2025

Codecov Report

Attention: Patch coverage is 36.58537% with 78 lines in your changes missing coverage. Please review.

Project coverage is 51.3%. Comparing base (83e7fae) to head (73e0e35).

Files with missing lines Patch % Lines
opentelemetry-user-events-logs/src/lib.rs 0.0% 62 Missing ⚠️
...entelemetry-user-events-logs/src/logs/processor.rs 74.4% 12 Missing ⚠️
...pentelemetry-user-events-logs/src/logs/exporter.rs 71.4% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -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>;
Copy link
Member

@lalitb lalitb May 23, 2025

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Contributor

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.

Copy link
Member Author

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?

Copy link
Contributor

@utpilla utpilla May 23, 2025

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")
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants