Set error status for ecto errors in opentelemetry_ecto (#41)
* Set error status on error instead of just adding attribute * Use Exception.message if error is an exception Co-authored-by: Bryan Naegele <bryannaegele@users.noreply.github.com>
This commit is contained in:
parent
4aaf86181a
commit
352c17231a
|
@ -67,23 +67,16 @@ defmodule OpentelemetryEcto do
|
|||
_ -> type
|
||||
end
|
||||
|
||||
result =
|
||||
case query_result do
|
||||
{:ok, _} -> []
|
||||
_ -> [error: true]
|
||||
end
|
||||
|
||||
# TODO: need connection information to complete the required attributes
|
||||
# net.peer.name or net.peer.ip and net.peer.port
|
||||
base_attributes =
|
||||
Keyword.merge(result,
|
||||
base_attributes = [
|
||||
"db.type": db_type,
|
||||
"db.statement": query,
|
||||
source: source,
|
||||
"db.instance": database,
|
||||
"db.url": url,
|
||||
"total_time_#{time_unit}s": System.convert_time_unit(total_time, :native, time_unit)
|
||||
)
|
||||
]
|
||||
|
||||
attributes =
|
||||
measurements
|
||||
|
@ -101,6 +94,20 @@ defmodule OpentelemetryEcto do
|
|||
kind: :client
|
||||
})
|
||||
|
||||
case query_result do
|
||||
{:error, error} ->
|
||||
OpenTelemetry.Span.set_status(s, OpenTelemetry.status(:error, format_error(error)))
|
||||
|
||||
{:ok, _} ->
|
||||
:ok
|
||||
end
|
||||
|
||||
OpenTelemetry.Span.end_span(s)
|
||||
end
|
||||
|
||||
defp format_error(%{__exception__: true} = exception) do
|
||||
Exception.message(exception)
|
||||
end
|
||||
|
||||
defp format_error(_), do: ""
|
||||
end
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
defmodule OpentelemetryEctoTest do
|
||||
use ExUnit.Case
|
||||
import Ecto.Query
|
||||
require OpenTelemetry.Tracer
|
||||
|
||||
alias OpentelemetryEcto.TestRepo, as: Repo
|
||||
alias OpentelemetryEcto.TestModels.{User, Post}
|
||||
require OpenTelemetry.Tracer
|
||||
use ExUnit.Case
|
||||
|
||||
@event_name [:opentelemetry_ecto, :test_repo]
|
||||
|
||||
|
@ -102,6 +104,24 @@ defmodule OpentelemetryEctoTest do
|
|||
assert_receive {:span, span(name: "opentelemetry_ecto.test_repo.query:posts")}
|
||||
end
|
||||
|
||||
test "sets error message on error" do
|
||||
attach_handler()
|
||||
|
||||
try do
|
||||
Repo.all(from u in "users", select: u.non_existant_field)
|
||||
rescue
|
||||
_ -> :ok
|
||||
end
|
||||
|
||||
assert_receive {:span,
|
||||
span(
|
||||
name: "opentelemetry_ecto.test_repo.query:users",
|
||||
status: {:status, :error, message}
|
||||
)}
|
||||
|
||||
assert message =~ "non_existant_field does not exist"
|
||||
end
|
||||
|
||||
def attach_handler(config \\ []) do
|
||||
# For now setup the handler manually in each test
|
||||
handler = {__MODULE__, self()}
|
||||
|
|
Loading…
Reference in New Issue