forked from vectordotdev/vector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket.rs
More file actions
220 lines (198 loc) · 6.08 KB
/
websocket.rs
File metadata and controls
220 lines (198 loc) · 6.08 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#![allow(dead_code)] // TODO requires optional feature compilation
use std::{
error::Error,
fmt::{Debug, Display, Formatter, Result},
};
use metrics::{counter, histogram};
use tokio_tungstenite::tungstenite::error::Error as TungsteniteError;
use vector_common::{
internal_event::{error_stage, error_type},
json_size::JsonSize,
};
use vector_lib::NamedInternalEvent;
use vector_lib::internal_event::InternalEvent;
pub const PROTOCOL: &str = "websocket";
#[derive(Debug, NamedInternalEvent)]
pub struct WebSocketConnectionEstablished;
impl InternalEvent for WebSocketConnectionEstablished {
fn emit(self) {
debug!(message = "Connected.");
counter!("connection_established_total").increment(1);
}
}
#[derive(Debug, NamedInternalEvent)]
pub struct WebSocketConnectionFailedError {
pub error: Box<dyn Error>,
}
impl InternalEvent for WebSocketConnectionFailedError {
fn emit(self) {
error!(
message = "WebSocket connection failed.",
error = %self.error,
error_code = "websocket_connection_error",
error_type = error_type::CONNECTION_FAILED,
stage = error_stage::SENDING,
);
counter!(
"component_errors_total",
"protocol" => PROTOCOL,
"error_code" => "websocket_connection_failed",
"error_type" => error_type::CONNECTION_FAILED,
"stage" => error_stage::SENDING,
)
.increment(1);
}
}
#[derive(Debug, NamedInternalEvent)]
pub struct WebSocketConnectionShutdown;
impl InternalEvent for WebSocketConnectionShutdown {
fn emit(self) {
warn!(message = "Closed by the server.");
counter!("connection_shutdown_total").increment(1);
}
}
#[derive(Debug, NamedInternalEvent)]
pub struct WebSocketConnectionError {
pub error: tokio_tungstenite::tungstenite::Error,
}
impl InternalEvent for WebSocketConnectionError {
fn emit(self) {
error!(
message = "WebSocket connection error.",
error = %self.error,
error_code = "websocket_connection_error",
error_type = error_type::WRITER_FAILED,
stage = error_stage::SENDING,
);
counter!(
"component_errors_total",
"protocol" => PROTOCOL,
"error_code" => "websocket_connection_error",
"error_type" => error_type::WRITER_FAILED,
"stage" => error_stage::SENDING,
)
.increment(1);
}
}
#[allow(dead_code)]
#[derive(Debug, Copy, Clone)]
pub enum WebSocketKind {
Ping,
Pong,
Text,
Binary,
Close,
Frame,
}
impl Display for WebSocketKind {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
write!(f, "{self:?}")
}
}
#[derive(Debug, NamedInternalEvent)]
pub struct WebSocketBytesReceived<'a> {
pub byte_size: usize,
pub url: &'a str,
pub protocol: &'static str,
pub kind: WebSocketKind,
}
impl InternalEvent for WebSocketBytesReceived<'_> {
fn emit(self) {
trace!(
message = "Bytes received.",
byte_size = %self.byte_size,
url = %self.url,
protocol = %self.protocol,
kind = %self.kind
);
let counter = counter!(
"component_received_bytes_total",
"url" => self.url.to_string(),
"protocol" => self.protocol,
"kind" => self.kind.to_string()
);
counter.increment(self.byte_size as u64);
}
}
#[derive(Debug, NamedInternalEvent)]
pub struct WebSocketMessageReceived<'a> {
pub count: usize,
pub byte_size: JsonSize,
pub url: &'a str,
pub protocol: &'static str,
pub kind: WebSocketKind,
}
impl InternalEvent for WebSocketMessageReceived<'_> {
fn emit(self) {
trace!(
message = "Events received.",
count = %self.count,
byte_size = %self.byte_size,
url = %self.url,
protcol = %self.protocol,
kind = %self.kind
);
let histogram = histogram!("component_received_events_count");
histogram.record(self.count as f64);
let counter = counter!(
"component_received_events_total",
"uri" => self.url.to_string(),
"protocol" => PROTOCOL,
"kind" => self.kind.to_string()
);
counter.increment(self.count as u64);
let counter = counter!(
"component_received_event_bytes_total",
"url" => self.url.to_string(),
"protocol" => PROTOCOL,
"kind" => self.kind.to_string()
);
counter.increment(self.byte_size.get() as u64);
}
}
#[derive(Debug, NamedInternalEvent)]
pub struct WebSocketReceiveError<'a> {
pub error: &'a TungsteniteError,
}
impl InternalEvent for WebSocketReceiveError<'_> {
fn emit(self) {
error!(
message = "Error receiving message from websocket.",
error = %self.error,
error_code = "websocket_receive_error",
error_type = error_type::CONNECTION_FAILED,
stage = error_stage::PROCESSING,
);
counter!(
"component_errors_total",
"protocol" => PROTOCOL,
"error_code" => "websocket_receive_error",
"error_type" => error_type::CONNECTION_FAILED,
"stage" => error_stage::PROCESSING,
)
.increment(1);
}
}
#[derive(Debug, NamedInternalEvent)]
pub struct WebSocketSendError<'a> {
pub error: &'a TungsteniteError,
}
impl InternalEvent for WebSocketSendError<'_> {
fn emit(self) {
error!(
message = "Error sending message to websocket.",
error = %self.error,
error_code = "websocket_send_error",
error_type = error_type::CONNECTION_FAILED,
stage = error_stage::PROCESSING,
);
counter!(
"component_errors_total",
"protocol" => PROTOCOL,
"error_code" => "websocket_send_error",
"error_type" => error_type::CONNECTION_FAILED,
"stage" => error_stage::PROCESSING,
)
.increment(1);
}
}