Skip to content

Commit

Permalink
Catch throws, stringify thrown values and exit reasons for reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
erszcz authored and marcdel committed Jul 12, 2024
1 parent da614c6 commit e29ddfa
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
11 changes: 9 additions & 2 deletions lib/open_telemetry_decorator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,18 @@ defmodule OpenTelemetryDecorator do
O11y.set_attribute(:exit, :shutdown, namespace: prefix)

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

:exit, reason ->
O11y.set_error("exited: #{reason}")
O11y.set_error("exited: #{inspect(reason)}")
:erlang.raise(:exit, reason, __STACKTRACE__)

:throw, thrown ->
O11y.set_error("uncaught: #{inspect(thrown)}")
:erlang.raise(:throw, thrown, __STACKTRACE__)
after
O11y.end_span(parent_span)
end
Expand Down
33 changes: 27 additions & 6 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_throw")
def with_throw(throw_args) do
throw(throw_args)
end

@decorate with_span("Example.with_error")
def with_error, do: Attributes.set(:error, "ruh roh!")

Expand Down Expand Up @@ -230,15 +235,15 @@ defmodule OpenTelemetryDecoratorTest do
end
end

test "catches exists, sets errors, and re-throws" do
test "catches exits, sets errors, and re-throws" do
try do
Example.with_exit(:bad_times)
Example.with_exit(%{bad: :times})
flunk("Should have re-raised the exception")
catch
:exit, :bad_times ->
:exit, %{bad: :times} ->
span = assert_span("Example.with_exit")
assert span.status.code == :error
assert span.status.message == "exited: bad_times"
assert span.status.message == "exited: %{bad: :times}"
end
end

Expand Down Expand Up @@ -276,9 +281,25 @@ defmodule OpenTelemetryDecoratorTest do
end

test "shutdowns with a reason add exit and shutdown_reason attributes" do
Example.with_exit({:shutdown, :chillin})
Example.with_exit({:shutdown, %{just: :chillin}})
span = assert_span("Example.with_exit")
assert span.attributes == %{"app.exit" => :shutdown, "app.shutdown_reason" => :chillin}

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

test "catches throws, sets errors, and re-throws" do
try do
Example.with_throw(%{catch: :this})
flunk("Should have re-raised the exception")
catch
:throw, %{catch: :this} ->
span = assert_span("Example.with_throw")
assert span.status.code == :error
assert span.status.message == "uncaught: %{catch: :this}"
end
end

test "adds included input params on exception" do
Expand Down

0 comments on commit e29ddfa

Please sign in to comment.