-
Notifications
You must be signed in to change notification settings - Fork 2.2k
enhancement(observability): add fixed tag option to RegisteredEventCache
#17814
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1777df
Add fixed tag option to EventCache
StephenWakely 61ef321
Added smoke test
StephenWakely 0e4f629
Clippy
StephenWakely 2ffc42a
Merge remote-tracking branch 'origin' into stephen/cached_events_fixe…
StephenWakely cb40494
Udate the Derivative bounds
StephenWakely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,8 +17,9 @@ use super::{InternalEventHandle, RegisterInternalEvent}; | |
| /// new event is emitted for a previously unseen set of tags an event is registered | ||
| /// and stored in the cache. | ||
| #[derive(Derivative)] | ||
| #[derivative(Clone(bound = ""), Default(bound = ""))] | ||
| pub struct RegisteredEventCache<Event: RegisterTaggedInternalEvent> { | ||
| #[derivative(Clone(bound = ""), Default(bound = "T: Default"))] | ||
| pub struct RegisteredEventCache<T: Clone, Event: RegisterTaggedInternalEvent> { | ||
| fixed_tags: T, | ||
| cache: Arc< | ||
| RwLock< | ||
| BTreeMap< | ||
|
|
@@ -36,16 +37,31 @@ pub trait RegisterTaggedInternalEvent: RegisterInternalEvent { | |
| /// that will be used when registering the event. | ||
| type Tags; | ||
|
|
||
| fn register(tags: Self::Tags) -> <Self as RegisterInternalEvent>::Handle; | ||
| /// The type that contains data necessary to extract the tags that will | ||
| /// be fixed and only need setting up front when the cache is first created. | ||
| type Fixed; | ||
|
|
||
| fn register(fixed: Self::Fixed, tags: Self::Tags) -> <Self as RegisterInternalEvent>::Handle; | ||
| } | ||
|
|
||
| impl<Event, EventHandle, Data, Tags> RegisteredEventCache<Event> | ||
| impl<Event, EventHandle, Data, Tags, FixedTags> RegisteredEventCache<FixedTags, Event> | ||
| where | ||
| Data: Sized, | ||
| EventHandle: InternalEventHandle<Data = Data>, | ||
| Tags: Ord + Clone, | ||
| Event: RegisterInternalEvent<Handle = EventHandle> + RegisterTaggedInternalEvent<Tags = Tags>, | ||
| FixedTags: Clone, | ||
| Event: RegisterInternalEvent<Handle = EventHandle> | ||
| + RegisterTaggedInternalEvent<Tags = Tags, Fixed = FixedTags>, | ||
| { | ||
| /// Create a new event cache with a set of fixed tags. These tags are passed to | ||
| /// all registered events. | ||
| pub fn new(fixed_tags: FixedTags) -> Self { | ||
| Self { | ||
| fixed_tags, | ||
| cache: Arc::default(), | ||
| } | ||
| } | ||
|
|
||
| /// Emits the event with the given tags. | ||
| /// It will register the event and store in the cache if this has not already | ||
| /// been done. | ||
|
|
@@ -58,7 +74,10 @@ where | |
| if let Some(event) = read.get(tags) { | ||
| event.emit(value); | ||
| } else { | ||
| let event = <Event as RegisterTaggedInternalEvent>::register(tags.clone()); | ||
| let event = <Event as RegisterTaggedInternalEvent>::register( | ||
| self.fixed_tags.clone(), | ||
| tags.clone(), | ||
| ); | ||
| event.emit(value); | ||
|
|
||
| // Ensure the read lock is dropped so we can write. | ||
|
|
@@ -67,3 +86,43 @@ where | |
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| #![allow(unreachable_pub)] | ||
| use metrics::{register_counter, Counter}; | ||
|
|
||
| use super::*; | ||
|
|
||
| crate::registered_event!( | ||
| TestEvent { | ||
| fixed: String, | ||
| dynamic: String, | ||
| } => { | ||
| event: Counter = { | ||
| register_counter!("test_event_total", "fixed" => self.fixed, "dynamic" => self.dynamic) | ||
| }, | ||
| } | ||
|
|
||
| fn emit(&self, count: u64) { | ||
| self.event.increment(count); | ||
| } | ||
|
|
||
| fn register(fixed: String, dynamic: String) { | ||
| crate::internal_event::register(TestEvent { | ||
| fixed, | ||
| dynamic, | ||
| }) | ||
| } | ||
| ); | ||
|
|
||
| #[test] | ||
| fn test_fixed_tag() { | ||
| let event: RegisteredEventCache<String, TestEvent> = | ||
| RegisteredEventCache::new("fixed".to_string()); | ||
|
|
||
| for tag in 1..=5 { | ||
| event.emit(&format!("dynamic{tag}"), tag); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a smoke test at the minute. I'm not sure if there is a way to capture and test the events that were emitted from |
||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 don't think the
Defaultderive here really makes sense given the fixed tags. Also, does theClonedrive not need aT: Clonebound (in which case it's the same as just#[derive(Clone)], or is that covered by theT: Clonebound in the generics?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 did have the
defaultfor cases where there are no fixed tags. Essentially thentype Fixed = ().But on reflection, I think it does make more sense to be more explicit about this so if there are no fixed tags you have to do
RegisteredEventCache::new(()).I've updated the
Clonebound toT:Cloneso you only needTto be clone if you want to clone the tags.