# Add Prometheus Metrics to a Phoenix LiveView App

Yes. An agent adds PromEx to a Phoenix LiveView app, exports endpoint, router, Ecto, LiveView and BEAM metrics, compiles and tests it, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-prometheus-metrics/phoenix-liveview/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads what the app already measures. In an app laid out like fly-apps/live_beats, `mix.exs` depends on `phoenix ~> 1.7.1` (1.7.12 in `mix.lock`), `phoenix_live_view` from its GitHub main branch, `phoenix_live_dashboard ~> 0.8`, `ecto_sql ~> 3.11`, `telemetry_metrics ~> 0.6` (0.6.1 locked), `telemetry_poller ~> 1.0` (1.0.0 locked) and `plug_cowboy ~> 2.5`, beside `flame`, `bumblebee` and `exla` for the transcription worker. `lib/live_beats_web/telemetry.ex` already declares the metrics the dashboard shows: `phoenix.endpoint.stop.duration`, `phoenix.router_dispatch.stop.duration` tagged by route, the five `live_beats.repo.query.*` timings and the `vm.memory` and run-queue summaries, with a `ConsoleReporter` left commented out. `lib/live_beats/application.ex` starts the children in order: the whisper serving, `DNSCluster`, a task supervisor, `LiveBeats.Repo`, `LiveBeatsWeb.Telemetry`, PubSub, presence, Finch, a `FLAME.Pool`, the endpoint and a songs cleaner. `lib/live_beats_web/endpoint.ex` has `plug Plug.RequestId` and `plug Plug.Telemetry, event_prefix: [:phoenix, :endpoint]`, and `router.ex` mounts `live_dashboard "/dashboard"` with those metrics in dev and test only. `fly.toml` serves internal port 4000 with TCP checks, the Dockerfile builds on Elixir 1.14 with OTP 24, and there are no GitHub workflows.

PromEx 1.12.0 is the exporter, and its requirements decide the first edit: it needs `telemetry_metrics ~> 1.0`, `telemetry_poller ~> 1.1`, `plug_cowboy >= 2.6`, `phoenix >= 1.7.0`, `phoenix_live_view >= 0.20.0` and Elixir 1.14 or later. So the agent adds `{:prom_ex, "~> 1.12"}` and raises `telemetry_metrics` to `~> 1.0`, `telemetry_poller` to `~> 1.1` and `plug_cowboy` to `~> 2.6`, runs `mix deps.get` and confirms `telemetry.ex` still compiles, because the `Telemetry.Metrics` functions it uses are unchanged across that major. Then `mix prom_ex.gen.config --datasource prometheus` writes `lib/live_beats/prom_ex.ex`, which the agent edits to enable `{Plugins.Phoenix, router: LiveBeatsWeb.Router, endpoint: LiveBeatsWeb.Endpoint}`, `Plugins.Ecto` and `Plugins.PhoenixLiveView` beside the default `Plugins.Application` and `Plugins.Beam`, and to list the matching dashboards.

Wiring follows the generator's instructions. `config/config.exs` gets `config :live_beats, LiveBeats.PromEx, disabled: false, manual_metrics_start_delay: :no_delay, drop_metrics_groups: [], grafana: :disabled, metrics_server: :disabled`. `LiveBeats.PromEx` goes first in the `children` list of `application.ex`, ahead of the Repo, so Ecto's init events are not missed; the existing `LiveBeatsWeb.Telemetry` stays where it is because the dashboard still reads from it. For the scrape route there are two options and the agent picks by deployment: `plug PromEx.Plug, prom_ex_module: LiveBeats.PromEx` placed before `Plug.Telemetry` in the endpoint, so scrapes do not log or count as requests, or a standalone `metrics_server` on its own port, which on Fly keeps `/metrics` off the public app and lets `fly.toml` declare a `[metrics]` section with that port and path for the platform's own scraper. The pull request explains the choice and shows the other.

The app's tests need Postgres, and the sandbox has no Docker, so the agent installs PostgreSQL with `apt-get`, creates the role the `config/test.exs` expects, and runs `mix compile --warnings-as-errors` and `mix test`, which the `test` alias precedes with `ecto.create` and `ecto.migrate`. It then starts the server, requests `/metrics`, and pastes the first lines of the exposition (the `prom_ex_*` and `live_beats_prom_ex_phoenix_*` families) into the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add Prometheus metrics to this Phoenix LiveView application with PromEx. Read mix.exs, mix.lock, lib/live_beats/application.ex, lib/live_beats_web/telemetry.ex, lib/live_beats_web/endpoint.ex, lib/live_beats_web/router.ex, config/config.exs, config/runtime.exs, fly.toml and the Dockerfile first. Add prom_ex and raise telemetry_metrics, telemetry_poller and plug_cowboy to the versions it requires, keeping the existing Telemetry module for the LiveDashboard. Generate the PromEx module with mix prom_ex.gen.config, enable the Phoenix, Ecto and PhoenixLiveView plugins and their dashboards, add the config block, start PromEx first in the supervision tree, and expose the metrics either with PromEx.Plug placed before Plug.Telemetry or with a standalone metrics server on its own port, choosing by how the app is deployed and explaining the choice. Install PostgreSQL in the sandbox, run mix compile --warnings-as-errors and mix test, start the server and fetch /metrics, and open a pull request that quotes the first lines of the scrape and the test output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `mix.exs` and `mix.lock` for PromEx and the three raised dependencies, adds `lib/live_beats/prom_ex.ex`, adds a config block in `config/config.exs`, moves one line in `lib/live_beats/application.ex`, and either adds one plug to `lib/live_beats_web/endpoint.ex` or sets `metrics_server` in config with a matching `[metrics]` section in `fly.toml`. Its description quotes the first lines of a `/metrics` response from the sandbox and the end of the `mix test` run.

