vector_common/internal_event/
component_events_dropped.rs

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