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(loki sinks): Fix loki event timestamp out of range panic #20780

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions src/internal_events/loki.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,18 @@ impl InternalEvent for LokiOutOfOrderEventRewritten {
counter!("rewritten_timestamp_events_total", self.count as u64);
}
}

#[derive(Debug)]
pub struct LokiEventTimestampOutOfRangeError;

impl InternalEvent for LokiEventTimestampOutOfRangeError {
fn emit(self) {
error!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this a warn! since it doesn't actually inhibit processing? Also, what happens when we send an event to Loki without a timestamp? Does it just use the current timestamp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to Loki api, timestamp is must.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, I missed that you are returning early. In that case we'd want to emit the metrics for errors and discarded events. See an example here:

impl<'a> InternalEvent for SplunkInvalidMetricReceivedError<'a> {
fn emit(self) {
error!(
message = "Invalid metric received.",
error = ?self.error,
error_type = error_type::INVALID_METRIC,
stage = error_stage::PROCESSING,
value = ?self.value,
kind = ?self.kind,
internal_log_rate_limit = true,
);
counter!(
"component_errors_total", 1,
"error_type" => error_type::INVALID_METRIC,
"stage" => error_stage::PROCESSING,
);
counter!(
"component_discarded_events_total", 1,
"error_type" => error_type::INVALID_METRIC,
"stage" => error_stage::PROCESSING,
);
}
}

message = "Event timestamp out of range.",
reason = "out_of_range",
internal_log_rate_limit = true,
);

counter!("timestamp_out_of_range_total", 1,);
}
}
12 changes: 9 additions & 3 deletions src/sinks/loki/sink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::sinks::loki::event::LokiBatchEncoding;
use crate::{
http::{get_http_scheme_from_uri, HttpClient},
internal_events::{
LokiEventUnlabeledError, LokiOutOfOrderEventDroppedError, LokiOutOfOrderEventRewritten,
SinkRequestBuildError,
LokiEventTimestampOutOfRangeError, LokiEventUnlabeledError,
LokiOutOfOrderEventDroppedError, LokiOutOfOrderEventRewritten, SinkRequestBuildError,
},
sinks::prelude::*,
};
Expand Down Expand Up @@ -253,7 +253,13 @@ impl EventEncoder {
self.remove_label_fields(&mut event);

let timestamp = match event.as_log().get_timestamp() {
Some(Value::Timestamp(ts)) => ts.timestamp_nanos_opt().expect("Timestamp out of range"),
Some(Value::Timestamp(ts)) => match ts.timestamp_nanos_opt() {
Some(timestamp) => timestamp,
None => {
emit!(LokiEventTimestampOutOfRangeError);
return None;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we also need to use the finalizers here and explicitly nack the event via update_status like finalizers.update_status(EventStatus::Errored). Otherwise Vector will consider them to have been successfully processed.

}
},
_ => chrono::Utc::now()
.timestamp_nanos_opt()
.expect("Timestamp out of range"),
Expand Down
29 changes: 29 additions & 0 deletions src/sinks/loki/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,32 @@ async fn healthcheck_grafana_cloud() {
.await
.expect("healthcheck failed");
}

#[tokio::test]
async fn timestamp_out_of_range() {
let (config, cx) = load_sink::<LokiConfig>(
r#"
endpoint = "http://localhost:3100"
labels = {label1 = "{{ foo }}", label2 = "some-static-label", label3 = "{{ foo }}", "{{ foo }}" = "{{ foo }}"}
encoding.codec = "json"
"#,
)
.unwrap();
let client = config.build_client(cx).unwrap();
let mut sink = LokiSink::new(config, client).unwrap();

let mut e1 = LogEvent::from("hello world");
use vector_lib::config::log_schema;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we move this use statement to the beginning of this tests module?

if let Some(timestamp_key) = log_schema().timestamp_key_target_path() {
let date = chrono::NaiveDate::from_ymd_opt(1677, 9, 21)
.unwrap()
.and_hms_nano_opt(0, 12, 43, 145_224_191)
.unwrap()
.and_local_timezone(chrono::Utc)
.unwrap();
e1.insert(timestamp_key, date);
}
let e1 = Event::Log(e1);

assert!(sink.encoder.encode_event(e1).is_none());
}
Loading