Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
albertored committed Nov 5, 2023
1 parent 826a5ce commit 7c01f87
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
19 changes: 9 additions & 10 deletions apps/opentelemetry_api/lib/open_telemetry/span.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,15 @@ defmodule OpenTelemetry.Span do
def record_exception(span_ctx, exception, trace \\ nil, attributes \\ [])

def record_exception(span_ctx, exception, trace, attributes) when is_exception?(exception) do
exception_type = to_string(exception.__struct__)

exception_attributes = [
{OpenTelemetry.SemanticConventions.Trace.exception_type(), exception_type},
{OpenTelemetry.SemanticConventions.Trace.exception_message(), Exception.message(exception)},
{OpenTelemetry.SemanticConventions.Trace.exception_stacktrace(),
Exception.format_stacktrace(trace)}
]

add_event(span_ctx, "exception", exception_attributes ++ attributes)
trace =

Check warning on line 139 in apps/opentelemetry_api/lib/open_telemetry/span.ex

View check run for this annotation

Codecov / codecov/patch

apps/opentelemetry_api/lib/open_telemetry/span.ex#L139

Added line #L139 was not covered by tests
if trace do
trace

Check warning on line 141 in apps/opentelemetry_api/lib/open_telemetry/span.ex

View check run for this annotation

Codecov / codecov/patch

apps/opentelemetry_api/lib/open_telemetry/span.ex#L141

Added line #L141 was not covered by tests
else
{:current_stacktrace, t} = Process.info(self(), :current_stacktrace)
Enum.drop(t, 3)

Check warning on line 144 in apps/opentelemetry_api/lib/open_telemetry/span.ex

View check run for this annotation

Codecov / codecov/patch

apps/opentelemetry_api/lib/open_telemetry/span.ex#L143-L144

Added lines #L143 - L144 were not covered by tests
end

:otel_span.record_exception(span_ctx, :error, exception, trace, attributes)

Check warning on line 147 in apps/opentelemetry_api/lib/open_telemetry/span.ex

View check run for this annotation

Codecov / codecov/patch

apps/opentelemetry_api/lib/open_telemetry/span.ex#L147

Added line #L147 was not covered by tests
end

def record_exception(_, _, _, _), do: false
Expand Down
43 changes: 29 additions & 14 deletions apps/opentelemetry_api/src/otel_span.erl
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,7 @@ add_events(_, _) ->
record_exception(SpanCtx, Class, Term, Stacktrace, Attributes) when is_list(Attributes) ->
record_exception(SpanCtx, Class, Term, Stacktrace, maps:from_list(Attributes));
record_exception(SpanCtx, Class, Term, Stacktrace, Attributes) when is_map(Attributes) ->
ExceptionType = exception_type(Class, Term),
{ok, StacktraceString} = otel_utils:format_binary_string("~0tP", [Stacktrace, 10], [{chars_limit, 50}]),
ExceptionAttributes = #{?EXCEPTION_TYPE => ExceptionType,
?EXCEPTION_STACKTRACE => StacktraceString},
ExceptionAttributes1 = add_elixir_message(ExceptionAttributes, Term),
add_event(SpanCtx, 'exception', maps:merge(ExceptionAttributes1, Attributes));
do_record_exception(SpanCtx, Class, Term, undefined, Stacktrace, Attributes);
record_exception(_, _, _, _, _) ->
false.

Expand All @@ -240,15 +235,33 @@ record_exception(_, _, _, _, _) ->
record_exception(SpanCtx, Class, Term, Message, Stacktrace, Attributes) when is_list(Attributes) ->
record_exception(SpanCtx, Class, Term, Message, Stacktrace, maps:from_list(Attributes));
record_exception(SpanCtx, Class, Term, Message, Stacktrace, Attributes) when is_map(Attributes) ->
ExceptionType = exception_type(Class, Term),
{ok, StacktraceString} = otel_utils:format_binary_string("~0tP", [Stacktrace, 10], [{chars_limit, 50}]),
ExceptionAttributes = #{?EXCEPTION_TYPE => ExceptionType,
?EXCEPTION_STACKTRACE => StacktraceString,
?EXCEPTION_MESSAGE => Message},
add_event(SpanCtx, 'exception', maps:merge(ExceptionAttributes, Attributes));
do_record_exception(SpanCtx, Class, Term, Message, Stacktrace, Attributes);
record_exception(_, _, _, _, _, _) ->
false.

