Skip to content
Open
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
3 changes: 3 additions & 0 deletions changelog.d/27295_batch_encode_discard_count.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Sinks that encode a batch of events now report every event discarded when one event fails to encode. Previously only the failing event was counted, so the remaining events in the batch were lost from both `component_sent_events_total` and `component_discarded_events_total`.

authors: Goldloli
13 changes: 9 additions & 4 deletions src/sinks/http/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
event::Event,
sinks::{
prelude::*,
util::encoding::{Encoder as SinkEncoder, write_all},
util::encoding::{Encoder as SinkEncoder, emit_dropped, write_all},
},
};

Expand Down Expand Up @@ -69,9 +69,14 @@ impl SinkEncoder<Vec<Event>> for HttpEncoder {

byte_size.add_event(&event, event.estimated_json_encoded_size_of());

encoder
.encode(event, &mut body)
.map_err(|_| io::Error::other("unable to encode event"))?;
if encoder.encode(event, &mut body).is_err() {
// Returning the error discards the whole batch, so every event in it is
// lost, not just the one that failed. The codec reports that one; report
// the rest here, otherwise they are counted in neither
// `component_sent_events_total` nor `component_discarded_events_total`.
emit_dropped(n_events - 1);
return Err(io::Error::other("unable to encode event"));
}
}

