Skip to content

Commit 5670ec3

Browse files
committed
Add LiveDashboard in dev
1 parent 99a0c07 commit 5670ec3

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed

config/dev.exs

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ config :ex_rss, ExRssWeb.Endpoint,
4747
]
4848
]
4949

50+
# Enable dev routes for dashboard and mailbox
51+
config :ex_rss, dev_routes: true
52+
5053
# Set a higher stacktrace during development. Avoid configuring such
5154
# in production as building large stacktraces may be expensive.
5255
config :phoenix, :stacktrace_depth, 20

lib/ex_rss/application.ex

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule ExRss.Application do
55
def start(_type, _args) do
66
children =
77
[
8+
ExRssWeb.Telemetry,
89
{Task.Supervisor, name: ExRss.TaskSupervisor},
910
ExRss.Repo,
1011
{Phoenix.PubSub, name: ExRss.PubSub},

lib/ex_rss_web/endpoint.ex

+5
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ defmodule ExRssWeb.Endpoint do
3737
plug Phoenix.CodeReloader
3838
end
3939

40+
plug Phoenix.LiveDashboard.RequestLogger,
41+
param_key: "request_logger",
42+
cookie_key: "request_logger"
43+
4044
plug Plug.RequestId
4145
plug Plug.Logger
46+
plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]
4247

4348
plug Plug.Parsers,
4449
parsers: [:urlencoded, :multipart, :json],

lib/ex_rss_web/router.ex

+17
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,23 @@ defmodule ExRssWeb.Router do
3131
plug ExRssWeb.Plug.AssignApiToken, @api_token_salt
3232
end
3333

34+
# Enable LiveDashboard and Swoosh mailbox preview in development
35+
if Application.compile_env(:ex_rss, :dev_routes) do
36+
# If you want to use the LiveDashboard in production, you should put it
37+
# behind authentication and allow only admins to access it. If your
38+
# application does not have an admins-only section yet, you can use
39+
# Plug.BasicAuth to set up some basic authentication as long as you are
40+
# also using SSL (which you should anyway).
41+
import Phoenix.LiveDashboard.Router
42+
43+
scope "/dev" do
44+
pipe_through :browser
45+
46+
live_dashboard "/dashboard", metrics: ExRssWeb.Telemetry
47+
forward "/mailbox", Plug.Swoosh.MailboxPreview
48+
end
49+
end
50+
3451
scope "/", ExRssWeb do
3552
pipe_through [:browser, :redirect_if_user_is_authenticated]
3653

lib/ex_rss_web/telemetry.ex

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
defmodule ExRssWeb.Telemetry do
2+
use Supervisor
3+
import Telemetry.Metrics
4+
5+
def start_link(arg) do
6+
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
7+
end
8+
9+
@impl true
10+
def init(_arg) do
11+
children = [
12+
# Telemetry poller will execute the given period measurements
13+
# every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics
14+
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
15+
# Add reporters as children of your supervision tree.
16+
# {Telemetry.Metrics.ConsoleReporter, metrics: metrics()}
17+
]
18+
19+
Supervisor.init(children, strategy: :one_for_one)
20+
end
21+
22+
def metrics do
23+
[
24+
# Phoenix Metrics
25+
summary("phoenix.endpoint.start.system_time",
26+
unit: {:native, :millisecond}
27+
),
28+
summary("phoenix.endpoint.stop.duration",
29+
unit: {:native, :millisecond}
30+
),
31+
summary("phoenix.router_dispatch.start.system_time",
32+
tags: [:route],
33+
unit: {:native, :millisecond}
34+
),
35+
summary("phoenix.router_dispatch.exception.duration",
36+
tags: [:route],
37+
unit: {:native, :millisecond}
38+
),
39+
summary("phoenix.router_dispatch.stop.duration",
40+
tags: [:route],
41+
unit: {:native, :millisecond}
42+
),
43+
summary("phoenix.socket_connected.duration",
44+
unit: {:native, :millisecond}
45+
),
46+
summary("phoenix.channel_joined.duration",
47+
unit: {:native, :millisecond}
48+
),
49+
summary("phoenix.channel_handled_in.duration",
50+
tags: [:event],
51+
unit: {:native, :millisecond}
52+
),
53+
54+
# Database Metrics
55+
summary("ex_rss.repo.query.total_time",
56+
unit: {:native, :millisecond},
57+
description: "The sum of the other measurements"
58+
),
59+
summary("ex_rss.repo.query.decode_time",
60+
unit: {:native, :millisecond},
61+
description: "The time spent decoding the data received from the database"
62+
),
63+
summary("ex_rss.repo.query.query_time",
64+
unit: {:native, :millisecond},
65+
description: "The time spent executing the query"
66+
),
67+
summary("ex_rss.repo.query.queue_time",
68+
unit: {:native, :millisecond},
69+
description: "The time spent waiting for a database connection"
70+
),
71+
summary("ex_rss.repo.query.idle_time",
72+
unit: {:native, :millisecond},
73+
description:
74+
"The time the connection spent waiting before being checked out for the query"
75+
),
76+
77+
# VM Metrics
78+
summary("vm.memory.total", unit: {:byte, :kilobyte}),
79+
summary("vm.total_run_queue_lengths.total"),
80+
summary("vm.total_run_queue_lengths.cpu"),
81+
summary("vm.total_run_queue_lengths.io")
82+
]
83+
end
84+
85+
defp periodic_measurements do
86+
[
87+
# A module, function and arguments to be invoked periodically.
88+
# This function must call :telemetry.execute/3 and a metric must be added above.
89+
# {ExRssWeb, :count_users, []}
90+
]
91+
end
92+
end

