From fc9061ac364ff70f20e62b2b1a5f6dd7e401c3a2 Mon Sep 17 00:00:00 2001 From: Vince Foley <39946+binaryseed@users.noreply.github.com> Date: Tue, 7 Apr 2020 02:11:53 -0700 Subject: [PATCH] Emit telemetry event upon Repo init (#3268) --- lib/ecto/repo/supervisor.ex | 6 ++++++ mix.exs | 1 + mix.lock | 1 + test/ecto/repo/supervisor_test.exs | 21 +++++++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/lib/ecto/repo/supervisor.ex b/lib/ecto/repo/supervisor.ex index 2755c4b9a9..e4d77a6cde 100644 --- a/lib/ecto/repo/supervisor.ex +++ b/lib/ecto/repo/supervisor.ex @@ -169,6 +169,12 @@ defmodule Ecto.Repo.Supervisor do def init({name, repo, otp_app, adapter, opts}) do case runtime_config(:supervisor, repo, otp_app, opts) do {:ok, opts} -> + :telemetry.execute( + [:ecto, :repo, :init], + %{system_time: System.system_time()}, + %{repo: repo, opts: opts} + ) + {:ok, child, meta} = adapter.init([repo: repo] ++ opts) cache = Ecto.Query.Planner.new_query_cache(name) meta = Map.merge(meta, %{repo: repo, cache: cache}) diff --git a/mix.exs b/mix.exs index 0eecd60431..acb3bf4a8e 100644 --- a/mix.exs +++ b/mix.exs @@ -30,6 +30,7 @@ defmodule Ecto.MixProject do defp deps do [ + {:telemetry, "~> 0.4"}, {:decimal, "~> 1.6 or ~> 2.0"}, {:jason, "~> 1.0", optional: true}, {:ex_doc, "~> 0.20", only: :docs} diff --git a/mix.lock b/mix.lock index 743c454ce5..50fa86869e 100644 --- a/mix.lock +++ b/mix.lock @@ -7,4 +7,5 @@ "makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "d4b316c7222a85bbaa2fd7c6e90e37e953257ad196dc229505137c5e505e9eff"}, "nimble_parsec": {:hex, :nimble_parsec, "0.5.3", "def21c10a9ed70ce22754fdeea0810dafd53c2db3219a0cd54cf5526377af1c6", [:mix], [], "hexpm", "589b5af56f4afca65217a1f3eb3fee7e79b09c40c742fddc1c312b3ac0b3399f"}, "poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm"}, + "telemetry": {:hex, :telemetry, "0.4.1", "ae2718484892448a24470e6aa341bc847c3277bfb8d4e9289f7474d752c09c7f", [:rebar3], [], "hexpm", "4738382e36a0a9a2b6e25d67c960e40e1a2c95560b9f936d8e29de8cd858480f"}, } diff --git a/test/ecto/repo/supervisor_test.exs b/test/ecto/repo/supervisor_test.exs index 069e7a61b1..138cb61253 100644 --- a/test/ecto/repo/supervisor_test.exs +++ b/test/ecto/repo/supervisor_test.exs @@ -22,6 +22,27 @@ defmodule Ecto.Repo.SupervisorTest do user: "invalid", username: "user"] end + def handle_event(event, measurements, metadata, %{pid: pid}) do + send(pid, {event, measurements, metadata}) + end + + test "emits telemetry event upon repo start" do + :telemetry.attach_many( + :telemetry_test, + [[:ecto, :repo, :init]], + &__MODULE__.handle_event/4, + %{pid: self()} + ) + + Ecto.TestRepo.start_link(name: :telemetry_test) + + assert_receive {[:ecto, :repo, :init], _, %{repo: Ecto.TestRepo, opts: opts}} + assert opts[:telemetry_prefix] == [:ecto, :test_repo] + assert opts[:name] == :telemetry_test + + :telemetry.detach(:telemetry_test) + end + test "reads otp app configuration" do put_env(database: "hello") {:ok, config} = runtime_config(:runtime, __MODULE__, :ecto, [])