vector_common/internal_event/
mod.rs1mod bytes_received;
2mod bytes_sent;
3pub mod cached_event;
4pub mod component_events_dropped;
5pub mod component_events_timed_out;
6mod events_received;
7mod events_sent;
8pub mod metric_name;
9mod optional_tag;
10mod prelude;
11pub mod service;
12
13use std::ops::{Add, AddAssign};
14
15pub use bytes_received::{BytesReceived, BytesReceivedHandle};
16pub use bytes_sent::BytesSent;
17#[allow(clippy::module_name_repetitions)]
18pub use cached_event::{RegisterTaggedInternalEvent, RegisteredEventCache};
19pub use component_events_dropped::{ComponentEventsDropped, INTENTIONAL, UNINTENTIONAL};
20pub use component_events_timed_out::ComponentEventsTimedOut;
21pub use events_received::{EventsReceived, EventsReceivedHandle};
22pub use events_sent::{DEFAULT_OUTPUT, EventsSent, TaggedEventsSent};
23pub use metric_name::{CounterName, GaugeName, HistogramName};
24pub use metrics::SharedString;
25pub use optional_tag::OptionalTag;
26pub use prelude::{error_stage, error_type};
27pub use service::{CallError, PollReadyError};
28
29use crate::json_size::JsonSize;
30
31pub trait NamedInternalEvent {
32 fn name(&self) -> &'static str;
33}
34
35pub trait InternalEvent: NamedInternalEvent + Sized {
36 fn emit(self);
37}
38
39#[allow(clippy::module_name_repetitions)]
40pub trait RegisterInternalEvent: NamedInternalEvent + Sized {
41 type Handle: InternalEventHandle;
42
43 fn register(self) -> Self::Handle;
44}
45
46#[allow(clippy::module_name_repetitions)]
47pub trait InternalEventHandle: Sized {
48 type Data: Sized;
49 fn emit(&self, data: Self::Data);
50}
51
52#[cfg(any(test, feature = "test"))]
53pub fn emit(event: impl InternalEvent) {
54 super::event_test_util::record_internal_event(event.name());
55 event.emit();
56}
57
58#[cfg(not(any(test, feature = "test")))]
59pub fn emit(event: impl InternalEvent) {
60 event.emit();
61}
62
63#[cfg(any(test, feature = "test"))]
64pub fn register<E: RegisterInternalEvent>(event: E) -> E::Handle {
65 super::event_test_util::record_internal_event(event.name());
66 event.register()
67}
68
69#[cfg(not(any(test, feature = "test")))]
70pub fn register<E: RegisterInternalEvent>(event: E) -> E::Handle {
71 event.register()
72}
73
74pub type Registered<T> = <T as RegisterInternalEvent>::Handle;
75
76#[derive(Clone, Copy)]
79pub struct ByteSize(pub usize);
80
81#[derive(Clone, Copy)]
82pub struct Count(pub usize);
83
84#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
86pub struct CountByteSize(pub usize, pub JsonSize);
87
88impl AddAssign for CountByteSize {
89 fn add_assign(&mut self, rhs: Self) {
90 self.0 += rhs.0;
91 self.1 += rhs.1;
92 }
93}
94
95impl Add<CountByteSize> for CountByteSize {
96 type Output = CountByteSize;
97
98 fn add(self, rhs: CountByteSize) -> Self::Output {
99 CountByteSize(self.0 + rhs.0, self.1 + rhs.1)
100 }
101}
102
103pub struct Output(pub Option<SharedString>);
106
107pub struct Protocol(pub SharedString);
108
109impl Protocol {
110 pub const HTTP: Protocol = Protocol(SharedString::const_str("http"));
111 pub const HTTPS: Protocol = Protocol(SharedString::const_str("https"));
112 pub const NONE: Protocol = Protocol(SharedString::const_str("none"));
113 pub const TCP: Protocol = Protocol(SharedString::const_str("tcp"));
114 pub const UDP: Protocol = Protocol(SharedString::const_str("udp"));
115 pub const UNIX: Protocol = Protocol(SharedString::const_str("unix"));
116 pub const INTERNAL: Protocol = Protocol(SharedString::const_str("internal"));
117 pub const STATIC: Protocol = Protocol(SharedString::const_str("static"));
118}
119
120impl From<&'static str> for Protocol {
121 fn from(s: &'static str) -> Self {
122 Self(SharedString::const_str(s))
123 }
124}
125
126impl From<Protocol> for SharedString {
127 fn from(value: Protocol) -> Self {
128 value.0
129 }
130}
131
132#[macro_export]
162macro_rules! registered_event {
163 ($event:ident => $($tail:tt)*) => {
165 #[derive(Debug, Clone, Eq, Hash, $crate::NamedInternalEvent, Ord, PartialEq, PartialOrd)]
166 pub struct $event;
167
168 $crate::registered_event!(=> $event $($tail)*);
169 };
170
171 ($event:ident { $( $field:ident: $type:ty, )* } => $($tail:tt)*) => {
173 #[derive(Debug, Clone, Eq, Hash, $crate::NamedInternalEvent, Ord, PartialEq, PartialOrd)]
174 pub struct $event {
175 $( pub $field: $type, )*
176 }
177
178 $crate::registered_event!(=> $event $($tail)*);
179 };
180
181 (
183 => $event:ident {
184 $( $field:ident: $type:ty = $value:expr, )*
185 }
186
187 fn emit(&$slf:ident, $data_name:ident: $data:ident)
188 $emit_body:block
189
190 $(fn register($fixed_name:ident: $fixed_tags:ty, $tags_name:ident: $tags:ty)
191 $register_body:block)?
192 ) => {
193 pastey::paste!{
194 #[derive(Clone)]
195 pub struct [<$event Handle>] {
196 $( $field: $type, )*
197 }
198
199 impl $crate::internal_event::RegisterInternalEvent for $event {
200 type Handle = [<$event Handle>];
201
202 fn register($slf) -> Self::Handle {
203 Self::Handle {
204 $( $field: $value, )*
205 }
206 }
207 }
208
209 impl $crate::internal_event::InternalEventHandle for [<$event Handle>] {
210 type Data = $data;
211
212 fn emit(&$slf, $data_name: $data)
213 $emit_body
214 }
215
216 $(impl $crate::internal_event::cached_event::RegisterTaggedInternalEvent for $event {
217 type Tags = $tags;
218 type Fixed = $fixed_tags;
219
220 fn register(
221 $fixed_name: $fixed_tags,
222 $tags_name: $tags,
223 ) -> <Self as $crate::internal_event::RegisterInternalEvent>::Handle {
224 $register_body
225 }
226 })?
227
228 }
229 };
230}