Skip to content

Commit e29ddfa

Browse files
erszczmarcdel
authored andcommitted
Catch throws, stringify thrown values and exit reasons for reporting
1 parent da614c6 commit e29ddfa

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

lib/open_telemetry_decorator.ex

+9-2
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,18 @@ 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(
94+
[exit: :shutdown, shutdown_reason: inspect(reason)],
95+
namespace: prefix
96+
)
9497

9598
:exit, reason ->
96-
O11y.set_error("exited: #{reason}")
99+
O11y.set_error("exited: #{inspect(reason)}")
97100
:erlang.raise(:exit, reason, __STACKTRACE__)
101+
102+
:throw, thrown ->
103+
O11y.set_error("uncaught: #{inspect(thrown)}")
104+
:erlang.raise(:throw, thrown, __STACKTRACE__)
98105
after
99106
O11y.end_span(parent_span)
100107
end

test/open_telemetry_decorator_test.exs

+27-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,25 @@ 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+
287+
assert span.attributes == %{
288+
"app.exit" => :shutdown,
289+
"app.shutdown_reason" => "%{just: :chillin}"
290+
}
291+
end
292+
293+
test "catches throws, sets errors, and re-throws" do
294+
try do
295+
Example.with_throw(%{catch: :this})
296+
flunk("Should have re-raised the exception")
297+
catch
298+
:throw, %{catch: :this} ->
299+
span = assert_span("Example.with_throw")
300+
assert span.status.code == :error
301+
assert span.status.message == "uncaught: %{catch: :this}"
302+
end
282303
end
283304

284305
test "adds included input params on exception" do

0 commit comments

Comments
 (0)