Skip to content

Commit 4fc43fd

Browse files
committed
feat: add telemetry event emission for repo process start
1 parent a75cefa commit 4fc43fd

3 files changed

Lines changed: 54 additions & 2 deletions

File tree

lib/ecto/repo/supervisor.ex

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,19 +201,26 @@ defmodule Ecto.Repo.Supervisor do
201201
name = if is_atom(name), do: name, else: nil
202202
cache = Ecto.Query.Planner.new_query_cache(name)
203203
meta = Map.merge(meta, %{repo: repo, cache: cache})
204-
child_spec = wrap_child_spec(child, [name, adapter, meta])
204+
child_spec = wrap_child_spec(child, [name, adapter, meta, opts])
205205
Supervisor.init([child_spec], strategy: :one_for_one, max_restarts: 0)
206206

207207
:ignore ->
208208
:ignore
209209
end
210210
end
211211

212-
def start_child({mod, fun, args}, name, adapter, meta) do
212+
def start_child({mod, fun, args}, name, adapter, meta, opts) do
213213
case apply(mod, fun, args) do
214214
{:ok, pid} ->
215215
meta = Map.merge(meta, %{pid: pid, adapter: adapter})
216216
Ecto.Repo.Registry.associate(self(), name, meta)
217+
218+
:telemetry.execute(
219+
[:ecto, :repo, :started],
220+
%{system_time: System.system_time()},
221+
%{repo: meta.repo, adapter: meta.adapter, config: opts}
222+
)
223+
217224
{:ok, pid}
218225

219226
other ->

test/ecto/repo/supervisor_test.exs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,41 @@ defmodule Ecto.Repo.SupervisorTest do
5252
:telemetry.detach(:telemetry_test)
5353
end
5454

55+
test "emits :started telemetry event after repo process starts" do
56+
:telemetry.attach_many(
57+
:telemetry_started_test,
58+
[[:ecto, :repo, :started]],
59+
&__MODULE__.handle_event/4,
60+
%{pid: self()}
61+
)
62+
63+
{:ok, _pid} = Ecto.TestRepo.start_link(name: :telemetry_started_test)
64+
65+
assert_receive {[:ecto, :repo, :started], %{system_time: _}, %{repo: Ecto.TestRepo, adapter: Ecto.TestAdapter, config: config}}
66+
assert config[:name] == :telemetry_started_test
67+
assert config[:database] == "hello"
68+
69+
:telemetry.detach(:telemetry_started_test)
70+
end
71+
72+
test "emits :started telemetry event even when repo modifies config in init" do
73+
:telemetry.attach_many(
74+
:telemetry_started_test_init,
75+
[[:ecto, :repo, :started]],
76+
&__MODULE__.handle_event/4,
77+
%{pid: self()}
78+
)
79+
80+
{:ok, _pid} = Ecto.TestRepoInitModify.start_link(name: :telemetry_started_test_init_modify)
81+
82+
assert_receive {[:ecto, :repo, :started], %{system_time: _}, %{repo: Ecto.TestRepoInitModify, adapter: Ecto.TestAdapter, config: config}}
83+
assert config[:name] == :telemetry_started_test_init_modify
84+
assert config[:database] == "hello"
85+
refute Keyword.has_key?(config, :telemetry_prefix)
86+
87+
:telemetry.detach(:telemetry_started_test_init)
88+
end
89+
5590
test "reads otp app configuration" do
5691
put_env(database: "hello")
5792
{:ok, config} = init_config(:runtime, __MODULE__, :ecto, [])

test/support/test_repo.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,14 @@ defmodule Ecto.TestRepo do
213213
end
214214
end
215215

216+
defmodule Ecto.TestRepoInitModify do
217+
use Ecto.Repo, otp_app: :ecto, adapter: Ecto.TestAdapter
218+
219+
def init(_type, opts) do
220+
opts = [url: "ecto://user:pass@local/hello"] ++ opts
221+
opts = Keyword.drop(opts, [:telemetry_prefix])
222+
{:ok, opts}
223+
end
224+
end
225+
216226
Ecto.TestRepo.start_link()

0 commit comments

Comments
 (0)