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
15 changes: 14 additions & 1 deletion src/sinks/websocket/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ use crate::{
tls::{MaybeTlsSettings, MaybeTlsStream, TlsError},
};

use codecs::encoding::Serializer::{RawMessage, Avro, Native, Csv, Logfmt, Gelf, Json, Text, NativeJson};


#[derive(Debug, Snafu)]
#[snafu(visibility(pub))]
pub enum WebSocketError {
Expand Down Expand Up @@ -261,6 +264,11 @@ impl WebSocketSink {

let bytes_sent = register!(BytesSent::from(Protocol("websocket".into())));
let events_sent = register!(EventsSent::from(Output(None)));
let encode_as_binary = match self.encoder.serializer() {
RawMessage(_) | Avro(_) | Native(_) => true,
Csv(_) | Logfmt(_) | Gelf(_) | Json(_) | Text(_) | NativeJson(_) => false,
};


loop {
let result = tokio::select! {
Expand Down Expand Up @@ -301,7 +309,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
Loading