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
|
2022-03-24 20:19:24 +00:00
|
|
|
alias OpentelemetryEcto.TestModels.{Comment, User, Post}
|
|
|
|
|
|
|
|
require Ecto.Query, as: Query
|
|
|
|
require OpenTelemetry.Tracer, as: Tracer
|
2021-10-08 21:38:18 +00:00
|
|
|
|
|
|
|
@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
|
2022-03-24 20:19:24 +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)
|
|
|
|
|
|
|
|
:otel_batch_processor.set_exporter(:otel_exporter_pid, self())
|
2021-10-08 21:38:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2022-09-02 19:47:34 +00:00
|
|
|
test "include additionaL_attributes" do
|
|
|
|
attach_handler(additional_attributes: %{"config.attribute": "special value", "db.instance": "my_instance"})
|
|
|
|
Repo.all(User)
|
|
|
|
|
|
|
|
assert_receive {:span, span(attributes: attributes)}
|
|
|
|
assert %{"config.attribute": "special value", "db.instance": "my_instance"} = :otel_attributes.map(attributes)
|
|
|
|
end
|
|
|
|
|
2021-10-08 21:38:18 +00:00
|
|
|
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
|
|
|
|
|
2022-03-24 20:19:24 +00:00
|
|
|
test "preloads in sequence are tied to the parent span" do
|
|
|
|
user = Repo.insert!(%User{email: "opentelemetry@erlang.org"})
|
|
|
|
Repo.insert!(%Post{body: "We got traced!", user: user})
|
|
|
|
Repo.insert!(%Comment{body: "We got traced!", user: user})
|
|
|
|
|
|
|
|
attach_handler()
|
|
|
|
|
|
|
|
Tracer.with_span "parent span" do
|
|
|
|
Repo.all(Query.from(User, preload: [:posts, :comments]), in_parallel: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_receive {:span, span(span_id: root_span_id, name: "parent span")}
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:users")}
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:posts")}
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:comments")}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "preloads in parallel are tied to the parent span" do
|
|
|
|
user = Repo.insert!(%User{email: "opentelemetry@erlang.org"})
|
|
|
|
Repo.insert!(%Post{body: "We got traced!", user: user})
|
|
|
|
Repo.insert!(%Comment{body: "We got traced!", user: user})
|
|
|
|
|
|
|
|
attach_handler()
|
|
|
|
|
|
|
|
Tracer.with_span "parent span" do
|
|
|
|
Repo.all(Query.from(User, preload: [:posts, :comments]))
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_receive {:span, span(span_id: root_span_id, name: "parent span")}
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:users")}
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:posts")}
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:comments")}
|
|
|
|
end
|
|
|
|
|
|
|
|
test "nested query preloads are tied to the parent span" do
|
|
|
|
user = Repo.insert!(%User{email: "opentelemetry@erlang.org"})
|
|
|
|
Repo.insert!(%Post{body: "We got traced!", user: user})
|
|
|
|
Repo.insert!(%Comment{body: "We got traced!", user: user})
|
|
|
|
|
|
|
|
attach_handler()
|
|
|
|
|
|
|
|
Tracer.with_span "parent span" do
|
|
|
|
users_query = from u in User, preload: [:posts, :comments]
|
|
|
|
comments_query = from c in Comment, preload: [user: ^users_query]
|
|
|
|
Repo.all(Query.from(User, preload: [:posts, comments: ^comments_query]))
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_receive {:span, span(span_id: root_span_id, name: "parent span")}
|
|
|
|
# root query
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:users")}
|
|
|
|
# comments preload
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:comments")}
|
|
|
|
# users preload
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:users")}
|
|
|
|
# preloads of user
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:posts")}
|
|
|
|
assert_receive {:span, span(parent_span_id: ^root_span_id, name: "opentelemetry_ecto.test_repo.query:comments")}
|
|
|
|
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
|