@@ -221,12 +221,7 @@ add_events(_, _) ->
221
221
record_exception (SpanCtx , Class , Term , Stacktrace , Attributes ) when is_list (Attributes ) ->
222
222
record_exception (SpanCtx , Class , Term , Stacktrace , maps :from_list (Attributes ));
223
223
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 );
230
225
record_exception (_ , _ , _ , _ , _ ) ->
231
226
false .
232
227
@@ -240,15 +235,33 @@ record_exception(_, _, _, _, _) ->
240
235
record_exception (SpanCtx , Class , Term , Message , Stacktrace , Attributes ) when is_list (Attributes ) ->
241
236
record_exception (SpanCtx , Class , Term , Message , Stacktrace , maps :from_list (Attributes ));
242
237
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 );
249
239
record_exception (_ , _ , _ , _ , _ , _ ) ->
250
240
false .
251
241
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 .
252
265
exception_type (error , #{'__exception__' := true , '__struct__' := ElixirErrorStruct } = Term ) ->
253
266
case atom_to_binary (ElixirErrorStruct ) of
254
267
<<" Elixir." , ExceptionType /binary >> -> ExceptionType ;
@@ -260,15 +273,17 @@ exception_type_erl(Class, Term) ->
260
273
{ok , ExceptionType } = otel_utils :format_binary_string (" ~0tP :~0tP " , [Class , 10 , Term , 10 ], [{chars_limit , 50 }]),
261
274
ExceptionType .
262
275
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 ) ->
264
279
try
265
280
Message = 'Elixir.Exception' :message (Exception ),
266
281
maps :put (? EXCEPTION_MESSAGE , Message , Attributes )
267
282
catch
268
283
_Class :_Exception ->
269
284
Attributes
270
285
end ;
271
- add_elixir_message (Attributes , _ ) ->
286
+ add_message (Attributes , _ , _ ) ->
272
287
Attributes .
273
288
274
289
-spec set_status (SpanCtx , StatusOrCode ) -> boolean () when
0 commit comments