match (self.encoder.serializer(), self.encoder.framer()) {
Expand Down
51 changes: 50 additions & 1 deletion src/sinks/http/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ use vector_lib::{
stream::batcher::limiter::ItemBatchSize as _,
};

use vrl::event_path;
use vrl::{
event_path,
value::{KeyString, ObjectMap},
};

use super::{
batch::HttpBatchSizer,
Expand Down Expand Up @@ -203,6 +206,52 @@ fn http_native_json_batch_sizing_does_not_report_dropped_events() {
}
}

/// A failing event aborts the whole batch, so the events that were already
/// encoded must be reported as dropped too.
#[test]
fn http_encode_failure_counts_every_discarded_event() {
init_test();
let controller = Controller::get().unwrap();
controller.reset();

let config = indoc::indoc! {r#"
uri: "http://127.0.0.1:8080"
encoding:
codec: native_json
framing:
method: newline_delimited
"#};
let config: HttpSinkConfig = serde_yaml::from_str(config).unwrap();
let encoder = HttpEncoder::new(
config.build_encoder().unwrap(),
config.encoding.transformer(),
String::new(),
String::new(),
);

// Nesting past the serializer's limit makes the third event fail to encode.
let too_deep = (0..41).fold(Value::from("leaf"), |value, _| Value::Array(vec![value]));
let input = vec![
Event::Log(LogEvent::from("first")),
Event::Log(LogEvent::from("second")),
Event::Log(LogEvent::from(ObjectMap::from([(
KeyString::from("bad"),
too_deep,
)]))),
];

assert!(encoder.encode_input(input, &mut Vec::new()).is_err());

let metrics = controller.capture_metrics();
let discarded = metrics
.iter()
.find(|metric| metric.name() == "component_discarded_events_total")
.expect("the discarded events counter must be incremented");
// All three events are lost: the codec reports the failing one and the
// encoder reports the other two.
assert_eq!(discarded.value(), &MetricValue::Counter { value: 3.0 });
}

#[test]
fn http_validates_normal_headers() {
let config = indoc::indoc! {r#"
Expand Down
88 changes: 76 additions & 12 deletions src/sinks/util/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use vector_lib::{
EstimatedJsonEncodedSizeOf,
codecs::{Transformer, encoding::Framer, internal_events::EncoderWriteError},
config::telemetry,
internal_event::emit,
internal_event::{ComponentEventsDropped, UNINTENTIONAL, emit},
request_metadata::GroupedCountByteSize,
};

Expand All @@ -34,7 +34,8 @@ impl Encoder<Vec<Event>> for (Transformer, vector_lib::codecs::Encoder<Framer>)
) -> io::Result<(usize, GroupedCountByteSize)> {
let mut encoder = self.1.clone();
let mut bytes_written = 0;
let mut n_events_pending = events.len();
let n_events = events.len();
let mut n_events_pending = n_events;
let is_empty = events.is_empty();
let batch_prefix = encoder.batch_prefix();
write_all(writer, n_events_pending, batch_prefix)?;
Expand All @@ -50,19 +51,21 @@ impl Encoder<Vec<Event>> for (Transformer, vector_lib::codecs::Encoder<Framer>)
byte_size.add_event(&event, event.estimated_json_encoded_size_of());

let mut bytes = BytesMut::new();
match (position, encoder.framer()) {
let result = match (position, encoder.framer()) {
(position, Framer::CharacterDelimited(_) | Framer::NewlineDelimited(_))
if position.is_last() =>
{
encoder
.serialize(event, &mut bytes)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
}
_ => {
encoder
.encode(event, &mut bytes)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
encoder.serialize(event, &mut bytes)
}
_ => encoder.encode(event, &mut bytes),
};
if let Err(error) = result {
// Returning the error discards the whole batch, so every event in it is
// lost, not just the one that failed. The codec reports that one; report
// the rest here, otherwise they are counted in neither
// `component_sent_events_total` nor `component_discarded_events_total`.
emit_dropped(n_events - 1);
return Err(io::Error::new(io::ErrorKind::InvalidData, error));
}
write_all(writer, n_events_pending, &bytes)?;
bytes_written += bytes.len();
Expand Down Expand Up @@ -164,6 +167,20 @@ impl Encoder<Vec<Event>> for (Transformer, vector_lib::codecs::EncoderKind) {
}
}

/// Emits an `EventsDropped` event for events that are neither sent nor reported
/// by the codec that failed.
///
/// A batch encode abort drops every event in the batch, but the codec only
/// reports the single event whose encode call returned the error.
pub fn emit_dropped(count: usize) {
if count > 0 {
emit(ComponentEventsDropped::<UNINTENTIONAL> {
count,
reason: "Failed to encode events in batch.",
});
}
}

/// Write the buffer to the writer. If the operation fails, emit an internal event which complies with the
/// instrumentation spec- as this necessitates both an error and an `EventsDropped` event.
///
Expand Down Expand Up @@ -234,7 +251,7 @@ mod tests {
NewlineDelimitedEncoder, TextSerializerConfig,
encoding::{ProtobufSerializerConfig, ProtobufSerializerOptions},
},
event::LogEvent,
event::{LogEvent, MetricValue},
internal_event::CountByteSize,
json_size::JsonSize,
};
Expand Down Expand Up @@ -626,4 +643,51 @@ mod tests {
contains_name_once("ComponentEventsDropped")
.expect("ComponentEventsDropped should be emitted by the wrapper");
}

/// A failing event aborts the whole batch, so the events that were already
/// encoded must be reported as dropped too.
#[test]
fn test_encode_batch_failure_counts_every_discarded_event() {
use vector_lib::metrics::Controller;

crate::test_util::trace_init();
vector_lib::metrics::init_test();
let controller = Controller::get().unwrap();
controller.reset();

let encoding = (
Transformer::default(),
vector_lib::codecs::Encoder::<Framer>::new(
NewlineDelimitedEncoder::default().into(),
vector_lib::codecs::NativeJsonSerializerConfig
.build()
.into(),
),
);

// Nesting past the native JSON serializer's limit makes this event fail
// to encode.
let too_deep = (0..41).fold(Value::from("leaf"), |value, _| Value::Array(vec![value]));
let input = vec![
Event::Log(LogEvent::from("first")),
Event::Log(LogEvent::from("second")),
Event::Log(LogEvent::from(ObjectMap::from([(
KeyString::from("bad"),
too_deep,
)]))),
];

let mut writer = Vec::new();
let result = encoding.encode_input(input, &mut writer);
assert!(result.is_err(), "the batch must fail to encode");

let metrics = controller.capture_metrics();
let discarded = metrics
.iter()
.find(|metric| metric.name() == "component_discarded_events_total")
.expect("the discarded events counter must be incremented");
// All three events are lost: the codec reports the failing one and this
// wrapper reports the other two.
assert_eq!(discarded.value(), &MetricValue::Counter { value: 3.0 });
}
}
Loading