do_record_exception(SpanCtx, Class, Term, Message, Stacktrace, Attributes) ->
ExceptionFromElixir = exception_from_elixir(Stacktrace),
Term1 = normalize_exception(Class, Term, Stacktrace, ExceptionFromElixir),
ExceptionType = exception_type(Class, Term1),
StacktraceString = format_stacktrace(Stacktrace, ExceptionFromElixir),
ExceptionAttributes = #{?EXCEPTION_TYPE => ExceptionType,
?EXCEPTION_STACKTRACE => StacktraceString},
ExceptionAttributes1 = add_message(ExceptionAttributes, Message, Term1),
add_event(SpanCtx, exception, maps:merge(ExceptionAttributes1, Attributes)).
exception_from_elixir([{Module, _, _, _} | _]) ->
ModuleStr = atom_to_list(Module),
string:find(ModuleStr, "Elixir.") =:= ModuleStr;
exception_from_elixir(_) ->
false.

Check warning on line 255 in apps/opentelemetry_api/src/otel_span.erl

View check run for this annotation

Codecov / codecov/patch

apps/opentelemetry_api/src/otel_span.erl#L255

Added line #L255 was not covered by tests
format_stacktrace(Stacktrace, false) ->
{ok, StacktraceString} = otel_utils:format_binary_string("~0tP", [Stacktrace, 10], [{chars_limit, 50}]),
StacktraceString;
format_stacktrace(Stacktrace, true) ->
'Elixir.Exception':format_stacktrace(Stacktrace).
normalize_exception(Class, Term, Stacktrace, true) ->
'Elixir.Exception':normalize(Class, Term, Stacktrace);

Check warning on line 262 in apps/opentelemetry_api/src/otel_span.erl

View check run for this annotation

Codecov / codecov/patch

apps/opentelemetry_api/src/otel_span.erl#L259-L262

Added lines #L259 - L262 were not covered by tests
normalize_exception(_Class, Term, _ExceptionStacktrace, false) ->
Term.
exception_type(error, #{'__exception__' := true, '__struct__' := ElixirErrorStruct} = Term) ->
case atom_to_binary(ElixirErrorStruct) of
<<"Elixir.", ExceptionType/binary>> -> ExceptionType;
Expand All @@ -260,15 +273,17 @@ exception_type_erl(Class, Term) ->
{ok, ExceptionType} = otel_utils:format_binary_string("~0tP:~0tP", [Class, 10, Term, 10], [{chars_limit, 50}]),
ExceptionType.

add_elixir_message(Attributes, #{'__exception__' := true} = Exception) ->
add_message(Attributes, Message, _Exception) when Message /= undefined ->
maps:put(?EXCEPTION_MESSAGE, Message, Attributes);
add_message(Attributes, undefined, #{'__exception__' := true} = Exception) ->
try
Message = 'Elixir.Exception':message(Exception),
maps:put(?EXCEPTION_MESSAGE, Message, Attributes)
catch
_Class:_Exception ->

Check warning on line 283 in apps/opentelemetry_api/src/otel_span.erl

View check run for this annotation

Codecov / codecov/patch

apps/opentelemetry_api/src/otel_span.erl#L281-L283

Added lines #L281 - L283 were not covered by tests
Attributes
end;
add_elixir_message(Attributes, _) ->
add_message(Attributes, _, _) ->
Attributes.

-spec set_status(SpanCtx, StatusOrCode) -> boolean() when
Expand Down
6 changes: 3 additions & 3 deletions test/otel_tests.exs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ defmodule OtelTests do
attributes =
:otel_attributes.new(
[
{:"exception.type", "Elixir.RuntimeError"},
{:"exception.type", "RuntimeError"},
{:"exception.message", "my error message"},
{:"exception.stacktrace", stacktrace}
],
Expand All @@ -265,7 +265,7 @@ defmodule OtelTests do
:infinity,
0,
[
{:event, _, "exception", ^attributes}
{:event, _, :exception, ^attributes}
]
}
)}
Expand Down Expand Up @@ -358,7 +358,7 @@ defmodule OtelTests do
event

assert %{
"exception.type": "error:badarg",
"exception.type": "ArgumentError",
"exception.stacktrace": _
} = received_attirbutes
end
Expand Down

0 comments on commit 7c01f87

Please sign in to comment.