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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use metrics::{counter, histogram};
use vector_lib::internal_event::{ComponentEventsDropped, InternalEvent, UNINTENTIONAL};
use vector_lib::{
    internal_event::{error_stage, error_type},
    json_size::JsonSize,
};

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
#[allow(dead_code)] // some features only use some variants
pub enum SocketMode {
    Tcp,
    Udp,
    Unix,
}

impl SocketMode {
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Tcp => "tcp",
            Self::Udp => "udp",
            Self::Unix => "unix",
        }
    }
}
#[derive(Debug)]
pub struct SocketBytesReceived {
    pub mode: SocketMode,
    pub byte_size: usize,
}

impl InternalEvent for SocketBytesReceived {
    fn emit(self) {
        let protocol = self.mode.as_str();
        trace!(
            message = "Bytes received.",
            byte_size = %self.byte_size,
            %protocol,
        );
        counter!(
            "component_received_bytes_total",
            "protocol" => protocol,
        )
        .increment(self.byte_size as u64);
        histogram!("component_received_bytes").record(self.byte_size as f64);
    }
}

#[derive(Debug)]
pub struct SocketEventsReceived {
    pub mode: SocketMode,
    pub byte_size: JsonSize,
    pub count: usize,
}

impl InternalEvent for SocketEventsReceived {
    fn emit(self) {
        let mode = self.mode.as_str();
        trace!(
            message = "Events received.",
            count = self.count,
            byte_size = self.byte_size.get(),
            %mode,
        );
        counter!("component_received_events_total", "mode" => mode).increment(self.count as u64);
        counter!("component_received_event_bytes_total", "mode" => mode)
            .increment(self.byte_size.get() as u64);
        histogram!("component_received_bytes", "mode" => mode).record(self.byte_size.get() as f64);
    }
}

#[derive(Debug)]
pub struct SocketBytesSent {
    pub mode: SocketMode,
    pub byte_size: usize,
}

impl InternalEvent for SocketBytesSent {
    fn emit(self) {
        let protocol = self.mode.as_str();
        trace!(
            message = "Bytes sent.",
            byte_size = %self.byte_size,
            %protocol,
        );
        counter!(
            "component_sent_bytes_total",
            "protocol" => protocol,
        )
        .increment(self.byte_size as u64);
    }
}

#[derive(Debug)]
pub struct SocketEventsSent {
    pub mode: SocketMode,
    pub count: u64,
    pub byte_size: JsonSize,
}

impl InternalEvent for SocketEventsSent {
    fn emit(self) {
        trace!(message = "Events sent.", count = %self.count, byte_size = %self.byte_size.get());
        counter!("component_sent_events_total", "mode" => self.mode.as_str()).increment(self.count);
        counter!("component_sent_event_bytes_total", "mode" => self.mode.as_str())
            .increment(self.byte_size.get() as u64);
    }
}

#[derive(Debug)]
pub struct SocketBindError<E> {
    pub mode: SocketMode,
    pub error: E,
}

impl<E: std::fmt::Display> InternalEvent for SocketBindError<E> {
    fn emit(self) {
        let mode = self.mode.as_str();
        error!(
            message = "Error binding socket.",
            error = %self.error,
            error_code = "socket_bind",
            error_type = error_type::IO_FAILED,
            stage = error_stage::RECEIVING,
            %mode,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "error_code" => "socket_bind",
            "error_type" => error_type::IO_FAILED,
            "stage" => error_stage::RECEIVING,
            "mode" => mode,
        )
        .increment(1);
    }
}

#[derive(Debug)]
pub struct SocketReceiveError<E> {
    pub mode: SocketMode,
    pub error: E,
}

impl<E: std::fmt::Display> InternalEvent for SocketReceiveError<E> {
    fn emit(self) {
        let mode = self.mode.as_str();
        error!(
            message = "Error receiving data.",
            error = %self.error,
            error_code = "socket_receive",
            error_type = error_type::READER_FAILED,
            stage = error_stage::RECEIVING,
            %mode,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "error_code" => "socket_receive",
            "error_type" => error_type::READER_FAILED,
            "stage" => error_stage::RECEIVING,
            "mode" => mode,
        )
        .increment(1);
    }
}

#[derive(Debug)]
pub struct SocketSendError<E> {
    pub mode: SocketMode,
    pub error: E,
}

impl<E: std::fmt::Display> InternalEvent for SocketSendError<E> {
    fn emit(self) {
        let mode = self.mode.as_str();
        let reason = "Error sending data.";
        error!(
            message = reason,
            error = %self.error,
            error_code = "socket_send",
            error_type = error_type::WRITER_FAILED,
            stage = error_stage::SENDING,
            %mode,
            internal_log_rate_limit = true,
        );
        counter!(
            "component_errors_total",
            "error_code" => "socket_send",
            "error_type" => error_type::WRITER_FAILED,
            "stage" => error_stage::SENDING,
            "mode" => mode,
        )
        .increment(1);

        emit!(ComponentEventsDropped::<UNINTENTIONAL> { count: 1, reason });
    }
}