Skip to content

Commit

Permalink
Record an exception as an error only when Plug.Exception.status/1
Browse files Browse the repository at this point in the history
returns 500..599.

It is encouraged in the Phoenix docs to use `Plug.Exception` with
custom exceptions to generate for example 404 responses at certain
places. These 404s should not be marked as error, since simple "route
not found" 404s are also not marked as error.
  • Loading branch information
derekkraan committed Apr 19, 2023
1 parent 1de26cc commit ea12a4b
Showing 1 changed file with 9 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,15 @@ defmodule OpentelemetryPhoenix do
# try to normalize all errors to Elixir exceptions
exception = Exception.normalize(kind, reason, stacktrace)

# record exception and mark the span as errored
Tracer.record_exception(exception, stacktrace, attrs)
Tracer.set_status(OpenTelemetry.status(:error, ""))
case Plug.Exception.status(exception) do
500..599 ->
# record exception and mark the span as errored
Tracer.record_exception(exception, stacktrace, attrs)
Tracer.set_status(OpenTelemetry.status(:error, ""))

_ ->
nil
end

# do not close the span as endpoint stop will still be called with
# more info, including the status code, which is nil at this stage
Expand Down

0 comments on commit ea12a4b

Please sign in to comment.