vector_common/internal_event/
component_events_dropped.rs

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