Skip to content

Commit bc86222

Browse files
enhancement(observability): add fixed tag option to RegisteredEventCache (#17814)
@bruceg highlighted the fact that there was no straight forward way to add fixed tags to the `RegisteredEventCache`. This adds the option to do that. Very quickly, supposing the `TaggedEventsSent` had two tags, `something` and `service`. `something` never changes so can be passed in when the event cache is first created. The code for this would look similar to: ```rust crate::registered_event!( TaggedEventsSent { something: String, service: OptionalTag<String>, } => { events: Counter = { register_counter!("component_sent_events_total", &make_tags(&self.something, &self.service)) }, event_bytes: Counter = { register_counter!("component_sent_event_bytes_total", &make_tags(&self.something, &self.service)) }, } fn emit(&self, data: CountByteSize) { ... } fn register(something: String, tags: EventCountTags) { super::register(TaggedEventsSent::new( something, tags, )) } ); ... let events_sent = RegisteredEventCache::default(String::from("something")); .... events_sent.emit(service, *size); ``` --------- Signed-off-by: Stephen Wakely <[email protected]>
1 parent 911477a commit bc86222

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

lib/vector-common/src/internal_event/cached_event.rs

+65-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ use super::{InternalEventHandle, RegisterInternalEvent};
1717
/// new event is emitted for a previously unseen set of tags an event is registered
1818
/// and stored in the cache.
1919
#[derive(Derivative)]
20-
#[derivative(Clone(bound = ""), Default(bound = ""))]
21-
pub struct RegisteredEventCache<Event: RegisterTaggedInternalEvent> {
20+
#[derivative(Clone(bound = "T: Clone"))]
21+
pub struct RegisteredEventCache<T, Event: RegisterTaggedInternalEvent> {
22+
fixed_tags: T,
2223
cache: Arc<
2324
RwLock<
2425
BTreeMap<
@@ -36,16 +37,31 @@ pub trait RegisterTaggedInternalEvent: RegisterInternalEvent {
3637
/// that will be used when registering the event.
3738
type Tags;
3839

39-
fn register(tags: Self::Tags) -> <Self as RegisterInternalEvent>::Handle;
40+
/// The type that contains data necessary to extract the tags that will
41+
/// be fixed and only need setting up front when the cache is first created.
42+
type Fixed;
43+
44+
fn register(fixed: Self::Fixed, tags: Self::Tags) -> <Self as RegisterInternalEvent>::Handle;
4045
}
4146

42-
impl<Event, EventHandle, Data, Tags> RegisteredEventCache<Event>
47+
impl<Event, EventHandle, Data, Tags, FixedTags> RegisteredEventCache<FixedTags, Event>
4348
where
4449
Data: Sized,
4550
EventHandle: InternalEventHandle<Data = Data>,
4651
Tags: Ord + Clone,
47-
Event: RegisterInternalEvent<Handle = EventHandle> + RegisterTaggedInternalEvent<Tags = Tags>,
52+
FixedTags: Clone,
53+
Event: RegisterInternalEvent<Handle = EventHandle>
54+
+ RegisterTaggedInternalEvent<Tags = Tags, Fixed = FixedTags>,
4855
{
56+
/// Create a new event cache with a set of fixed tags. These tags are passed to
57+
/// all registered events.
58+
pub fn new(fixed_tags: FixedTags) -> Self {
59+
Self {
60+
fixed_tags,
61+
cache: Arc::default(),
62+
}
63+
}
64+
4965
/// Emits the event with the given tags.
5066
/// It will register the event and store in the cache if this has not already
5167
/// been done.
@@ -58,7 +74,10 @@ where
5874
if let Some(event) = read.get(tags) {
5975
event.emit(value);
6076
} else {
61-
let event = <Event as RegisterTaggedInternalEvent>::register(tags.clone());
77+
let event = <Event as RegisterTaggedInternalEvent>::register(
78+
self.fixed_tags.clone(),
79+
tags.clone(),
80+
);
6281
event.emit(value);
6382

6483
// Ensure the read lock is dropped so we can write.
@@ -67,3 +86,43 @@ where
6786
}
6887
}
6988
}
89+
90+
#[cfg(test)]
91+
mod tests {
92+
#![allow(unreachable_pub)]
93+
use metrics::{register_counter, Counter};
94+
95+
use super::*;
96+
97+
crate::registered_event!(
98+
TestEvent {
99+
fixed: String,
100+
dynamic: String,
101+
} => {
102+
event: Counter = {
103+
register_counter!("test_event_total", "fixed" => self.fixed, "dynamic" => self.dynamic)
104+
},
105+
}
106+
107+
fn emit(&self, count: u64) {
108+
self.event.increment(count);
109+
}
110+
111+
fn register(fixed: String, dynamic: String) {
112+
crate::internal_event::register(TestEvent {
113+
fixed,
114+
dynamic,
115+
})
116+
}
117+
);
118+
119+
#[test]
120+
fn test_fixed_tag() {
121+
let event: RegisteredEventCache<String, TestEvent> =
122+
RegisteredEventCache::new("fixed".to_string());
123+
124+
for tag in 1..=5 {
125+
event.emit(&format!("dynamic{tag}"), tag);
126+
}
127+
}
128+
}

lib/vector-common/src/internal_event/events_sent.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ crate::registered_event!(
9191
self.event_bytes.increment(byte_size.get() as u64);
9292
}
9393

94-
fn register(tags: EventCountTags) {
94+
fn register(_fixed: (), tags: EventCountTags) {
9595
super::register(TaggedEventsSent::new(
9696
tags,
9797
))

lib/vector-common/src/internal_event/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ macro_rules! registered_event {
219219
fn emit(&$slf:ident, $data_name:ident: $data:ident)
220220
$emit_body:block
221221

222-
$(fn register($tags_name:ident: $tags:ty)
222+
$(fn register($fixed_name:ident: $fixed_tags:ty, $tags_name:ident: $tags:ty)
223223
$register_body:block)?
224224
) => {
225225
paste::paste!{
@@ -251,8 +251,10 @@ macro_rules! registered_event {
251251

252252
$(impl $crate::internal_event::cached_event::RegisterTaggedInternalEvent for $event {
253253
type Tags = $tags;
254+
type Fixed = $fixed_tags;
254255

255256
fn register(
257+
$fixed_name: $fixed_tags,
256258
$tags_name: $tags,
257259
) -> <Self as $crate::internal_event::RegisterInternalEvent>::Handle {
258260
$register_body

lib/vector-common/src/request_metadata.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ impl GroupedCountByteSize {
129129
}
130130

131131
/// Emits our counts to a `RegisteredEvent` cached event.
132-
pub fn emit_event<T, H>(&self, event_cache: &RegisteredEventCache<T>)
132+
pub fn emit_event<T, H>(&self, event_cache: &RegisteredEventCache<(), T>)
133133
where
134-
T: RegisterTaggedInternalEvent<Tags = EventCountTags, Handle = H>,
134+
T: RegisterTaggedInternalEvent<Tags = EventCountTags, Fixed = (), Handle = H>,
135135
H: InternalEventHandle<Data = CountByteSize>,
136136
{
137137
match self {

lib/vector-core/src/stream/driver.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ where
9999
pin!(batched_input);
100100

101101
let bytes_sent = protocol.map(|protocol| register(BytesSent { protocol }));
102-
let events_sent = RegisteredEventCache::default();
102+
let events_sent = RegisteredEventCache::new(());
103103

104104
loop {
105105
// Core behavior of the loop:
@@ -203,7 +203,7 @@ where
203203
finalizers: EventFinalizers,
204204
event_count: usize,
205205
bytes_sent: &Option<Registered<BytesSent>>,
206-
events_sent: &RegisteredEventCache<TaggedEventsSent>,
206+
events_sent: &RegisteredEventCache<(), TaggedEventsSent>,
207207
) {
208208
match result {
209209
Err(error) => {

0 commit comments

Comments
 (0)