Skip to content

Commit

Permalink
ecto_info -> ecto_stats + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 16, 2020
1 parent cb1199c commit 9b32809
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 53 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ LiveDashboard provides real-time performance monitoring and debugging tools for

* ETS - See, filter, and search ETS tables (in-memory storage) in the current node

* Ecto Info - Shows index, table, and general usage about the underlying Ecto Repo storage
* Ecto Stats - Shows index, table, and general usage about the underlying Ecto Repo storage

The dashboard also works across nodes. If your nodes are connected via Distributed Erlang, then you can access information from node B while accessing the dashboard on node A.

Expand Down
35 changes: 0 additions & 35 deletions guides/ecto_info.md

This file was deleted.

35 changes: 35 additions & 0 deletions guides/ecto_stats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Configuring Ecto repository stats

This guide covers how to configure the LiveDashboard to stats from your underlying database. At the moment, these stats can only be show for Ecto repositories running on `Ecto.Adapters.Postgres`.

## Installing Ecto Stats

To enable the "Ecto Stats" functionality in your dashboard, you will need to do the three steps below:

1. Add the ecto_psql_extras dependency
2. Configure the dashboard
3. (optional) Install custom extensions

### Add the `ecto_psql_extras` dependency

In your `mix.exs`, add the following to your `deps`:

```elixir
{:ecto_psql_extras, "~> 0.2"},
```

### Configure the dashboard

The next step is to configure the dashboard. Go to the `live_dashboard` call in your router and list all of your repositories:

```elixir
live_dashboard "/dashboard", ecto_repos: [MyApp.Repo]
```

You want to list all repositories that connect to distinct databases. For example, if you have both `MyApp.Repo` and `MyApp.RepoAnother` but they connect to the same database, there is no benefit in listing both. Remember only Ecto repositories running on `Ecto.Adapters.Postgres` are currently supported.

If you want to disable the "Ecto Stats" option altogether, set `ecto_repos: []`.

### Install custom extensions

Once the repository page is enabled, some of the queries (Calls and Outliers) require the [pg_stat_statements](https://www.postgresql.org/docs/current/pgstatstatements.html) extension installed. If you wish to access said functionality, you must install the extension first, otherwise an error will be shown.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
defmodule Phoenix.LiveDashboard.EctoInfoPage do
defmodule Phoenix.LiveDashboard.EctoStatsPage do
@moduledoc false
use Phoenix.LiveDashboard.PageBuilder

@compile {:no_warn_undefined, [Decimal, EctoPSQLExtras]}
@disabled_link "https://hexdocs.pm/phoenix_live_dashboard/ecto_info.html"
@disabled_link "https://hexdocs.pm/phoenix_live_dashboard/ecto_stats.html"

@impl true
def init(%{repo: nil}), do: {:ok, %{repo: nil}}
Expand All @@ -17,15 +17,15 @@ defmodule Phoenix.LiveDashboard.EctoInfoPage do
@impl true
def menu_link(%{repo: nil}, _capabilities) do
if Code.ensure_loaded?(Ecto.Adapters.SQL) do
{:disabled, "Ecto Info", @disabled_link}
{:disabled, "Ecto Stats", @disabled_link}
else
:skip
end
end

@impl true
def menu_link(%{repo: repo}, capabilities) do
title = "#{repo |> inspect() |> String.replace(".", " ")} Info"
title = "#{repo |> inspect() |> String.replace(".", " ")} Stats"
extra = info_module_for(repo)

cond do
Expand Down Expand Up @@ -101,7 +101,10 @@ defmodule Phoenix.LiveDashboard.EctoInfoPage do
mapped =
if search do
Enum.filter(mapped, fn map ->
Enum.any?(searchable, &(Map.fetch!(map, &1) =~ search))
Enum.any?(searchable, fn column ->
value = Map.fetch!(map, column)
value && value =~ search
end)
end)
else
mapped
Expand Down
8 changes: 4 additions & 4 deletions lib/phoenix/live_dashboard/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ defmodule Phoenix.LiveDashboard.Router do
sockets: {Phoenix.LiveDashboard.SocketsPage, %{}},
ets: {Phoenix.LiveDashboard.EtsPage, %{}}
]
|> Enum.concat(ecto_info(ecto_repos))
|> Enum.concat(ecto_stats(ecto_repos))
|> Enum.concat(additional_pages)
|> Enum.map(fn {key, {module, opts}} ->
{session, requirements} = initialize_page(module, opts)
Expand All @@ -249,9 +249,9 @@ defmodule Phoenix.LiveDashboard.Router do
}
end

