Skip to content

Commit 8f74a7b

Browse files
committed
Catch throws, stringify thrown values and exit reasons for reporting
1 parent da614c6 commit 8f74a7b

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

lib/open_telemetry_decorator.ex

+6-2
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,15 @@ defmodule OpenTelemetryDecorator do
9090
O11y.set_attribute(:exit, :shutdown, namespace: prefix)
9191

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

9595
:exit, reason ->
96-
O11y.set_error("exited: #{reason}")
96+
O11y.set_error("exited: #{inspect(reason)}")
9797
:erlang.raise(:exit, reason, __STACKTRACE__)
98+
99+
:throw, thrown ->
100+
O11y.set_error("uncaught: #{inspect(thrown)}")
101+
:erlang.raise(:throw, thrown, __STACKTRACE__)
98102
after
99103
O11y.end_span(parent_span)
100104
end

test/open_telemetry_decorator_test.exs

+23-6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,11 @@ defmodule OpenTelemetryDecoratorTest do
7474
exit(exit_args)
7575
end
7676

77+
@decorate with_span("Example.with_throw")
78+
def with_throw(throw_args) do
79+
throw(throw_args)
80+
end
81+
7782
@decorate with_span("Example.with_error")
7883
def with_error, do: Attributes.set(:error, "ruh roh!")
7984

@@ -230,15 +235,15 @@ defmodule OpenTelemetryDecoratorTest do
230235
end
231236
end
232237

233-
test "catches exists, sets errors, and re-throws" do
238+
test "catches exits, sets errors, and re-throws" do
234239
try do
235-
Example.with_exit(:bad_times)
240+
Example.with_exit(%{bad: :times})
236241
flunk("Should have re-raised the exception")
237242
catch
238-
:exit, :bad_times ->
243+
:exit, %{bad: :times} ->
239244
span = assert_span("Example.with_exit")
240245
assert span.status.code == :error
241-
assert span.status.message == "exited: bad_times"
246+
assert span.status.message == "exited: %{bad: :times}"
242247
end
243248
end
244249

@@ -276,9 +281,21 @@ defmodule OpenTelemetryDecoratorTest do
276281
end
277282

278283
test "shutdowns with a reason add exit and shutdown_reason attributes" do
279-
Example.with_exit({:shutdown, :chillin})
284+
Example.with_exit({:shutdown, %{just: :chillin}})
280285
span = assert_span("Example.with_exit")
281-
assert span.attributes == %{"app.exit" => :shutdown, "app.shutdown_reason" => :chillin}
286+
assert span.attributes == %{"app.exit" => :shutdown, "app.shutdown_reason" => "%{just: :chillin}"}
287+
end
288+
289+
test "catches throws, sets errors, and re-throws" do
290+
try do
291+
Example.with_throw(%{catch: :this})
292+
flunk("Should have re-raised the exception")
293+
catch
294+
:throw, %{catch: :this} ->
295+
span = assert_span("Example.with_throw")
296+
assert span.status.code == :error
297+
assert span.status.message == "uncaught: %{catch: :this}"
298+
end
282299
end
283300

284301
test "adds included input params on exception" do

0 commit comments

Comments
 (0)