Skip to content

Commit

Permalink
avoid expensive to_string() calls, now using 'get_message()'
Browse files Browse the repository at this point in the history
  • Loading branch information
pront committed Jul 19, 2023
1 parent 4affadf commit aec5937
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 31 deletions.
21 changes: 4 additions & 17 deletions lib/codecs/src/encoding/format/raw_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ use crate::encoding::format::common::get_serializer_schema_requirement;
use bytes::{BufMut, BytesMut};
use serde::{Deserialize, Serialize};
use tokio_util::codec::Encoder;
use vector_core::{
config::{log_schema, DataType},
event::Event,
schema,
};
use vrl::path::PathPrefix;
use vector_core::{config::DataType, event::Event, schema};

/// Config used to build a `RawMessageSerializer`.
#[derive(Debug, Clone, Default, Deserialize, Serialize)]
Expand Down Expand Up @@ -50,18 +45,10 @@ impl Encoder<Event> for RawMessageSerializer {
type Error = vector_common::Error;

fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
if let Some(message_key) = log_schema().message_key() {
let log = event.as_log();

if let Some(bytes) = log
.get_by_meaning(message_key.to_string().as_str())
.or_else(|| log.get((PathPrefix::Event, message_key)))
.map(|value| value.coerce_to_bytes())
{
buffer.put(bytes);
}
let log = event.as_log();
if let Some(bytes) = log.get_message().map(|value| value.coerce_to_bytes()) {
buffer.put(bytes);
}

Ok(())
}
}
Expand Down
17 changes: 3 additions & 14 deletions lib/codecs/src/encoding/format/text.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
use crate::encoding::format::common::get_serializer_schema_requirement;
use bytes::{BufMut, BytesMut};
use tokio_util::codec::Encoder;
use vector_core::{
config::{log_schema, DataType},
event::Event,
schema,
};
use vrl::path::PathPrefix;
use vector_core::{config::DataType, event::Event, schema};

use crate::MetricTagValues;

Expand Down Expand Up @@ -70,14 +65,8 @@ impl Encoder<Event> for TextSerializer {
fn encode(&mut self, event: Event, buffer: &mut BytesMut) -> Result<(), Self::Error> {
match event {
Event::Log(log) => {
if let Some(message_key) = log_schema().message_key() {
if let Some(bytes) = log
.get_by_meaning(message_key.to_string().as_str())
.or(log.get((PathPrefix::Event, message_key)))
.map(|value| value.coerce_to_bytes())
{
buffer.put(bytes);
}
if let Some(bytes) = log.get_message().map(|value| value.coerce_to_bytes()) {
buffer.put(bytes);
}
}
Event::Metric(mut metric) => {
Expand Down

0 comments on commit aec5937

Please sign in to comment.