defp ecto_info(nil), do: [{:ecto_info, {Phoenix.LiveDashboard.EctoInfoPage, %{repo: nil}}}]
defp ecto_stats(nil), do: [{:ecto_stats, {Phoenix.LiveDashboard.EctoStatsPage, %{repo: nil}}}]

defp ecto_info(repos) do
defp ecto_stats(repos) do
for repo <- List.wrap(repos) do
page =
repo
Expand All @@ -260,7 +260,7 @@ defmodule Phoenix.LiveDashboard.Router do
|> Kernel.<>("_info")
|> String.to_atom()

{page, {Phoenix.LiveDashboard.EctoInfoPage, %{repo: repo}}}
{page, {Phoenix.LiveDashboard.EctoStatsPage, %{repo: repo}}}
end
end

Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ defmodule Phoenix.LiveDashboard.MixProject do

defp extras do
[
"guides/ecto_info.md",
"guides/ecto_stats.md",
"guides/metrics.md",
"guides/metrics_history.md",
"guides/os_mon.md",
Expand Down
58 changes: 58 additions & 0 deletions test/phoenix/live_dashboard/pages/ecto_stats_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule Phoenix.LiveDashboard.EctoStatsPageTest do
use ExUnit.Case, async: true

import Phoenix.ConnTest
import Phoenix.LiveViewTest
@endpoint Phoenix.LiveDashboardTest.Endpoint

alias Phoenix.LiveDashboard.EctoStatsPage
alias Phoenix.LiveDashboardTest.Repo
@link "https://hexdocs.pm/phoenix_live_dashboard/ecto_stats.html"

test "menu_link/2" do
assert {:disabled, "Ecto Stats", @link} = EctoStatsPage.menu_link(%{repo: nil}, %{})
assert :skip = EctoStatsPage.menu_link(%{repo: Repo}, %{processes: []})

assert {:ok, "Phoenix LiveDashboardTest Repo Stats"} =
EctoStatsPage.menu_link(%{repo: Repo}, %{processes: [Repo]})
end

test "renders" do
{:ok, live, _} = live(build_conn(), ecto_stats_path())
rendered = render(live)
assert rendered =~ "All locks"
assert rendered =~ "Extensions"
assert rendered =~ "Transactionid"
assert rendered =~ "Granted"
end

test "navs" do
{:ok, live, _} = live(build_conn(), ecto_stats_path(:extensions))
rendered = render(live)
assert rendered =~ "Default version"
assert rendered =~ "Installed version"
assert rendered =~ "fuzzystrmatch"
assert rendered =~ "hstore"
end

test "search" do
{:ok, live, _} = live(build_conn(), ecto_stats_path(:extensions, "hstore"))
rendered = render(live)
assert rendered =~ "Default version"
assert rendered =~ "Installed version"
refute rendered =~ "fuzzystrmatch"
assert rendered =~ "hstore"
end

defp ecto_stats_path() do
"/dashboard/nonode%40nohost/phoenix_live_dashboard_test_repo_info"
end

defp ecto_stats_path(nav) do
"#{ecto_stats_path()}?nav=#{nav}"
end

defp ecto_stats_path(nav, search) do
"#{ecto_stats_path()}?nav=#{nav}&search=#{search}"
end
end
6 changes: 3 additions & 3 deletions test/phoenix/live_dashboard/pages/sockets_page_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ defmodule Phoenix.LiveDashboard.SocketsPageTest do
assert rendered =~ first_address
assert rendered =~ second_address
assert rendered =~ "*:*"
assert rendered =~ "sockets out of 2"
assert rendered =~ ~r"sockets out of \d+"
assert rendered =~ sockets_href(50, "", :send_oct, :asc)

{:ok, live, _} = live(build_conn(), sockets_path(50, first_socket_port, :send_oct, :desc))

rendered = render(live)
assert rendered =~ first_address
refute rendered =~ second_address
assert rendered =~ "sockets out of 1"
assert rendered =~ ~r"sockets out of \d+"
assert rendered =~ sockets_href(50, first_socket_port, :send_oct, :asc)

{:ok, live, _} = live(build_conn(), sockets_path(50, "localhost", :send_oct, :desc))
rendered = render(live)
assert rendered =~ first_address
assert rendered =~ second_address
assert rendered =~ "sockets out of 2"
assert rendered =~ ~r"sockets out of \d+"
assert rendered =~ sockets_href(50, "localhost", :send_oct, :asc)
end

Expand Down
6 changes: 3 additions & 3 deletions test/phoenix/live_dashboard/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ defmodule Phoenix.LiveDashboard.RouterTest do
ports: {Phoenix.LiveDashboard.PortsPage, %{}},
sockets: {Phoenix.LiveDashboard.SocketsPage, %{}},
ets: {Phoenix.LiveDashboard.EtsPage, %{}},
ecto_info: {Phoenix.LiveDashboard.EctoInfoPage, %{repo: nil}}
ecto_stats: {Phoenix.LiveDashboard.EctoStatsPage, %{repo: nil}}
],
"requirements" => [{:application, :os_mon}]
} =
Expand All @@ -147,8 +147,8 @@ defmodule Phoenix.LiveDashboard.RouterTest do

