Skip to content

Commit 7c01f87

Browse files
committed
improvements
1 parent 826a5ce commit 7c01f87

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

apps/opentelemetry_api/lib/open_telemetry/span.ex

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,15 @@ defmodule OpenTelemetry.Span do
136136
def record_exception(span_ctx, exception, trace \\ nil, attributes \\ [])
137137

138138
def record_exception(span_ctx, exception, trace, attributes) when is_exception?(exception) do
139-
exception_type = to_string(exception.__struct__)
140-
141-
exception_attributes = [
142-
{OpenTelemetry.SemanticConventions.Trace.exception_type(), exception_type},
143-
{OpenTelemetry.SemanticConventions.Trace.exception_message(), Exception.message(exception)},
144-
{OpenTelemetry.SemanticConventions.Trace.exception_stacktrace(),
145-
Exception.format_stacktrace(trace)}
146-
]
147-
148-
add_event(span_ctx, "exception", exception_attributes ++ attributes)
139+
trace =
140+
if trace do
141+
trace
142+
else
143+
{:current_stacktrace, t} = Process.info(self(), :current_stacktrace)
144+
Enum.drop(t, 3)
145+
end
146+
147+
:otel_span.record_exception(span_ctx, :error, exception, trace, attributes)
149148
end
150149

151150
def record_exception(_, _, _, _), do: false

apps/opentelemetry_api/src/otel_span.erl

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,7 @@ add_events(_, _) ->
221221
record_exception(SpanCtx, Class, Term, Stacktrace, Attributes) when is_list(Attributes) ->
222222
record_exception(SpanCtx, Class, Term, Stacktrace, maps:from_list(Attributes));
223223
record_exception(SpanCtx, Class, Term, Stacktrace, Attributes) when is_map(Attributes) ->
224-
ExceptionType = exception_type(Class, Term),
225-
{ok, StacktraceString} = otel_utils:format_binary_string("~0tP", [Stacktrace, 10], [{chars_limit, 50}]),
226-
ExceptionAttributes = #{?EXCEPTION_TYPE => ExceptionType,
227-
?EXCEPTION_STACKTRACE => StacktraceString},
228-
ExceptionAttributes1 = add_elixir_message(ExceptionAttributes, Term),
229-
add_event(SpanCtx, 'exception', maps:merge(ExceptionAttributes1, Attributes));
224+
do_record_exception(SpanCtx, Class, Term, undefined, Stacktrace, Attributes);
230225
record_exception(_, _, _, _, _) ->
231226
false.
232227

@@ -240,15 +235,33 @@ record_exception(_, _, _, _, _) ->
240235
record_exception(SpanCtx, Class, Term, Message, Stacktrace, Attributes) when is_list(Attributes) ->
241236
record_exception(SpanCtx, Class, Term, Message, Stacktrace, maps:from_list(Attributes));
242237
record_exception(SpanCtx, Class, Term, Message, Stacktrace, Attributes) when is_map(Attributes) ->
243-
ExceptionType = exception_type(Class, Term),
244-
{ok, StacktraceString} = otel_utils:format_binary_string("~0tP", [Stacktrace, 10], [{chars_limit, 50}]),
245-
ExceptionAttributes = #{?EXCEPTION_TYPE => ExceptionType,
246-
?EXCEPTION_STACKTRACE => StacktraceString,
247-
?EXCEPTION_MESSAGE => Message},
248-
add_event(SpanCtx, 'exception', maps:merge(ExceptionAttributes, Attributes));
238+
do_record_exception(SpanCtx, Class, Term, Message, Stacktrace, Attributes);
249239
record_exception(_, _, _, _, _, _) ->
250240
false.
251241

242+
do_record_exception(SpanCtx, Class, Term, Message, Stacktrace, Attributes) ->
243+
ExceptionFromElixir = exception_from_elixir(Stacktrace),
244+
Term1 = normalize_exception(Class, Term, Stacktrace, ExceptionFromElixir),
245+
ExceptionType = exception_type(Class, Term1),
246+
StacktraceString = format_stacktrace(Stacktrace, ExceptionFromElixir),
247+
ExceptionAttributes = #{?EXCEPTION_TYPE => ExceptionType,
248+
?EXCEPTION_STACKTRACE => StacktraceString},
249+
ExceptionAttributes1 = add_message(ExceptionAttributes, Message, Term1),
250+
add_event(SpanCtx, exception, maps:merge(ExceptionAttributes1, Attributes)).
251+
exception_from_elixir([{Module, _, _, _} | _]) ->
252+
ModuleStr = atom_to_list(Module),
253+
string:find(ModuleStr, "Elixir.") =:= ModuleStr;
254+
exception_from_elixir(_) ->
255+
false.
256+
format_stacktrace(Stacktrace, false) ->
257+
{ok, StacktraceString} = otel_utils:format_binary_string("~0tP", [Stacktrace, 10], [{chars_limit, 50}]),
258+
StacktraceString;
259+
format_stacktrace(Stacktrace, true) ->
260+
'Elixir.Exception':format_stacktrace(Stacktrace).
261+
normalize_exception(Class, Term, Stacktrace, true) ->
262+
'Elixir.Exception':normalize(Class, Term, Stacktrace);
263+
normalize_exception(_Class, Term, _ExceptionStacktrace, false) ->
264+
Term.
252265
exception_type(error, #{'__exception__' := true, '__struct__' := ElixirErrorStruct} = Term) ->
253266
case atom_to_binary(ElixirErrorStruct) of
254267
<<"Elixir.", ExceptionType/binary>> -> ExceptionType;
@@ -260,15 +273,17 @@ exception_type_erl(Class, Term) ->
260273
{ok, ExceptionType} = otel_utils:format_binary_string("~0tP:~0tP", [Class, 10, Term, 10], [{chars_limit, 50}]),
261274
ExceptionType.
262275

263-
add_elixir_message(Attributes, #{'__exception__' := true} = Exception) ->
276+
add_message(Attributes, Message, _Exception) when Message /= undefined ->
277+
maps:put(?EXCEPTION_MESSAGE, Message, Attributes);
278+
add_message(Attributes, undefined, #{'__exception__' := true} = Exception) ->
264279
try
265280
Message = 'Elixir.Exception':message(Exception),
266281
maps:put(?EXCEPTION_MESSAGE, Message, Attributes)
267282
catch
268283
_Class:_Exception ->
269284
Attributes
270285
end;
271-
add_elixir_message(Attributes, _) ->
286+
add_message(Attributes, _, _) ->
272287
Attributes.
273288

274289
-spec set_status(SpanCtx, StatusOrCode) -> boolean() when

test/otel_tests.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ defmodule OtelTests do
247247
attributes =
248248
:otel_attributes.new(
249249
[
250-
{:"exception.type", "Elixir.RuntimeError"},
250+
{:"exception.type", "RuntimeError"},
251251
{:"exception.message", "my error message"},
252252
{:"exception.stacktrace", stacktrace}
253253
],
@@ -265,7 +265,7 @@ defmodule OtelTests do
265265
:infinity,
266266
0,
267267
[
268-
{:event, _, "exception", ^attributes}
268+
{:event, _, :exception, ^attributes}
269269
]
270270
}
271271
)}
@@ -358,7 +358,7 @@ defmodule OtelTests do
358358
event
359359

360360
assert %{
361-
"exception.type": "error:badarg",
361+
"exception.type": "ArgumentError",
362362
"exception.stacktrace": _
363363
} = received_attirbutes
364364
end

0 commit comments

Comments
 (0)