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(observability): add all events that are being encoded #18289

Merged
merged 1 commit into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/vector-common/src/request_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl GroupedCountByteSize {
/// Returns a `HashMap` of tags => event counts for when we are tracking by tags.
/// Returns `None` if we are not tracking by tags.
#[must_use]
#[cfg(test)]
#[cfg(any(test, feature = "test"))]
pub fn sizes(&self) -> Option<&HashMap<TaggedEventsSent, CountByteSize>> {
match self {
Self::Tagged { sizes } => Some(sizes),
Expand All @@ -71,8 +71,8 @@ impl GroupedCountByteSize {

/// Returns a single count for when we are not tracking by tags.
#[must_use]
#[cfg(test)]
fn size(&self) -> Option<CountByteSize> {
#[cfg(any(test, feature = "test"))]
pub fn size(&self) -> Option<CountByteSize> {
match self {
Self::Tagged { .. } => None,
Self::Untagged { size } => Some(*size),
Expand Down
175 changes: 99 additions & 76 deletions src/sinks/util/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ impl Encoder<Vec<Event>> for (Transformer, crate::codecs::Encoder<Framer>) {
if let Some(last) = events.pop() {
for mut event in events {
self.0.transform(&mut event);

// Ensure the json size is calculated after any fields have been removed
// by the transformer.
byte_size.add_event(&event, event.estimated_json_encoded_size_of());

let mut bytes = BytesMut::new();
encoder
.encode(event, &mut bytes)
Expand Down Expand Up @@ -150,6 +155,7 @@ mod tests {
CharacterDelimitedEncoder, JsonSerializerConfig, NewlineDelimitedEncoder,
TextSerializerConfig,
};
use vector_common::{internal_event::CountByteSize, json_size::JsonSize};
use vector_core::event::LogEvent;
use vrl::value::Value;

Expand All @@ -166,10 +172,14 @@ mod tests {
);

let mut writer = Vec::new();
let (written, _json_size) = encoding.encode_input(vec![], &mut writer).unwrap();
let (written, json_size) = encoding.encode_input(vec![], &mut writer).unwrap();
assert_eq!(written, 2);

assert_eq!(String::from_utf8(writer).unwrap(), "[]");
assert_eq!(
CountByteSize(0, JsonSize::zero()),
json_size.size().unwrap()
);
}

#[test]
Expand All @@ -183,18 +193,21 @@ mod tests {
);

let mut writer = Vec::new();
let (written, _json_size) = encoding
.encode_input(
vec![Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value"),
)])))],
&mut writer,
)
.unwrap();
let input = vec![Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value"),
)])))];

let input_json_size = input
.iter()
.map(|event| event.estimated_json_encoded_size_of())
.sum::<JsonSize>();

let (written, json_size) = encoding.encode_input(input, &mut writer).unwrap();
assert_eq!(written, 17);

assert_eq!(String::from_utf8(writer).unwrap(), r#"[{"key":"value"}]"#);
assert_eq!(CountByteSize(1, input_json_size), json_size.size().unwrap());
}

#[test]
Expand All @@ -207,32 +220,36 @@ mod tests {
),
);

let input = vec![
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value1"),
)]))),
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value2"),
)]))),
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value3"),
)]))),
];

let input_json_size = input
.iter()
.map(|event| event.estimated_json_encoded_size_of())
.sum::<JsonSize>();

let mut writer = Vec::new();
let (written, _json_size) = encoding
.encode_input(
vec![
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value1"),
)]))),
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value2"),
)]))),
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value3"),
)]))),
],
&mut writer,
)
.unwrap();
let (written, json_size) = encoding.encode_input(input, &mut writer).unwrap();
assert_eq!(written, 52);

assert_eq!(
String::from_utf8(writer).unwrap(),
r#"[{"key":"value1"},{"key":"value2"},{"key":"value3"}]"#
);

assert_eq!(CountByteSize(3, input_json_size), json_size.size().unwrap());
}

#[test]
Expand All @@ -246,10 +263,14 @@ mod tests {
);

let mut writer = Vec::new();
let (written, _json_size) = encoding.encode_input(vec![], &mut writer).unwrap();
let (written, json_size) = encoding.encode_input(vec![], &mut writer).unwrap();
assert_eq!(written, 0);

assert_eq!(String::from_utf8(writer).unwrap(), "");
assert_eq!(
CountByteSize(0, JsonSize::zero()),
json_size.size().unwrap()
);
}

#[test]
Expand All @@ -263,18 +284,20 @@ mod tests {
);

let mut writer = Vec::new();
let (written, _json_size) = encoding
.encode_input(
vec![Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value"),
)])))],
&mut writer,
)
.unwrap();
let input = vec![Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value"),
)])))];
let input_json_size = input
.iter()
.map(|event| event.estimated_json_encoded_size_of())
.sum::<JsonSize>();

let (written, json_size) = encoding.encode_input(input, &mut writer).unwrap();
assert_eq!(written, 15);

assert_eq!(String::from_utf8(writer).unwrap(), r#"{"key":"value"}"#);
assert_eq!(CountByteSize(1, input_json_size), json_size.size().unwrap());
}

#[test]
Expand All @@ -288,31 +311,33 @@ mod tests {
);

let mut writer = Vec::new();
let (written, _json_size) = encoding
.encode_input(
vec![
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value1"),
)]))),
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value2"),
)]))),
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value3"),
)]))),
],
&mut writer,
)
.unwrap();
let input = vec![
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value1"),
)]))),
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value2"),
)]))),
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value3"),
)]))),
];
let input_json_size = input
.iter()
.map(|event| event.estimated_json_encoded_size_of())
.sum::<JsonSize>();

let (written, json_size) = encoding.encode_input(input, &mut writer).unwrap();
assert_eq!(written, 50);

assert_eq!(
String::from_utf8(writer).unwrap(),
"{\"key\":\"value1\"}\n{\"key\":\"value2\"}\n{\"key\":\"value3\"}"
);
assert_eq!(CountByteSize(3, input_json_size), json_size.size().unwrap());
}

#[test]
Expand All @@ -323,18 +348,17 @@ mod tests {
);

let mut writer = Vec::new();
let (written, _json_size) = encoding
.encode_input(
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value"),
)]))),
&mut writer,
)
.unwrap();
let input = Event::Log(LogEvent::from(BTreeMap::from([(
String::from("key"),
Value::from("value"),
)])));
let input_json_size = input.estimated_json_encoded_size_of();

let (written, json_size) = encoding.encode_input(input, &mut writer).unwrap();
assert_eq!(written, 15);

assert_eq!(String::from_utf8(writer).unwrap(), r#"{"key":"value"}"#);
assert_eq!(CountByteSize(1, input_json_size), json_size.size().unwrap());
}

#[test]
Expand All @@ -345,17 +369,16 @@ mod tests {
);

let mut writer = Vec::new();
let (written, _json_size) = encoding
.encode_input(
Event::Log(LogEvent::from(BTreeMap::from([(
String::from("message"),
Value::from("value"),
)]))),
&mut writer,
)
.unwrap();
let input = Event::Log(LogEvent::from(BTreeMap::from([(
String::from("message"),
Value::from("value"),
)])));
let input_json_size = input.estimated_json_encoded_size_of();

let (written, json_size) = encoding.encode_input(input, &mut writer).unwrap();
assert_eq!(written, 5);

assert_eq!(String::from_utf8(writer).unwrap(), r#"value"#);
assert_eq!(CountByteSize(1, input_json_size), json_size.size().unwrap());
}
}