diff --git a/lib/ecto/repo/supervisor.ex b/lib/ecto/repo/supervisor.ex index 1be968d816..05caaa1f07 100644 --- a/lib/ecto/repo/supervisor.ex +++ b/lib/ecto/repo/supervisor.ex @@ -201,7 +201,7 @@ defmodule Ecto.Repo.Supervisor do name = if is_atom(name), do: name, else: nil cache = Ecto.Query.Planner.new_query_cache(name) meta = Map.merge(meta, %{repo: repo, cache: cache}) - child_spec = wrap_child_spec(child, [name, adapter, meta]) + child_spec = wrap_child_spec(child, [name, adapter, meta, opts]) Supervisor.init([child_spec], strategy: :one_for_one, max_restarts: 0) :ignore -> @@ -209,11 +209,18 @@ defmodule Ecto.Repo.Supervisor do end end - def start_child({mod, fun, args}, name, adapter, meta) do + def start_child({mod, fun, args}, name, adapter, meta, opts) do case apply(mod, fun, args) do {:ok, pid} -> meta = Map.merge(meta, %{pid: pid, adapter: adapter}) Ecto.Repo.Registry.associate(self(), name, meta) + + :telemetry.execute( + [:ecto, :repo, :started], + %{system_time: System.system_time()}, + %{repo: meta.repo, adapter: meta.adapter, config: opts} + ) + {:ok, pid} other -> diff --git a/test/ecto/repo/supervisor_test.exs b/test/ecto/repo/supervisor_test.exs index 73f2f827a7..dfd12375d2 100644 --- a/test/ecto/repo/supervisor_test.exs +++ b/test/ecto/repo/supervisor_test.exs @@ -52,6 +52,41 @@ defmodule Ecto.Repo.SupervisorTest do :telemetry.detach(:telemetry_test) end + test "emits :started telemetry event after repo process starts" do + :telemetry.attach_many( + :telemetry_started_test, + [[:ecto, :repo, :started]], + &__MODULE__.handle_event/4, + %{pid: self()} + ) + + {:ok, _pid} = Ecto.TestRepo.start_link(name: :telemetry_started_test) + + assert_receive {[:ecto, :repo, :started], %{system_time: _}, %{repo: Ecto.TestRepo, adapter: Ecto.TestAdapter, config: config}} + assert config[:name] == :telemetry_started_test + assert config[:database] == "hello" + + :telemetry.detach(:telemetry_started_test) + end + + test "emits :started telemetry event even when repo modifies config in init" do + :telemetry.attach_many( + :telemetry_started_test_init, + [[:ecto, :repo, :started]], + &__MODULE__.handle_event/4, + %{pid: self()} + ) + + {:ok, _pid} = Ecto.TestRepoInitModify.start_link(name: :telemetry_started_test_init_modify) + + assert_receive {[:ecto, :repo, :started], %{system_time: _}, %{repo: Ecto.TestRepoInitModify, adapter: Ecto.TestAdapter, config: config}} + assert config[:name] == :telemetry_started_test_init_modify + assert config[:database] == "hello" + refute Keyword.has_key?(config, :telemetry_prefix) + + :telemetry.detach(:telemetry_started_test_init) + end + test "reads otp app configuration" do put_env(database: "hello") {:ok, config} = init_config(:runtime, __MODULE__, :ecto, []) diff --git a/test/support/test_repo.exs b/test/support/test_repo.exs index 7d7b8a322f..1d03d0d690 100644 --- a/test/support/test_repo.exs +++ b/test/support/test_repo.exs @@ -213,4 +213,14 @@ defmodule Ecto.TestRepo do end end +defmodule Ecto.TestRepoInitModify do + use Ecto.Repo, otp_app: :ecto, adapter: Ecto.TestAdapter + + def init(_type, opts) do + opts = [url: "ecto://user:pass@local/hello"] ++ opts + opts = Keyword.drop(opts, [:telemetry_prefix]) + {:ok, opts} + end +end + Ecto.TestRepo.start_link()