-
Notifications
You must be signed in to change notification settings - Fork 116
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
Logs from different streams can arrive out of order #401
Changes from all commits
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ def initialize(parent_id:, container_name:, namespace:, context:, logger:) | |
def sync | ||
new_logs = fetch_latest | ||
return unless new_logs.present? | ||
@lines += deduplicate(new_logs) | ||
@lines += sort_and_deduplicate(new_logs) | ||
end | ||
|
||
def empty? | ||
|
@@ -60,18 +60,24 @@ def rfc3339_timestamp(time) | |
time.strftime("%FT%T.%N%:z") | ||
end | ||
|
||
def deduplicate(logs) | ||
deduped = [] | ||
check_for_duplicate = true | ||
def sort_and_deduplicate(logs) | ||
parsed_lines = logs.map { |line| split_timestamped_line(line) } | ||
sorted_lines = parsed_lines.sort do |(timestamp1, _msg1), (timestamp2, _msg2)| | ||
if timestamp1.nil? | ||
-1 | ||
elsif timestamp2.nil? | ||
1 | ||
else | ||
timestamp1 <=> timestamp2 | ||
end | ||
end | ||
|
||
logs.each do |line| | ||
timestamp, msg = split_timestamped_line(line) | ||
next if check_for_duplicate && likely_duplicate?(timestamp) | ||
check_for_duplicate = false # logs are ordered, so once we've seen a new one, assume all subsequent logs are new | ||
deduped = [] | ||
sorted_lines.each do |timestamp, msg| | ||
next if likely_duplicate?(timestamp) | ||
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. Hm. 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.
Exactly. We fetch logs (and then process them here) repeatedly, and since the API does not support subsecond granularity, we inevitably sometimes get the same log lines in more than one batch. |
||
@last_timestamp = timestamp if timestamp | ||
deduped << msg | ||
end | ||
|
||
deduped | ||
end | ||
|
||
|
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.
womp womp lol
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.
😆