1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use metrics::counter;
use vector_lib::internal_event::{ComponentEventsDropped, InternalEvent, INTENTIONAL};

#[derive(Debug)]
pub(crate) struct ThrottleEventDiscarded {
    pub key: String,
    pub emit_events_discarded_per_key: bool,
}

impl InternalEvent for ThrottleEventDiscarded {
    fn emit(self) {
        let message = "Rate limit exceeded.";

        debug!(message, key = self.key, internal_log_rate_limit = true);
        if self.emit_events_discarded_per_key {
            // TODO: Technically, the Component Specification states that the discarded events metric
            // must _only_ have the `intentional` tag, in addition to the core tags like
            // `component_kind`, etc, and nothing else.
            //
            // That doesn't give us the leeway to specify which throttle bucket the events are being
            // discarded for... but including the key/bucket as a tag does seem useful and so I wonder
            // if we should change the specification wording? Sort of a similar situation to the
            // `error_code` tag for the component errors metric, where it's meant to be optional and
            // only specified when relevant.
            counter!("events_discarded_total", "key" => self.key).increment(1); // Deprecated.
        }

        emit!(ComponentEventsDropped::<INTENTIONAL> {
            count: 1,
            reason: message
        })
    }
}