assert [
ets: _,
foo_info: {Phoenix.LiveDashboard.EctoInfoPage, %{repo: Foo}},
bar_baz_info: {Phoenix.LiveDashboard.EctoInfoPage, %{repo: Bar.Baz}}
foo_info: {Phoenix.LiveDashboard.EctoStatsPage, %{repo: Foo}},
bar_baz_info: {Phoenix.LiveDashboard.EctoStatsPage, %{repo: Bar.Baz}}
] = Enum.take(pages, -3)
end
end
Expand Down
16 changes: 15 additions & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
System.put_env("PHX_DASHBOARD_TEST", "PHX_DASHBOARD_ENV_VALUE")

pg_url = System.get_env("PG_URL") || "postgres:[email protected]"

Application.put_env(:phoenix_live_dashboard, Phoenix.LiveDashboardTest.Repo,
url: "ecto://#{pg_url}/phx_dashboard_test"
)

defmodule Phoenix.LiveDashboardTest.Repo do
use Ecto.Repo, otp_app: :phoenix_live_dashboard, adapter: Ecto.Adapters.Postgres
end

_ = Ecto.Adapters.Postgres.storage_up(Phoenix.LiveDashboardTest.Repo.config())

Application.put_env(:phoenix_live_dashboard, Phoenix.LiveDashboardTest.Endpoint,
url: [host: "localhost", port: 4000],
secret_key_base: "Hu4qQN3iKzTV4fJxhorPQlA/osH9fAMtbtjVS58PFgfw3ja5Z18Q/WSNR9wP4OfW",
Expand Down Expand Up @@ -42,7 +54,8 @@ defmodule Phoenix.LiveDashboardTest.Router do
pipe_through :browser

live_dashboard "/dashboard",
metrics: Phoenix.LiveDashboardTest.Telemetry
metrics: Phoenix.LiveDashboardTest.Telemetry,
ecto_repos: [Phoenix.LiveDashboardTest.Repo]

live_dashboard "/config",
live_socket_path: "/custom/live",
Expand Down Expand Up @@ -86,6 +99,7 @@ Application.ensure_all_started(:os_mon)

Supervisor.start_link(
[
Phoenix.LiveDashboardTest.Repo,
{Phoenix.PubSub, name: Phoenix.LiveDashboardTest.PubSub, adapter: Phoenix.PubSub.PG2},
Phoenix.LiveDashboardTest.Endpoint
],
Expand Down

0 comments on commit 9b32809

Please sign in to comment.