-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
ab38a76
e51c71b
5875595
df6d925
ef3d173
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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::*, | ||
}; | ||
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we also need to use the |
||
} | ||
}, | ||
_ => chrono::Utc::now() | ||
.timestamp_nanos_opt() | ||
.expect("Timestamp out of range"), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we move this |
||
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()); | ||
} |
There was a problem hiding this comment.
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?There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
vector/src/internal_events/splunk_hec.rs
Lines 55 to 77 in 8325300