Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Phoenix Live Dashboard with Absinthe metrics #404

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ config :ret, RetWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "txlMOtlaY5x3crvOCko4uV5PM29ul3zGo1oBGNO3cDXx+7GHLKqt0gR9qzgThxb5",
render_errors: [view: RetWeb.ErrorView, accepts: ~w(html json)],
pubsub: [name: Ret.PubSub, adapter: Phoenix.PubSub.PG2]
pubsub: [name: Ret.PubSub, adapter: Phoenix.PubSub.PG2],
live_view: [signing_salt: "0b777554b23c402fc0388418a66bdf2a8dc5e9f4e09622c307da318f89994448"]

# Configures Elixir's Logger
config :logger, :console,
Expand Down
1 change: 1 addition & 0 deletions lib/ret/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ defmodule Ret.Application do
children = [
# Start the Ecto repository
supervisor(Ret.Repo, []),
RetWeb.Telemetry,
supervisor(RetWeb.Endpoint, []),
supervisor(RetWeb.Presence, []),

Expand Down
3 changes: 3 additions & 0 deletions lib/ret_web.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ defmodule RetWeb do
alias RetWeb.Router.Helpers, as: Routes
import RetWeb.Gettext
import RetWeb.ControllerHelpers
import Phoenix.LiveView.Controller
end
end

Expand All @@ -42,6 +43,7 @@ defmodule RetWeb do
alias RetWeb.Router.Helpers, as: Routes
import RetWeb.ErrorHelpers
import RetWeb.Gettext
import Phoenix.LiveView.Helpers
end
end

Expand All @@ -50,6 +52,7 @@ defmodule RetWeb do
use Phoenix.Router
import Plug.Conn
import Phoenix.Controller
import Phoenix.LiveView.Router
end
end

Expand Down
4 changes: 4 additions & 0 deletions lib/ret_web/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ defmodule RetWeb.Endpoint do

socket("/socket", RetWeb.SessionSocket, websocket: [check_origin: {RetWeb.Endpoint, :allowed_origin?, []}])

socket("/live", Phoenix.LiveView.Socket)

def get_cors_origins, do: Application.get_env(:ret, RetWeb.Endpoint)[:allowed_origins] |> String.split(",")
def get_cors_origin_urls, do: get_cors_origins() |> Enum.filter(&(&1 != "*")) |> Enum.map(&URI.parse/1)

Expand All @@ -24,6 +26,8 @@ defmodule RetWeb.Endpoint do
plug(Phoenix.CodeReloader)
end

plug Phoenix.LiveDashboard.RequestLogger,
param_key: "request_logger"
plug(Plug.RequestId)
plug(Plug.Logger)

Expand Down
7 changes: 7 additions & 0 deletions lib/ret_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ defmodule RetWeb.Router do
use Plug.ErrorHandler
use Sentry.Plug

import Phoenix.LiveDashboard.Router

pipeline :secure_headers do
plug(:put_secure_browser_headers)
plug(RetWeb.Plugs.AddCSP)
Expand Down Expand Up @@ -69,6 +71,11 @@ defmodule RetWeb.Router do
plug RetWeb.Context
end

scope "/dashboard", RetWeb do
pipe_through([:browser])
live_dashboard "/", metrics: RetWeb.Telemetry
end

scope "/health", RetWeb do
get("/", HealthController, :index)
end
Expand Down
43 changes: 43 additions & 0 deletions lib/ret_web/telemetry.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
defmodule RetWeb.Telemetry do
use Supervisor
import Telemetry.Metrics

def start_link(arg) do
Supervisor.start_link(__MODULE__, arg, name: __MODULE__)
end

def init(_arg) do
children = [
{:telemetry_poller, measurements: periodic_measurements(), period: 10_000}
]

Supervisor.init(children, strategy: :one_for_one)
end

def metrics do
[
# Phoenix Metrics
summary("phoenix.endpoint.stop.duration",
unit: {:native, :millisecond}
),
summary("phoenix.router_dispatch.stop.duration",
tags: [:route],
unit: {:native, :millisecond}
),
#Absinthe Metrics
summary("absinthe.execute.operation.stop.duration"),
summary("absinthe.resolve.field.stop.duration"),
summary("absinthe.middleware.batch.stop.duration"),

# VM Metrics
summary("vm.memory.total", unit: {:byte, :kilobyte}),
summary("vm.total_run_queue_lengths.total"),
summary("vm.total_run_queue_lengths.cpu"),
summary("vm.total_run_queue_lengths.io")
]
end

defp periodic_measurements do
[]
end
end
18 changes: 11 additions & 7 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,27 @@ defmodule Ret.Mixfile do
defp deps do
[
{:ecto_boot_migration, "~> 0.2.0"},
{:phoenix, "~> 1.4.0"},
{:phoenix_pubsub, "~> 1.1"},
{:phoenix, "~> 1.5.0"},
{:phoenix_pubsub, "~> 2.0"},
{:phoenix_ecto, "~> 4.0"},
{:plug, "~> 1.7"},
# Avoid 3.4.0 for now bc https://github.com/elixir-ecto/ecto/issues/3246
{:ecto, "~> 3.3.0"},
{:ecto_sql, "~> 3.3.0"},
{:absinthe, "~> 1.4"},
{:absinthe, "~> 1.5"},
{:dataloader, "~> 1.0.0"},
{:absinthe_plug, "~> 1.4"},
{:absinthe_phoenix, "~> 1.4.0"},
{:absinthe_plug, "~> 1.5"},
{:absinthe_phoenix, "~> 2.0"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.13"},
{:phoenix_live_view, "~> 0.14.4"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:telemetry_metrics, "~> 0.4"},
{:telemetry_poller, "~> 0.4"},
{:phoenix_live_dashboard, "~> 0.2.7"},
{:gettext, "~> 0.17"},
{:cowboy, "~> 2.6.3"},
{:plug_cowboy, "~> 2.0"},
{:cowboy, "~> 2.8"},
Copy link
Contributor Author

@johnshaughnessy johnshaughnessy Aug 7, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we upgrade cowboy to 2.8, then we need to make sure we are running erlang 22+

See phoenixframework/phoenix#3950

It looks like we are running 22.0 in habitat deploys:
https://github.com/mozilla/hubs-ops/blob/master/plans/erlang/habitat/plan.sh#L3

Need to confirm this and check terraform / hmc

{:plug_cowboy, "~> 2.1"},
{:distillery, "~> 2.0"},
{:peerage, "~> 1.0"},
{:httpoison, "~> 1.5"},
Expand Down
Loading