A reviewer checks that `telemetry.ex` and the LiveDashboard still work after the `telemetry_metrics` major bump, reads the plugin list against what the app really uses (no Oban, no Absinthe), and decides whether `/metrics` may be public: on the endpoint it is, behind a standalone port it is not. The dashboards PromEx ships for Grafana are listed in the module and can be uploaded later by setting the `grafana` config.

From then on every request, LiveView mount and Ecto query is a Prometheus series, and adding an app-specific counter is a `PromEx.Plugin` module beside the generated one.

## Run it on every Phoenix service without metrics

A team with several Phoenix apps sends this body once per repository from its own server, each a `POST /o/{org}/sessions` with that repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started. Each run reads its own `mix.exs`: an app that already has `telemetry_metrics` 1.x skips the bump, an app with Oban gets the Oban plugin enabled, and an app without LiveView leaves that plugin out.

Give the key a `webhook_url` and Rush POSTs `run.completed` to it when each turn ends, signed per Standard Webhooks, with `data.session_url` to read the pull request from. A follow-up on `POST /o/{org}/sessions/{id}/messages`, once the turn has finished, wakes the same sandbox to switch from the endpoint plug to a standalone port or to add a custom plugin.

## Why run it on Rush?

The exporter is a generator away, but the dependency bumps it forces, the order of the supervision tree, the plug position and the Postgres the test suite needs are where a laptop attempt stalls. The agent does all of it in its own sandbox, compiles with warnings as errors, runs the tests and fetches a real scrape, and that output comes back with the pull request.

A team with several Elixir services starts the same request against each repository at once; each run reads its own dependencies and deployment file, so the pull requests differ where the apps differ and are otherwise the same shape, which makes them quick to review together.

## Frequently asked questions

### Why do telemetry_metrics and telemetry_poller change version?

PromEx 1.12 declares `telemetry_metrics ~> 1.0` and `telemetry_poller ~> 1.1`, and this app pins `~> 0.6` and `~> 1.0`, so `mix deps.get` cannot resolve both until the requirements are raised. The metric definitions in `telemetry.ex` use the same `summary/2` functions in both majors, and the agent confirms that by compiling and running the dashboard in dev.

### Should /metrics be on the public endpoint?

Only if the deployment restricts who can reach it. On Fly the standalone `metrics_server` on a separate port, declared under `[metrics]` in `fly.toml`, keeps the route off the public service while the platform scraper reaches it on the private network. Behind a reverse proxy that can block one path, the endpoint plug is simpler. The agent picks from `fly.toml` and says why.

### Does the LiveDashboard keep working?

Yes. The dashboard reads `LiveBeatsWeb.Telemetry`, which stays in the supervision tree and in the router, and PromEx attaches its own handlers to the same telemetry events. The two do not interfere; the pull request shows the dashboard loading in dev after the change.

### What about the Bumblebee and EXLA dependencies?

They are untouched, but they make the first compile in a fresh sandbox slow because EXLA fetches a precompiled XLA archive. The agent compiles once, keeps the sandbox for follow-ups, and does not count that time against the change.

## More Phoenix LiveView jobs

- [Add Live Search to a Phoenix LiveView Page from a Linear Issue](https://getrush.ai/run/add-song-search/phoenix-liveview/)
- [Add Structured Logging to a Phoenix LiveView App](https://getrush.ai/run/add-structured-logging/phoenix-liveview/)
- [Write LiveView Tests for a Phoenix App](https://getrush.ai/run/write-liveview-tests/phoenix-liveview/)

## The same job on other stacks

- [Add Prometheus Metrics to a Quarkus App](https://getrush.ai/run/add-prometheus-metrics/quarkus/)
- [Add Prometheus Metrics to a Rocket App](https://getrush.ai/run/add-prometheus-metrics/rocket/)
- [Add Prometheus Metrics to an Express and Sequelize API](https://getrush.ai/run/add-prometheus-metrics/sequelize/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
