Skip to content

Commit

Permalink
Refactor paasta logs filtering for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
yaroliakh committed Nov 22, 2024
1 parent f65acaf commit eb81d09
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
56 changes: 28 additions & 28 deletions paasta_tools/cli/cmds/logs.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,18 +294,21 @@ def paasta_log_line_passes_filter(
except ValueError:
log.debug("Trouble parsing line as json. Skipping. Line: %r" % line)
return False
timestamp = isodate.parse_datetime(parsed_line.get("timestamp"))
if not check_timestamp_in_range(timestamp, start_time, end_time):
return False
return (
(parsed_line.get("level") is None or parsed_line.get("level") in levels)

if (
(instances is None or parsed_line.get("instance") in instances)
and (parsed_line.get("level") is None or parsed_line.get("level") in levels)
and parsed_line.get("component") in components
and (
parsed_line.get("cluster") in clusters
or parsed_line.get("cluster") == ANY_CLUSTER
)
and (instances is None or parsed_line.get("instance") in instances)
)
):
timestamp = isodate.parse_datetime(parsed_line.get("timestamp"))
if check_timestamp_in_range(timestamp, start_time, end_time):
return True
else:
return False


def paasta_app_output_passes_filter(
Expand All @@ -324,28 +327,25 @@ def paasta_app_output_passes_filter(
except ValueError:
log.debug("Trouble parsing line as json. Skipping. Line: %r" % line)
return False
try:
timestamp = isodate.parse_datetime(parsed_line.get("timestamp"))
# https://github.com/gweis/isodate/issues/53
except ValueError:
return True
except AttributeError:
# Timestamp might be missing. We had an issue where OTel was splitting overly long log lines
# and not including timestamps in the resulting log records (OBSPLAT-2216).
# Although this was then fixed in OTel, we should not rely on timestamps being present,
# as the format cannot be guaranteed.
return False
if not check_timestamp_in_range(timestamp, start_time, end_time):
return False
return (
parsed_line.get("component") in components
and (
parsed_line.get("cluster") in clusters
or parsed_line.get("cluster") == ANY_CLUSTER
)
and (instances is None or parsed_line.get("instance") in instances)

if (
(instances is None or parsed_line.get("instance") in instances)
and parsed_line.get("cluster") in clusters
and parsed_line.get("component") in components
and (pods is None or parsed_line.get("pod_name") in pods)
)
):
try:
timestamp = isodate.parse_datetime(parsed_line.get("timestamp"))
except AttributeError:
# Timestamp might be missing. We had an issue where OTel was splitting overly long log lines
# and not including timestamps in the resulting log records (OBSPLAT-2216).
# Although this was then fixed in OTel, we should not rely on timestamps being present,
# as the format cannot be guaranteed.
return False
if check_timestamp_in_range(timestamp, start_time, end_time):
return True
else:
return False


def extract_utc_timestamp_from_log_line(line: str) -> datetime.datetime:
Expand Down
2 changes: 1 addition & 1 deletion requirements-minimal.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ humanfriendly
humanize >= 0.5.1
inotify >= 0.2.8
ipaddress >= 1.0.22
isodate >= 0.5.0
isodate >= 0.7.2
jsonschema[format]
kazoo >= 2.0.0
# the upper-bound here is mainly for things that use paasta-tools as a library and don't benefit
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ hupper==1.0
idna==2.6
inotify==0.2.8
ipaddress==1.0.22
isodate==0.6.0
isodate==0.7.2
itsdangerous==2.0.1
Jinja2==2.11.3
jinja2-time==0.1.0
Expand Down

0 comments on commit eb81d09

Please sign in to comment.