Skip to content

Commit

Permalink
making format analyzer not flag stack traces
Browse files Browse the repository at this point in the history
  • Loading branch information
kitrady committed Jul 31, 2024
1 parent 1acf11b commit 9d20151
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/alogamous/format_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ class FormatAnalyzer(analyzer.Analyzer):
def __init__(self, line_parser):
self.parser = line_parser
self.startup_block = False
self.stack_trace = False
self.un_formated_lines = []

def read_log_line(self, line):
line_type = self.parser.parse(line)["type"]
if self.startup_block is False:
if line.count("Traceback") == 1:
self.stack_trace = True
elif self.stack_trace:
if line_type == (log_line_parser.LineType.LOG_LINE or log_line_parser.LineType.HEADER_LINE):
self.stack_trace = False
elif self.startup_block is False and self.stack_trace is False:
if line_type == log_line_parser.LineType.UNSTRUCTURED_LINE:
self.un_formated_lines.append(line)
elif line_type == log_line_parser.LineType.HEADER_LINE:
Expand Down
49 changes: 49 additions & 0 deletions tests/format_analyzer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,52 @@ def test_format_analyzer_with_good_lines():
format_checker.read_log_line(line)
format_checker.report(out_stream)
assert out_stream.getvalue() == "All lines conform to log line format"


def test_format_with_stack_trace():
parser = log_line_parser.LogLineParser(
["datetime", "source", "level", "message"], " - ", "===================================================="
)
format_checker = format_analyzer.FormatAnalyzer(parser)
in_stream = """2024-07-23 21:13:53,862 - root - INFO - Closing client connection.
Hello I am a bad log line
2024-07-23 21:13:53,862 - root - INFO - Closing client connection.
Traceback (most recent call last):
File "<frozen runpy>", line 1938, in _run_module_as_main
File "<frozen runpy>", line 88, in _run_code
File ".build/2811/execution/execution_service", line 973, in <module>
app.run(life_cycle_runner.run, life_cycle_runner.stop)
File ".build/2811/app/application.py", line 219, in run
run(self.start(my_date, main, stop))
File ".build/2811/.venv/lib/python3.11/runners.py", line 190, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File ".build/2811/.venv/lib/python3.11/runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File ".build/2811/.venv/lib/python3.11/events.py", line 653, in run_until_done
return future.result()
^^^^^^^^^^^^^^^
File "2811/app/application.py", line 421, in start
await self.task
File "2811/messages/app/runner.py", line 449, in run
await asyncio.gather(*self.running_tasks)
File "2811/messages/processor.py", line 340, in run
await self.dispatch(message)
File ".build/2811/execution/execution_service", line 1315, in market_test
if symbol and obj.region != 'NORTHAMERICA'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'region'
2024-07-23 21:13:53,862 - root - INFO - Closing client connection.
Hello I am a bad log line"""
out_stream = StringIO()
for line in in_stream.splitlines():
format_checker.read_log_line(line)
format_checker.report(out_stream)
assert (
out_stream.getvalue()
== """
Lines that do not conform to log format:
- Hello I am a bad log line
- Hello I am a bad log line"""
)

0 comments on commit 9d20151

Please sign in to comment.