vector_common/internal_event/
component_events_dropped.rs

1use metrics::Counter;
2
3use crate::counter;
4
5use super::{Count, CounterName, InternalEvent, InternalEventHandle, RegisterInternalEvent};
6use crate::NamedInternalEvent;
7
8pub const INTENTIONAL: bool = true;
9pub const UNINTENTIONAL: bool = false;
10
11#[derive(Debug, NamedInternalEvent)]
12pub struct ComponentEventsDropped<'a, const INTENTIONAL: bool> {
13    pub count: usize,
14    pub reason: &'a str,
15}
16
17impl<const INTENTIONAL: bool> InternalEvent for ComponentEventsDropped<'_, INTENTIONAL> {
18    fn emit(self) {
19        let count = self.count;
20        self.register().emit(Count(count));
21    }
22}
23
24impl<'a, const INTENTIONAL: bool> From<&'a str> for ComponentEventsDropped<'a, INTENTIONAL> {
25    fn from(reason: &'a str) -> Self {
26        Self { count: 0, reason }
27    }
28}
29
30// ComponentEventsDropped is the foundation type the `registered_event!` macro
31// abstracts over, so we have to implement RegisterInternalEvent by hand here.
32impl<'a, const INTENTIONAL: bool> RegisterInternalEvent
33    for ComponentEventsDropped<'a, INTENTIONAL>
34{
35    // ## skip check-validity-events ##
36    type Handle = DroppedHandle<'a, INTENTIONAL>;
37    fn register(self) -> Self::Handle {
38        Self::Handle {
39            discarded_events: counter!(
40                CounterName::ComponentDiscardedEventsTotal,
41                "intentional" => if INTENTIONAL { "true" } else { "false" },
42            ),
43            reason: self.reason,
44        }
45    }
46}
47
48#[derive(Clone)]
49pub struct DroppedHandle<'a, const INTENDED: bool> {
50    discarded_events: Counter,
51    reason: &'a str,
52}
53
54impl<const INTENDED: bool> InternalEventHandle for DroppedHandle<'_, INTENDED> {
55    type Data = Count;
56    fn emit(&self, data: Self::Data) {
57        let message = "Events dropped";
58        if INTENDED {
59            debug!(
60                message,
61                intentional = INTENDED,
62                count = data.0,
63                reason = self.reason,
64            );
65        } else {
66            error!(
67                message,
68                intentional = INTENDED,
69                count = data.0,
70                reason = self.reason,
71            );
72        }
73        self.discarded_events.increment(data.0 as u64);
74    }
75}