Skip to content

Commit

Permalink
fix: Catch KeyError's when processing events, fix transforms in file-…
Browse files Browse the repository at this point in the history
…to-file
  • Loading branch information
bmtcril committed Jun 6, 2023
1 parent df9f6be commit 1050fc3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def parse_json_event(line):
parsed["timestamp"] = parsed["time"]

return parsed
except (AttributeError, JSONDecodeError) as e:
except (AttributeError, JSONDecodeError, KeyError) as e:
log.error("EXCEPTION!!!")
log.error(type(e))
log.error(e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def queue(self, event):
def send(self):
"""
Send to the LRS if we're configured for that, otherwise a no-op.
Events are converted to the output xAPI / Caliper format in the router.
"""
if self.destination == "LRS":
print(f"Sending {len(self.event_queue)} events to LRS...")
Expand All @@ -114,6 +116,8 @@ def send(self):
def store(self):
"""
Store to a libcloud destination if we're configured for that.
Events are converted to the output xAPI / Caliper format here before being saved.
"""
if self.destination == "LRS":
print("Store is being called on an LRS destination, skipping.")
Expand All @@ -130,7 +134,8 @@ def store(self):

out = BytesIO()
for event in self.event_queue:
out.write(str.encode(json.dumps(event)))
transformed_event = self.router.processors[0].process(event)
out.write(str.encode(json.dumps(transformed_event)))
out.write(str.encode("\n"))
out.seek(0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,11 @@ def test_transform_command(command_opts, mock_common_calls, caplog, capsys):
mm.return_value.download_object_as_stream = get_raw_log_stream
mock_libcloud_get_driver.return_value = mm

# Fake a router mapping so some events in the log are actually processed
mm2 = MagicMock()
# Fake a router mapping so some events in the log are actually processed
mm2.registry.mapping = {"problem_check": 1}
# Fake a process response that can be serialized to json
mm2.process.return_value = {"foo": "bar"}
mock_eventsrouter.return_value.processors = [mm2]

call_command(
Expand Down

0 comments on commit 1050fc3

Please sign in to comment.