Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(websocket sink): send encoded message as binary frame #18060

Merged
merged 9 commits into from
Jul 25, 2023
19 changes: 18 additions & 1 deletion src/sinks/websocket/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ impl WebSocketSink {
Ok(())
}

const fn should_encode_as_binary(&self) -> bool {
use codecs::encoding::Serializer::{
Avro, Csv, Gelf, Json, Logfmt, Native, NativeJson, RawMessage, Text,
};

match self.encoder.serializer() {
RawMessage(_) | Avro(_) | Native(_) => true,
Csv(_) | Logfmt(_) | Gelf(_) | Json(_) | Text(_) | NativeJson(_) => false,
}
}

async fn handle_events<I, WS, O>(
&mut self,
input: &mut I,
Expand All @@ -261,6 +272,7 @@ impl WebSocketSink {

let bytes_sent = register!(BytesSent::from(Protocol("websocket".into())));
let events_sent = register!(EventsSent::from(Output(None)));
let encode_as_binary = self.should_encode_as_binary();

loop {
let result = tokio::select! {
Expand Down Expand Up @@ -301,7 +313,12 @@ impl WebSocketSink {
Ok(()) => {
finalizers.update_status(EventStatus::Delivered);

let message = Message::text(String::from_utf8_lossy(&bytes));
let message = if encode_as_binary {
Message::binary(bytes)
}
else {
Message::text(String::from_utf8_lossy(&bytes))
};
let message_len = message.len();

ws_sink.send(message).await.map(|_| {
Expand Down