### Run with Ecto Options Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md Examples of running the development version with specific Ecto repository configurations for PostgreSQL, MySQL, and SQLite. ```bash mix dev --postgres ``` ```bash mix dev --mysql ``` ```bash mix dev --sqlite ``` -------------------------------- ### Configure SQLite Extras Options Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/ecto_stats.md Configures the `ecto_sqlite3_extras_options` for SQLite in LiveDashboard. This example shows an empty options list. ```elixir live_dashboard "/dashboard", ecto_repos: [MyApp.Repo], ecto_sqlite3_extras_options: [] ``` -------------------------------- ### Add Dashboard Access (Production with Basic Auth) Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md This snippet provides an example of securing LiveDashboard access in production using basic HTTP authentication. It defines a pipeline with a `Plug.BasicAuth` implementation. ```elixir # lib/my_app_web/router.ex use MyAppWeb, :router import Phoenix.LiveDashboard.Router ... pipeline :admins_only do plug :admin_basic_auth end scope "/" do pipe_through [:browser, :admins_only] live_dashboard "/dashboard" end defp admin_basic_auth(conn, _opts) do username = System.fetch_env!("AUTH_USERNAME") password = System.fetch_env!("AUTH_PASSWORD") Plug.BasicAuth.basic_auth(conn, username: username, password: password) end ``` -------------------------------- ### Define Telemetry Module and Metrics Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics.md Provides an example of a `Telemetry` module in Elixir that uses `Supervisor` and `Telemetry.Metrics`. It defines child specs for `telemetry_poller` and lists various metrics for Phoenix, Ecto, and VM, including summaries for duration, time, and memory. ```elixir defmodule MyAppWeb.Telemetry do use Supervisor import Telemetry.Metrics def start_link(arg) do Supervisor.start_link(__MODULE__, arg, name: __MODULE__) end @impl true def init(_arg) do children = [ # Telemetry poller will execute the given period measurements # every 10_000ms. Learn more here: https://hexdocs.pm/telemetry_metrics {:telemetry_poller, measurements: periodic_measurements(), period: 10_000} # Add reporters as children of your supervision tree. # {Telemetry.Metrics.ConsoleReporter, metrics: metrics()} ] 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} ), # Database Time Metrics summary("my_app.repo.query.total_time", unit: {:native, :millisecond}), summary("my_app.repo.query.decode_time", unit: {:native, :millisecond}), summary("my_app.repo.query.query_time", unit: {:native, :millisecond}), summary("my_app.repo.query.queue_time", unit: {:native, :millisecond}), summary("my_app.repo.query.idle_time", unit: {:native, :millisecond}), # 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 ``` -------------------------------- ### Add Dashboard Access (Production with Auth) Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md This snippet shows how to secure LiveDashboard access in production environments using a custom pipeline for authentication. It includes an example using `fetch_current_admin` and `require_authenticated_admin`. ```elixir # lib/my_app_web/router.ex use MyAppWeb, :router import Phoenix.LiveDashboard.Router ... pipeline :admins_only do plug :fetch_current_admin plug :require_authenticated_admin end scope "/" do pipe_through [:browser, :admins_only] live_dashboard "/dashboard" end ``` -------------------------------- ### Configure PostgreSQL Long Running Queries Threshold Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/ecto_stats.md Configures the `long_running_queries` option for PostgreSQL in LiveDashboard by passing `ecto_psql_extras_options`. This example sets the threshold to '200 milliseconds'. ```elixir live_dashboard "/dashboard", ecto_repos: [MyApp.Repo], ecto_psql_extras_options: [long_running_queries: [threshold: "200 milliseconds"]] ``` -------------------------------- ### Configure MySQL/MariaDB Long Running Queries Threshold Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/ecto_stats.md Configures the `long_running_queries` option for MySQL/MariaDB in LiveDashboard by passing `ecto_mysql_extras_options`. This example sets the threshold to 200. ```elixir live_dashboard "/dashboard", ecto_repos: [MyApp.Repo], ecto_mysql_extras_options: [long_running_queries: [threshold: 200]] ``` -------------------------------- ### Install Request Logger Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/request_logger.md Adds the Phoenix.LiveDashboard.RequestLogger plug to your application's endpoint. This plug enables streaming of request logs via URL parameters or cookies. It should be placed before Plug.RequestId. ```elixir plug Phoenix.LiveDashboard.RequestLogger, param_key: "request_logger", cookie_key: "request_logger" ``` -------------------------------- ### Run Development Version Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md Commands to set up and run a development version of the Phoenix Live Dashboard. ```bash mix setup mix dev ``` -------------------------------- ### Run with Interactive Shell Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md How to run the development version with an interactive IEx shell, allowing for passing flags. ```bash iex -S mix dev [flags] ``` -------------------------------- ### Reporter Options Configuration Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics.md Demonstrates how to pass reporter options to a counter metric. Explains available options like `:nav`, `:prune_threshold`, `:refresh_interval`, and `:bucket_size` and their default values. ```elixir counter("my_app.counter", reporter_options: [...]) ``` -------------------------------- ### Configure LiveView Signing Salt Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md This snippet demonstrates how to configure the signing salt for LiveView in your application's configuration file. This is a necessary step for LiveDashboard to function correctly. ```elixir # config/config.exs config :my_app, MyAppWeb.Endpoint, live_view: [signing_salt: "SECRET_SALT"] ``` -------------------------------- ### Configure LiveDashboard Router Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics.md Shows how to configure the `live_dashboard` call in the router to enable the metrics functionality by passing the `metrics` option with the defined Telemetry module. ```elixir live_dashboard "/dashboard", metrics: MyAppWeb.Telemetry ``` -------------------------------- ### Reporter Options Details Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics.md Provides detailed explanations for each reporter option: `:nav` for metric grouping, `:prune_threshold` for data pruning, `:refresh_interval` for refresh rate, and `:bucket_size` for histogram buckets. ```APIDOC :nav Configures the group the metric belongs to. By default the group is the first part of the name. For example, `counter("my_app.counter")` defaults to group "my_app". :prune_threshold The maximum number of data points. When the threshold is reached, the chart data will be pruned by half. Default is `1_000`. :refresh_interval The interval in milliseconds at which the chart will be refreshed. The default value is `1_000`. :bucket_size The unit width of each bucket. This option only applies to `distribution` histograms. The default value is `20`. ``` -------------------------------- ### Enable os_mon Application Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/os_mon.md Adds the 'os_mon' application to the extra applications list in the mix.exs file to enable OS Data collection in LiveDashboard. This requires the 'os_mon' Erlang application to be available in the Erlang distribution. ```elixir def application do [ ..., extra_applications: [:logger, :runtime_tools, :os_mon] ] end ``` -------------------------------- ### Add PostgreSQL Ecto Extras Dependency Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/ecto_stats.md Adds the `ecto_psql_extras` dependency to your project's `mix.exs` file to enable PostgreSQL statistics in LiveDashboard. ```elixir {:ecto_psql_extras, "~> 0.6"} ``` -------------------------------- ### Add phoenix_live_dashboard Dependency Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md This snippet shows how to add the phoenix_live_dashboard dependency to your project's mix.exs file. After adding the dependency, run `mix deps.get` to fetch it. ```elixir def deps do [ {:phoenix_live_dashboard, "~> 0.7"} ] end ``` -------------------------------- ### Add Telemetry Module to Application Supervisor Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics.md Demonstrates how to include the custom Telemetry module as a child in the main application's supervision tree, typically found in `lib/my_app/application.ex`. ```elixir children = [ MyApp.Repo, MyAppWeb.Telemetry, MyAppWeb.Endpoint, ... ] ``` -------------------------------- ### Initialize MetricsStorage in Application Supervisor Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics_history.md This snippet shows how to add the `MyAppWeb.MetricsStorage` GenServer to your application's supervisor tree. It initializes the storage with a list of metrics, typically obtained from `MyAppWeb.Telemetry.metrics/0`. ```elixir # Start genserver to store transient metrics {MyAppWeb.MetricsStorage, MyAppWeb.Telemetry.metrics()}, ``` -------------------------------- ### Add MySQL/MariaDB Ecto Extras Dependency Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/ecto_stats.md Adds the `ecto_mysql_extras` dependency to your project's `mix.exs` file to enable MySQL/MariaDB statistics in LiveDashboard. ```elixir {:ecto_mysql_extras, "~> 0.3"} ``` -------------------------------- ### Add SQLite Ecto Extras Dependency Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/ecto_stats.md Adds the `ecto_sqlite3_extras` dependency to your project's `mix.exs` file to enable SQLite statistics in LiveDashboard. ```elixir {:ecto_sqlite3_extras, "~> 1.2.0"} ``` -------------------------------- ### Add LiveView Socket Declaration Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md This snippet shows how to add the Phoenix.LiveView.Socket declaration to your endpoint configuration. This enables LiveView functionality, which LiveDashboard relies on. ```elixir socket "/live", Phoenix.LiveView.Socket ``` -------------------------------- ### Add Telemetry Dependencies Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics.md Specifies the necessary dependencies for enabling metrics functionality in `mix.exs`. These are `telemetry_poller` and `telemetry_metrics`. ```elixir {:telemetry_poller, "~> 0.4"}, {:telemetry_metrics, "~> 0.4"}, ``` -------------------------------- ### Configure LiveDashboard with Metrics History Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics_history.md This snippet shows how to configure the Phoenix LiveDashboard in `router.ex` to use a custom module for storing and retrieving metrics history. It specifies the `metrics_history` option with a module, function, and arguments. ```elixir live_dashboard "/dashboard", metrics: MyAppWeb.Telemetry, metrics_history: {MyAppWeb.MetricsStorage, :metrics_history, []} ``` -------------------------------- ### Add Circular Buffer Dependency Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics_history.md This snippet demonstrates how to add the `circular_buffer` package as a dependency in your `mix.exs` file. This is required for the custom metrics storage implementation. ```elixir {:circular_buffer, "~> 0.4.0"} ``` -------------------------------- ### Add Dashboard Access (Development) Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md This snippet illustrates how to add LiveDashboard access to your router for development-only usage. It uses a conditional scope based on the environment. ```elixir # lib/my_app_web/router.ex use MyAppWeb, :router import Phoenix.LiveDashboard.Router ... if Mix.env() == :dev do scope "/" do pipe_through :browser live_dashboard "/dashboard" end end ``` -------------------------------- ### Custom Metrics Storage Module (MyAppWeb.MetricsStorage) Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/metrics_history.md This Elixir module implements a GenServer to store metrics history using a circular buffer. It handles attaching telemetry event handlers, processing incoming metrics data, and providing historical data upon request. The `metrics_history/1` function is the public interface for fetching data. ```elixir defmodule MyAppWeb.MetricsStorage do use GenServer @history_buffer_size 50 def metrics_history(metric) do GenServer.call(__MODULE__, {:data, metric}) end def start_link(args) do GenServer.start_link(__MODULE__, args, name: __MODULE__) end @impl true def init(metrics) do Process.flag(:trap_exit, true) metric_histories_map = metrics |> Enum.map(fn metric -> attach_handler(metric) {metric, CircularBuffer.new(@history_buffer_size)} end) |> Map.new() {:ok, metric_histories_map} end @impl true def terminate(_, metrics) do for metric <- metrics do :telemetry.detach({__MODULE__, metric, self()}) end :ok end defp attach_handler(%{event_name: name_list} = metric) do :telemetry.attach( {__MODULE__, metric, self()}, name_list, &__MODULE__.handle_event/4, metric ) end def handle_event(_event_name, data, metadata, metric) do if data = Phoenix.LiveDashboard.extract_datapoint_for_metric(metric, data, metadata) do GenServer.cast(__MODULE__, {:telemetry_metric, data, metric}) end end @impl true def handle_cast({:telemetry_metric, data, metric}, state) do {:noreply, update_in(state[metric], &CircularBuffer.insert(&1, data))} end @impl true def handle_call({:data, metric}, _from, state) do if history = state[metric] do {:reply, CircularBuffer.to_list(history), state} else {:reply, [], state} end end end ``` -------------------------------- ### Configure LiveDashboard with Ecto Repositories Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/guides/ecto_stats.md Configures the LiveDashboard in your router to list specific Ecto repositories. This is optional and used to restrict which repositories are displayed. Set `ecto_repos: []` to disable Ecto stats. ```elixir live_dashboard "/dashboard", ecto_repos: [MyApp.Repo] ``` -------------------------------- ### Discard Dist Directory Changes Source: https://github.com/phoenixframework/phoenix_live_dashboard/blob/main/README.md Command to discard any local changes made to the 'dist' directory before submitting a pull request. ```bash git restore dist ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.