Skip to content

Commit eb44262

Browse files
authored
Strengthen some Logger tests (#743)
1 parent 572f76c commit eb44262

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

lib/sentry/logger_handler.ex

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,25 @@ defmodule Sentry.LoggerHandler do
462462
%{genserver_state: state, last_message: last_message}
463463
end
464464

465+
# Sometimes there's an extra sneaky [] in there.
466+
defp extra_info_from_message([
467+
[
468+
"GenServer ",
469+
_pid,
470+
" terminating",
471+
_reason,
472+
[],
473+
"\nLast message",
474+
_from,
475+
": ",
476+
last_message
477+
],
478+
"\nState: ",
479+
state | _rest
480+
]) do
481+
%{genserver_state: state, last_message: last_message}
482+
end
483+
465484
defp extra_info_from_message(_message) do
466485
%{}
467486
end
@@ -471,6 +490,39 @@ defmodule Sentry.LoggerHandler do
471490
# message, and a treasure trove of stuff. If we cannot parse the message, such is life
472491
# and we just report it as is.
473492

493+
defp try_to_parse_message_or_just_report_it(
494+
[
495+
[
496+
"GenServer ",
497+
inspected_pid,
498+
" terminating",
499+
chardata_reason,
500+
_whatever_this_is = [],
501+
"\nLast message",
502+
[" (from ", inspected_sender_pid, ")"],
503+
": ",
504+
inspected_last_message
505+
],
506+
"\nState: ",
507+
inspected_state | _
508+
],
509+
sentry_opts,
510+
config
511+
) do
512+
string_reason = chardata_reason |> :unicode.characters_to_binary() |> String.trim()
513+
514+
sentry_opts =
515+
sentry_opts
516+
|> Keyword.put(:interpolation_parameters, [inspected_pid])
517+
|> add_extra_to_sentry_opts(%{
518+
pid_which_sent_last_message: inspected_sender_pid,
519+
last_message: inspected_last_message,
520+
genserver_state: inspected_state
521+
})
522+
523+
capture(:message, "GenServer %s terminating: #{string_reason}", sentry_opts, config)
524+
end
525+
474526
defp try_to_parse_message_or_just_report_it(
475527
[
476528
[

test/sentry/logger_handler_test.exs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,25 @@ defmodule Sentry.LoggerHandlerTest do
8888
assert stacktrace_frame.filename == "test/support/example_plug_application.ex"
8989
end
9090

91+
test "TODO", %{sender_ref: ref} do
92+
start_supervised!(Sentry.ExamplePlugApplication, restart: :temporary)
93+
:hackney.get("http://127.0.0.1:8003/error_route", [], "", [])
94+
assert_receive {^ref, event}, 1000
95+
assert event.original_exception == %RuntimeError{message: "Error"}
96+
end
97+
9198
@tag handler_config: %{excluded_domains: []}
9299
test "sends two errors when a Plug process crashes if cowboy domain is not excluded",
93100
%{sender_ref: ref} do
94101
start_supervised!(Sentry.ExamplePlugApplication, restart: :temporary)
95102

96103
:hackney.get("http://127.0.0.1:8003/error_route", [], "", [])
97104

98-
assert_receive {^ref, _event}, 1000
105+
assert_receive {^ref, event}, 1000
106+
assert event.original_exception == %RuntimeError{message: "Error"}
107+
108+
assert_receive {^ref, second_event}, 1000
109+
assert second_event.original_exception == %RuntimeError{message: "Error"}
99110
end
100111

101112
@tag handler_config: %{excluded_domains: []}
@@ -109,6 +120,9 @@ defmodule Sentry.LoggerHandlerTest do
109120
end
110121
end
111122

123+
describe "with Bandit" do
124+
end
125+
112126
describe "with capture_log_messages: true" do
113127
@tag handler_config: %{capture_log_messages: true}
114128
test "sends error messages by default", %{sender_ref: ref} do

0 commit comments

Comments
 (0)