Skip to content

Commit

Permalink
fix: add additional protective wrapping to crash log handling (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
btkostner authored Aug 8, 2023
1 parent aa1ddc0 commit f7aba76
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/logger_json/formatters/datadog_logger.ex
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ defmodule LoggerJSON.Formatters.DatadogLogger do
# errors to automatically be aggregated by error tracking.
defp format_process_crash(metadata) do
case Keyword.get(metadata, :crash_reason) do
{reason, stacktrace} ->
{reason, stacktrace} when is_list(stacktrace) ->
initial_call = Keyword.get(metadata, :initial_call)

json_map(
Expand All @@ -130,6 +130,20 @@ defmodule LoggerJSON.Formatters.DatadogLogger do
kind: format_exception_kind(reason)
)

# This should never happen unless you are manually faking
# a crash reason. Non the less, we've had errors appear
# in the past where `Exception.format_stacktrace/1` has
# not received a list of stacktrace entries.
{reason, not_a_stacktrace} ->
initial_call = Keyword.get(metadata, :initial_call)

json_map(
initial_call: format_initial_call(initial_call),
stack: inspect(not_a_stacktrace),
message: format_exception_message(reason),
kind: format_exception_kind(reason)
)

nil ->
nil
end
Expand Down
21 changes: 21 additions & 0 deletions test/unit/logger_json/formatters/datadog_logger_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,27 @@ defmodule LoggerJSONDatadogTest do
assert log["error"]["stack"] =~ "stacktrace test"
end

test "inspects any non stacktrace crash reason data" do
Logger.configure_backend(LoggerJSON, metadata: [:crash_reason])

Logger.metadata(
crash_reason: {
:exit,
{Task.Supervised, :stream, [5000]}
}
)

log =
fn -> Logger.debug("hello") end
|> capture_log()
|> Jason.decode!()

assert is_nil(log["error"]["initial_call"])
assert log["error"]["kind"] == "exit"
assert log["error"]["message"] == "exit"
assert log["error"]["stack"] =~ "{Task.Supervised, :stream, [5000]}"
end

test "logs erlang style crash reasons" do
Logger.configure_backend(LoggerJSON, metadata: [:crash_reason])
Logger.metadata(crash_reason: {:socket_closed_unexpectedly, []})
Expand Down

0 comments on commit f7aba76

Please sign in to comment.