Skip to content

Commit

Permalink
catching exits prevents the program from exiting (surprise, surprise)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcdel committed Jul 12, 2024
1 parent c96129d commit a52f42b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 24 deletions.
4 changes: 4 additions & 0 deletions lib/open_telemetry_decorator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,20 @@ defmodule OpenTelemetryDecorator do
catch
:exit, :normal ->
O11y.set_attribute(:exit, :normal, namespace: prefix)
exit(:normal)

:exit, :shutdown ->
O11y.set_attribute(:exit, :shutdown, namespace: prefix)
exit(:shutdown)

:exit, {:shutdown, reason} ->
O11y.set_attributes(
[exit: :shutdown, shutdown_reason: reason],
namespace: prefix
)

exit({:shutdown, reason})

:exit, reason ->
O11y.set_error("exited: #{inspect(reason)}")
:erlang.raise(:exit, reason, __STACKTRACE__)
Expand Down
83 changes: 59 additions & 24 deletions test/open_telemetry_decorator_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ defmodule OpenTelemetryDecoratorTest do
exit(exit_args)
end

@decorate with_span("Example.with_process_exit")
def with_process_exit(exit_args) do
exit(exit_args)
end

@decorate with_span("Example.with_throw")
def with_throw(throw_args) do
throw(throw_args)
Expand Down Expand Up @@ -248,46 +253,76 @@ defmodule OpenTelemetryDecoratorTest do
end

test "normal exits don't throw or set errors" do
Example.with_exit(:normal)
span = assert_span("Example.with_exit")
assert span.status.code == :unset
assert span.status.message == ""
try do
Example.with_exit(:normal)
flunk("Should have continued normal exit")
catch
:exit, :normal ->
span = assert_span("Example.with_exit")
assert span.status.code == :unset
assert span.status.message == ""
end
end

test "normal exits add an exit attribute" do
Example.with_exit(:normal)
span = assert_span("Example.with_exit")
assert span.attributes == %{"app.exit" => :normal}
try do
Example.with_exit(:normal)
flunk("Should have continued normal exit")
catch
:exit, :normal ->
span = assert_span("Example.with_exit")
assert span.attributes == %{"app.exit" => :normal}
end
end

test "shutdowns don't throw or set errors" do
Example.with_exit(:shutdown)
span = assert_span("Example.with_exit")
assert span.status.code == :unset
assert span.status.message == ""
try do
Example.with_exit(:shutdown)
flunk("Should have continued normal shutdown")
catch
:exit, :shutdown ->
span = assert_span("Example.with_exit")
assert span.status.code == :unset
assert span.status.message == ""
end
end

test "shutdowns add an exit attribute" do
Example.with_exit(:shutdown)
span = assert_span("Example.with_exit")
assert span.attributes == %{"app.exit" => :shutdown}
try do
Example.with_exit(:shutdown)
flunk("Should have continued normal shutdown")
catch
:exit, :shutdown ->
span = assert_span("Example.with_exit")
assert span.attributes == %{"app.exit" => :shutdown}
end
end

test "shutdowns with a reason don't throw or set errors" do
Example.with_exit({:shutdown, :chillin})
span = assert_span("Example.with_exit")
assert span.status.code == :unset
assert span.status.message == ""
try do
Example.with_exit({:shutdown, :chillin})
flunk("Should have continued normal shutdown")
catch
:exit, {:shutdown, _reason} ->
span = assert_span("Example.with_exit")
assert span.status.code == :unset
assert span.status.message == ""
end
end

test "shutdowns with a reason add exit and shutdown_reason attributes" do
Example.with_exit({:shutdown, %{just: :chillin}})
span = assert_span("Example.with_exit")
try do
Example.with_exit({:shutdown, %{just: :chillin}})
flunk("Should have continued normal shutdown")
catch
:exit, {:shutdown, _reason} ->
span = assert_span("Example.with_exit")

assert span.attributes == %{
"app.exit" => :shutdown,
"app.shutdown_reason.just" => :chillin
}
assert span.attributes == %{
"app.exit" => :shutdown,
"app.shutdown_reason.just" => :chillin
}
end
end

test "catches throws, sets errors, and re-throws" do
Expand Down

0 comments on commit a52f42b

Please sign in to comment.