mix.exs

+3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ defmodule ExRss.Mixfile do
4444
{:phoenix_html_helpers, "~> 1.0"},
4545
{:phoenix_live_reload, "~> 1.0", only: :dev},
4646
{:phoenix_live_view, "~> 1.0"},
47+
{:phoenix_live_dashboard, "~> 0.8"},
4748
{:swoosh, "~> 1.4"},
4849
{:finch, "~> 0.19"},
50+
{:telemetry_metrics, "~> 1.0"},
51+
{:telemetry_poller, "~> 1.0"},
4952
{:gettext, "~> 0.11"},
5053
{:floki, "~> 0.24"},
5154
{:feeder_ex, "~> 1.1"},

mix.lock

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"phoenix_ecto": {:hex, :phoenix_ecto, "4.6.3", "f686701b0499a07f2e3b122d84d52ff8a31f5def386e03706c916f6feddf69ef", [:mix], [{:ecto, "~> 3.5", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 3.0 or ~> 4.1", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:postgrex, "~> 0.16 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}], "hexpm", "909502956916a657a197f94cc1206d9a65247538de8a5e186f7537c895d95764"},
4242
"phoenix_html": {:hex, :phoenix_html, "4.2.0", "83a4d351b66f472ebcce242e4ae48af1b781866f00ef0eb34c15030d4e2069ac", [:mix], [], "hexpm", "9713b3f238d07043583a94296cc4bbdceacd3b3a6c74667f4df13971e7866ec8"},
4343
"phoenix_html_helpers": {:hex, :phoenix_html_helpers, "1.0.1", "7eed85c52eff80a179391036931791ee5d2f713d76a81d0d2c6ebafe1e11e5ec", [:mix], [{:phoenix_html, "~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: true]}], "hexpm", "cffd2385d1fa4f78b04432df69ab8da63dc5cf63e07b713a4dcf36a3740e3090"},
44+
"phoenix_live_dashboard": {:hex, :phoenix_live_dashboard, "0.8.6", "7b1f0327f54c9eb69845fd09a77accf922f488c549a7e7b8618775eb603a62c7", [:mix], [{:ecto, "~> 3.6.2 or ~> 3.7", [hex: :ecto, repo: "hexpm", optional: true]}, {:ecto_mysql_extras, "~> 0.5", [hex: :ecto_mysql_extras, repo: "hexpm", optional: true]}, {:ecto_psql_extras, "~> 0.7", [hex: :ecto_psql_extras, repo: "hexpm", optional: true]}, {:ecto_sqlite3_extras, "~> 1.1.7 or ~> 1.2.0", [hex: :ecto_sqlite3_extras, repo: "hexpm", optional: true]}, {:mime, "~> 1.6 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:phoenix_live_view, "~> 0.19 or ~> 1.0", [hex: :phoenix_live_view, repo: "hexpm", optional: false]}, {:telemetry_metrics, "~> 0.6 or ~> 1.0", [hex: :telemetry_metrics, repo: "hexpm", optional: false]}], "hexpm", "1681ab813ec26ca6915beb3414aa138f298e17721dc6a2bde9e6eb8a62360ff6"},
4445
"phoenix_live_reload": {:hex, :phoenix_live_reload, "1.5.3", "f2161c207fda0e4fb55165f650f7f8db23f02b29e3bff00ff7ef161d6ac1f09d", [:mix], [{:file_system, "~> 0.3 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm", "b4ec9cd73cb01ff1bd1cac92e045d13e7030330b74164297d1aee3907b54803c"},
4546
"phoenix_live_view": {:hex, :phoenix_live_view, "1.0.1", "5389a30658176c0de816636ce276567478bffd063c082515a6e8368b8fc9a0db", [:mix], [{:floki, "~> 0.36", [hex: :floki, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix, "~> 1.6.15 or ~> 1.7.0", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 3.3 or ~> 4.0", [hex: :phoenix_html, repo: "hexpm", optional: false]}, {:phoenix_template, "~> 1.0", [hex: :phoenix_template, repo: "hexpm", optional: false]}, {:phoenix_view, "~> 2.0", [hex: :phoenix_view, repo: "hexpm", optional: true]}, {:plug, "~> 1.15", [hex: :plug, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "c0f517e6f290f10dbb94343ac22e0109437fb1fa6f0696e7c73967b789c1c285"},
4647
"phoenix_pubsub": {:hex, :phoenix_pubsub, "2.1.3", "3168d78ba41835aecad272d5e8cd51aa87a7ac9eb836eabc42f6e57538e3731d", [:mix], [], "hexpm", "bba06bc1dcfd8cb086759f0edc94a8ba2bc8896d5331a1e2c2902bf8e36ee502"},
@@ -54,6 +55,8 @@
5455
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
5556
"swoosh": {:hex, :swoosh, "1.17.5", "14910d267a2633d4335917b37846e376e2067815601592629366c39845dad145", [:mix], [{:bandit, ">= 1.0.0", [hex: :bandit, repo: "hexpm", optional: true]}, {:cowboy, "~> 1.1 or ~> 2.4", [hex: :cowboy, repo: "hexpm", optional: true]}, {:ex_aws, "~> 2.1", [hex: :ex_aws, repo: "hexpm", optional: true]}, {:finch, "~> 0.6", [hex: :finch, repo: "hexpm", optional: true]}, {:gen_smtp, "~> 0.13 or ~> 1.0", [hex: :gen_smtp, repo: "hexpm", optional: true]}, {:hackney, "~> 1.9", [hex: :hackney, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}, {:mail, "~> 0.2", [hex: :mail, repo: "hexpm", optional: true]}, {:mime, "~> 1.1 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mua, "~> 0.2.3", [hex: :mua, repo: "hexpm", optional: true]}, {:multipart, "~> 0.4", [hex: :multipart, repo: "hexpm", optional: true]}, {:plug, "~> 1.9", [hex: :plug, repo: "hexpm", optional: true]}, {:plug_cowboy, ">= 1.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:req, "~> 0.5 or ~> 1.0", [hex: :req, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.2 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "629113d477bc82c4c3bffd15a25e8becc1c7ccc0f0e67743b017caddebb06f04"},
5657
"telemetry": {:hex, :telemetry, "1.3.0", "fedebbae410d715cf8e7062c96a1ef32ec22e764197f70cda73d82778d61e7a2", [:rebar3], [], "hexpm", "7015fc8919dbe63764f4b4b87a95b7c0996bd539e0d499be6ec9d7f3875b79e6"},
58+
"telemetry_metrics": {:hex, :telemetry_metrics, "1.0.0", "29f5f84991ca98b8eb02fc208b2e6de7c95f8bb2294ef244a176675adc7775df", [:mix], [{:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f23713b3847286a534e005126d4c959ebcca68ae9582118ce436b521d1d47d5d"},
59+
"telemetry_poller": {:hex, :telemetry_poller, "1.1.0", "58fa7c216257291caaf8d05678c8d01bd45f4bdbc1286838a28c4bb62ef32999", [:rebar3], [{:telemetry, "~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "9eb9d9cbfd81cbd7cdd24682f8711b6e2b691289a0de6826e58452f28c103c8f"},
5760
"timex": {:hex, :timex, "3.7.11", "bb95cb4eb1d06e27346325de506bcc6c30f9c6dea40d1ebe390b262fad1862d1", [:mix], [{:combine, "~> 0.10", [hex: :combine, repo: "hexpm", optional: false]}, {:gettext, "~> 0.20", [hex: :gettext, repo: "hexpm", optional: false]}, {:tzdata, "~> 1.1", [hex: :tzdata, repo: "hexpm", optional: false]}], "hexpm", "8b9024f7efbabaf9bd7aa04f65cf8dcd7c9818ca5737677c7b76acbc6a94d1aa"},
5861
"tzdata": {:hex, :tzdata, "1.1.1", "20c8043476dfda8504952d00adac41c6eda23912278add38edc140ae0c5bcc46", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "a69cec8352eafcd2e198dea28a34113b60fdc6cb57eb5ad65c10292a6ba89787"},
5962
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},

0 commit comments

Comments
 (0)