Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch throws, stringify thrown values and exit reasons for reporting #149

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading