vector_common/internal_event/
component_events_dropped.rs

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