Skip to content

Commit bab2d19

Browse files
committed
🐛Gracefully handle no consumer_messages.log file
1 parent 890eb36 commit bab2d19

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

lib/sequin/logs/logs.ex

+34-30
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,40 @@ defmodule Sequin.Logs do
1616
end
1717

1818
defp get_logs_from_file(account_id, consumer_id, trace_id) do
19-
log_file_path()
20-
|> File.stream!()
21-
|> Stream.map(&String.trim/1)
22-
|> Stream.filter(&(&1 != ""))
23-
|> Stream.map(fn entry ->
24-
case Jason.decode(entry) do
25-
{:ok, log} ->
26-
log
27-
28-
{:error, error} ->
29-
Logger.error("Failed to decode log entry", entry: entry, error: error)
30-
nil
31-
end
32-
end)
33-
|> Stream.filter(& &1)
34-
|> Stream.filter(fn log ->
35-
log["account_id"] == account_id and log["consumer_id"] == consumer_id and log["trace_id"] == trace_id and
36-
log["console_logs"] == "consumer_message"
37-
end)
38-
|> Enum.map(fn log ->
39-
{:ok, timestamp, 0} = DateTime.from_iso8601(log["timestamp"])
40-
41-
%Log{
42-
timestamp: timestamp,
43-
status: log["level"],
44-
account_id: log["account_id"],
45-
message: log["message"]
46-
}
47-
end)
48-
|> then(&{:ok, &1})
19+
if File.exists?(log_file_path()) do
20+
log_file_path()
21+
|> File.stream!()
22+
|> Stream.map(&String.trim/1)
23+
|> Stream.filter(&(&1 != ""))
24+
|> Stream.map(fn entry ->
25+
case Jason.decode(entry) do
26+
{:ok, log} ->
27+
log
28+
29+
{:error, error} ->
30+
Logger.error("Failed to decode log entry", entry: entry, error: error)
31+
nil
32+
end
33+
end)
34+
|> Stream.filter(& &1)
35+
|> Stream.filter(fn log ->
36+
log["account_id"] == account_id and log["consumer_id"] == consumer_id and log["trace_id"] == trace_id and
37+
log["console_logs"] == "consumer_message"
38+
end)
39+
|> Enum.map(fn log ->
40+
{:ok, timestamp, 0} = DateTime.from_iso8601(log["timestamp"])
41+
42+
%Log{
43+
timestamp: timestamp,
44+
status: log["level"],
45+
account_id: log["account_id"],
46+
message: log["message"]
47+
}
48+
end)
49+
|> then(&{:ok, &1})
50+
else
51+
{:ok, []}
52+
end
4953
end
5054

5155
defp get_logs_from_datadog(account_id, consumer_id, trace_id) do

0 commit comments

Comments
 (0)