2021-10-08 21:38:18 +00:00
|
|
|
defmodule OpentelemetryEctoTest do
|
2021-11-22 19:08:31 +00:00
|
|
|
use ExUnit.Case
|
|
|
|
import Ecto.Query
|
|
|
|
require OpenTelemetry.Tracer
|
|
|
|
|
2021-10-08 21:38:18 +00:00
|
|
|
alias OpentelemetryEcto.TestRepo, as: Repo
|
|
|
|
alias OpentelemetryEcto.TestModels.{User, Post}
|
|
|
|
|
|
|
|
@event_name [:opentelemetry_ecto, :test_repo]
|
|
|
|
|
|
|
|
require Record
|
|
|
|
|
|
|
|
for {name, spec} <- Record.extract_all(from_lib: "opentelemetry/include/otel_span.hrl") do
|
|
|
|
Record.defrecord(name, spec)
|
|
|
|
end
|
|
|
|
|
|
|
|
setup do
|
2021-10-14 03:11:26 +00:00
|
|
|
:application.stop(:opentelemetry)
|
|
|
|
:application.set_env(:opentelemetry, :tracer, :otel_tracer_default)
|
|
|
|
|
|
|
|
:application.set_env(:opentelemetry, :processors, [
|
|
|
|
{:otel_batch_processor, %{scheduled_delay_ms: 1}}
|
|
|
|
])
|
|
|
|
|
|
|
|
:application.start(:opentelemetry)
|
|
|
|
|
2021-10-08 21:38:18 +00:00
|
|
|
:otel_batch_processor.set_exporter(:otel_exporter_pid, self())
|
|
|
|
|
|
|
|
OpenTelemetry.Tracer.start_span("test")
|
|
|
|
|
|
|
|
on_exit(fn ->
|
|
|
|
OpenTelemetry.Tracer.end_span()
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
test "captures basic query events" do
|
|
|
|
attach_handler()
|
|
|
|
|
|
|
|
Repo.all(User)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "opentelemetry_ecto.test_repo.query:users",
|
2021-12-28 23:39:06 +00:00
|
|
|
attributes: attributes,
|
2021-11-22 18:49:58 +00:00
|
|
|
kind: :client
|
2021-10-08 21:38:18 +00:00
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
assert %{
|
2021-10-08 21:38:18 +00:00
|
|
|
"db.instance": "opentelemetry_ecto_test",
|
|
|
|
"db.statement": "SELECT u0.\"id\", u0.\"email\" FROM \"users\" AS u0",
|
|
|
|
"db.type": :sql,
|
|
|
|
"db.url": "ecto://localhost",
|
|
|
|
decode_time_microseconds: _,
|
|
|
|
query_time_microseconds: _,
|
|
|
|
queue_time_microseconds: _,
|
|
|
|
source: "users",
|
|
|
|
total_time_microseconds: _
|
2021-12-28 23:39:06 +00:00
|
|
|
} = :otel_attributes.map(attributes)
|
2021-10-08 21:38:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "changes the time unit" do
|
|
|
|
attach_handler(time_unit: :millisecond)
|
|
|
|
|
|
|
|
Repo.all(Post)
|
|
|
|
|
|
|
|
assert_receive {:span,
|
|
|
|
span(
|
|
|
|
name: "opentelemetry_ecto.test_repo.query:posts",
|
2021-12-28 23:39:06 +00:00
|
|
|
attributes: attributes
|
2021-10-08 21:38:18 +00:00
|
|
|
)}
|
|
|
|
|
2021-12-28 23:39:06 +00:00
|
|
|
assert %{
|
2021-10-08 21:38:18 +00:00
|
|
|
"db.instance": "opentelemetry_ecto_test",
|
|
|
|
"db.statement": "SELECT p0.\"id\", p0.\"body\", p0.\"user_id\" FROM \"posts\" AS p0",
|
|
|
|
"db.type": :sql,
|
|
|
|
"db.url": "ecto://localhost",
|
|
|
|
decode_time_milliseconds: _,
|
|
|
|
query_time_milliseconds: _,
|
|
|
|
queue_time_milliseconds: _,
|
|
|
|
source: "posts",
|
|
|
|
total_time_milliseconds: _
|
2021-12-28 23:39:06 +00:00
|
|
|
} = :otel_attributes.map(attributes)
|
2021-10-08 21:38:18 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
test "changes the span name prefix" do
|
|
|
|
attach_handler(span_prefix: "Ecto")
|
|
|
|
|
|
|
|
Repo.all(User)
|
|
|
|
|
|
|
|
assert_receive {:span, span(name: "Ecto:users")}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "collects multiple spans" do
|
|
|
|
user = Repo.insert!(%User{email: "opentelemetry@erlang.org"})
|
|
|
|
Repo.insert!(%Post{body: "We got traced!", user: user})
|
|
|
|
|
|
|
|
attach_handler()
|
|
|
|
|
|
|
|
User
|
|
|
|
|> Repo.all()
|
|
|
|
|> Repo.preload([:posts])
|
|
|
|
|
|
|
|
assert_receive {:span, span(name: "opentelemetry_ecto.test_repo.query:users")}
|
|
|
|
assert_receive {:span, span(name: "opentelemetry_ecto.test_repo.query:posts")}
|
|
|
|
end
|
|
|
|
|
2021-11-22 19:08:31 +00:00
|
|
|
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
|
|
|
|
|
2021-10-08 21:38:18 +00:00
|
|
|
def attach_handler(config \\ []) do
|
|
|
|
# For now setup the handler manually in each test
|
|
|
|
handler = {__MODULE__, self()}
|
|
|
|
|
|
|
|
:telemetry.attach(handler, @event_name ++ [:query], &OpentelemetryEcto.handle_event/4, config)
|
|
|
|
|
|
|
|
on_exit(fn ->
|
|
|
|
:telemetry.detach(handler)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|