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
|
_ -> type
|
||||||
end
|
end
|
||||||
|
|
||||||
result =
|
|
||||||
case query_result do
|
|
||||||
{:ok, _} -> []
|
|
||||||
_ -> [error: true]
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: need connection information to complete the required attributes
|
# TODO: need connection information to complete the required attributes
|
||||||
# net.peer.name or net.peer.ip and net.peer.port
|
# net.peer.name or net.peer.ip and net.peer.port
|
||||||
base_attributes =
|
base_attributes = [
|
||||||
Keyword.merge(result,
|
|
||||||
"db.type": db_type,
|
"db.type": db_type,
|
||||||
"db.statement": query,
|
"db.statement": query,
|
||||||
source: source,
|
source: source,
|
||||||
"db.instance": database,
|
"db.instance": database,
|
||||||
"db.url": url,
|
"db.url": url,
|
||||||
"total_time_#{time_unit}s": System.convert_time_unit(total_time, :native, time_unit)
|
"total_time_#{time_unit}s": System.convert_time_unit(total_time, :native, time_unit)
|
||||||
)
|
]
|
||||||
|
|
||||||
attributes =
|
attributes =
|
||||||
measurements
|
measurements
|
||||||
|
@ -101,6 +94,20 @@ defmodule OpentelemetryEcto do
|
||||||
kind: :client
|
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)
|
OpenTelemetry.Span.end_span(s)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp format_error(%{__exception__: true} = exception) do
|
||||||
|
Exception.message(exception)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp format_error(_), do: ""
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
defmodule OpentelemetryEctoTest do
|
defmodule OpentelemetryEctoTest do
|
||||||
|
use ExUnit.Case
|
||||||
|
import Ecto.Query
|
||||||
|
require OpenTelemetry.Tracer
|
||||||
|
|
||||||
alias OpentelemetryEcto.TestRepo, as: Repo
|
alias OpentelemetryEcto.TestRepo, as: Repo
|
||||||
alias OpentelemetryEcto.TestModels.{User, Post}
|
alias OpentelemetryEcto.TestModels.{User, Post}
|
||||||
require OpenTelemetry.Tracer
|
|
||||||
use ExUnit.Case
|
|
||||||
|
|
||||||
@event_name [:opentelemetry_ecto, :test_repo]
|
@event_name [:opentelemetry_ecto, :test_repo]
|
||||||
|
|
||||||
|
@ -102,6 +104,24 @@ defmodule OpentelemetryEctoTest do
|
||||||
assert_receive {:span, span(name: "opentelemetry_ecto.test_repo.query:posts")}
|
assert_receive {:span, span(name: "opentelemetry_ecto.test_repo.query:posts")}
|
||||||
end
|
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
|
def attach_handler(config \\ []) do
|
||||||
# For now setup the handler manually in each test
|
# For now setup the handler manually in each test
|
||||||
handler = {__MODULE__, self()}
|
handler = {__MODULE__, self()}
|
||||||
|
|
Loading…
Reference in New Issue