Phoenix LiveDashboard

repository·main·Indexed 24 days ago

https://github.com/phoenixframework/phoenix_live_dashboard

A real-time performance monitoring and debugging tool for Phoenix applications. It provides a web-based interface to visualize system metrics, process trees, database stats, and telemetry data. Features include OS data monitoring, request logging, ETS and socket monitoring, and Ecto repository statistics for PostgreSQL, MySQL/MariaDB, and SQLite. It supports distributed monitoring for remote nodes and allows for the implementation of custom metrics history providers.

Tokens
4.4K
Snippets
14
Records
22
Agent score
80%

What's inside phoenix_live_dashboard

  1. Overview of LiveDashboard monitoring modules

    main

    LiveDashboard provides several real-time monitoring modules:

    • Home: General system information.
    • OS Data: CPU, Memory, and Disk usage.
    • Metrics: Real-time charts visualizing :telemetry events.
    • Request logging: Logs associated with specific requests.
    • Applications: Supervision trees, filtering, and searching for applications in the current node.
    • Processes: Search and filter processes in the current node.
    • Ports: I/O port monitoring.
    • Sockets: TCP/UDP socket monitoring.
    • ETS: In-memory ETS table monitoring.
    • Ecto Stats: Usage statistics for Ecto Repo storage (indexes, tables, etc.).

    Distributed Monitoring: If your nodes are connected via Distributed Erlang, you can access information from remote nodes while viewing the dashboard on your local node.

  2. Implement a metrics history provider

    main

    A metrics history provider is a module used by LiveDashboard to retrieve historical data points. When LiveDashboard requests history for a specific metric, it calls the configured function.

    Data Format Requirements:

    • The function must return a list of maps.
    • Each map must contain exactly these keys: :label, :measurement, and :time.
    • If no data is available, return an empty list [].
    • If Phoenix.LiveDashboard.extract_datapoint_for_metric/4 returns nil for a specific data point, that point should not be included in the history.

    Example Implementation Pattern: Using a GenServer with a circular buffer is a common way to store transient metrics in memory. You can also use Redis, an ETS table, or a database.

  3. How Telemetry.Metrics map to LiveDashboard charts

    main

    LiveDashboard integrates with :telemetry by converting different Telemetry.Metrics types into specific chart visualizations:

    Telemetry.MetricsY-Axis Value(s)
    last_valueAlways set to an absolute value
    counterAlways increased by 1
    sumAlways increased/decreased by an absolute value
    summaryValue/Min/Max/Avg
    distributionTotal number of events in individual buckets
  4. Configure LiveView for LiveDashboard

    main

    LiveDashboard requires LiveView to be configured. You must update your endpoint configuration to include a signing_salt and declare a socket for LiveView.

    In your config/config.exs:

    config :my_app, MyAppWeb.Endpoint,
      live_view: [signing_salt: "SECRET_SALT"]

    In your endpoint file (e.g., lib/my_app_web/endpoint.ex):

    socket "/live", Phoenix.LiveView.Socket
    # config/config.exs
    config :my_app, MyAppWeb.Endpoint,
      live_view: [signing_salt: "SECRET_SALT"]
    
    socket "/live", Phoenix.LiveView.Socket
  5. Install Ecto Stats for MySQL/MariaDB

    main

    To enable Ecto repository statistics for MySQL or MariaDB in LiveDashboard, add the ecto_mysql_extras dependency to your mix.exs file.

    Configuration Notes:

    • The database user must have access to specific system-level databases (see ecto_mysql_extras documentation).
    • For MariaDB, ensure performance_schema=ON is set in your my.cnf file and restart the server.
      {:ecto_mysql_extras, "~> 0.3"}
  6. Install Phoenix LiveDashboard

    main

    To install Phoenix LiveDashboard, follow these three steps:

    1. Add the dependency: Add {:phoenix_live_dashboard, "~> 0.7"} to your mix.exs file and run mix deps.get.
    2. Configure LiveView: Ensure your endpoint has a signing_salt configured and a Phoenix.LiveView.Socket declaration.
    3. Add dashboard access: Update your router to include the Phoenix.LiveDashboard.Router and define a live_dashboard route.
    def deps do
      [
        {:phoenix_live_dashboard, "~> 0.7"}
      ]
    end
  7. Add dashboard access for development-only usage

    main

    To enable the dashboard only during development, import Phoenix.LiveDashboard.Router in your router and wrap the live_dashboard route in a Mix.env() == :dev check.

    # 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
  8. Install the Phoenix LiveDashboard request logger

    main

    To install the request logger, add the Phoenix.LiveDashboard.RequestLogger plug to your lib/my_app_web/endpoint.ex file. It should be placed immediately before Plug.RequestId.

    For standard web applications, use both param_key and cookie_key. If your application is an API-only application that does not use cookies, you should omit the cookie_key option.

    plug Phoenix.LiveDashboard.RequestLogger,
      param_key: "request_logger",
      cookie_key: "request_logger"
  9. Enable OS Data in LiveDashboard by enabling `os_mon`

    main

    LiveDashboard retrieves OS-level metrics via the Erlang os_mon application. To enable this data, you must add :os_mon to the extra_applications list in your project's mix.exs file.

    Note: On some operating systems where Erlang is split into multiple packages, you may need to install the specific Erlang package for os_mon (e.g., erlang-os-mon) via your system's package manager.

      def application do
        [
          ...,
          extra_applications: [:logger, :runtime_tools, :os_mon]
        ]
      end