From e29ddfaabaad95b436d6680c0466a5cded192e4f Mon Sep 17 00:00:00 2001 From: Radek Szymczyszyn Date: Thu, 4 Jul 2024 12:21:56 +0200 Subject: [PATCH] Catch throws, stringify thrown values and exit reasons for reporting --- lib/open_telemetry_decorator.ex | 11 +++++++-- test/open_telemetry_decorator_test.exs | 33 +++++++++++++++++++++----- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/lib/open_telemetry_decorator.ex b/lib/open_telemetry_decorator.ex index 4a6b240..eedfe27 100644 --- a/lib/open_telemetry_decorator.ex +++ b/lib/open_telemetry_decorator.ex @@ -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 diff --git a/test/open_telemetry_decorator_test.exs b/test/open_telemetry_decorator_test.exs index 34ba466..cbbda17 100644 --- a/test/open_telemetry_decorator_test.exs +++ b/test/open_telemetry_decorator_test.exs @@ -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!") @@ -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 @@ -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