# Add Prometheus Metrics to a Fiber App

Yes. An agent adds the Fiber v3 Prometheus middleware from gofiber/contrib, serves /metrics on the same port, writes a test, and opens a PR.

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

## What the agent does

The agent reads how requests flow before adding a collector, because the middleware has to sit in front of every route and must be mounted once. In a service like gofiber/boilerplate, `go.mod` names `github.com/gofiber/fiber/v3` v3.6.1 and `github.com/gofiber/utils/v2` v2.6.3 on a `go 1.26.0` directive. `app.go` parses `-port` (default `:3000`) and `-prod`, calls `database.Connect()`, creates `fiber.New(fiber.Config{})`, mounts `recover.New()` and `logger.New()`, groups `GET` and `POST /users` under `/api/v1`, serves `./static/public` on `/*` with `static.New`, falls through to `handlers.NotFound`, and listens with `fiber.ListenConfig{EnablePrefork: *prod}`. `database/database.go` is an in-memory slice guarded by a mutex, and there are no tests.

The agent picks the maintained middleware for this major version: `github.com/gofiber/contrib/v3/prometheus`, whose own `go.mod` requires `fiber/v3` v3.6.1 and Go 1.26.0, the same as the project, and `prometheus/client_golang` v1.25.0. `go get` adds it. In `app.go` it mounts `fiberprometheus.New(fiberprometheus.Config{ServiceName: "boilerplate"})` with `app.Use` right after `recover.New()` and before `logger.New()` and the routes. The middleware answers `/metrics` itself in the exposition format and never instruments that path, so no route is added; everything else passes through and is counted under the registered route pattern (`/api/v1/users`, `/*`), not the raw request path, which keeps label cardinality bounded even when clients probe random URLs.

Two details come out of the files rather than the middleware's README. First, `-prod` turns on prefork, where Fiber runs one child process per CPU and each has its own private registry: a Prometheus scrape of `:3000` lands on one child and reports that child's counters only. The agent says this in the pull request and in a comment next to the flag, and recommends scraping without prefork or exporting per-child with a `Labels` entry for the PID. Second, the Dockerfile exposes only port 3000, so serving `/metrics` on the app port rather than a second listener needs no change to `Dockerfile`, `Makefile` or the `docker run -p 3000:3000` lines in `README.md`.

`app_test.go` is new, since the repository has none. It builds the app the same way `main` does, sends `GET /api/v1/users` and `POST /api/v1/users` through `app.Test`, then reads `GET /metrics` and asserts that `http_requests_total` and `http_request_duration_seconds` appear with `path="/api/v1/users"` and `service="boilerplate"`, and that `/metrics` itself does not appear as a path. The agent runs `go mod tidy`, `gofmt -l`, `go vet ./...` and `go test ./... -v -race` as `.github/workflows/test.yml` does, and runs `golangci-lint` with the project's `.golangci.yml`, which enables `modernize`, so the new file passes the same lint the `lint.yml` workflow applies. The sandbox fetches the Go toolchain the `go 1.26.0` directive asks for.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add Prometheus metrics to this Fiber v3 application. Read go.mod, app.go, handlers/handlers.go, database/database.go, Dockerfile, Makefile, .golangci.yml, .github/workflows/test.yml and .github/workflows/lint.yml first. Add github.com/gofiber/contrib/v3/prometheus with go get and mount it once in app.go with app.Use, after recover and before the logger and routes, with ServiceName set to the module name; let it serve /metrics on the app's own port. Do not add a second listener. Explain in a code comment and in the pull request what prefork (-prod) means for scraping, since each child has its own registry. Write app_test.go that sends requests to /api/v1/users through app.Test and asserts the request counter and duration histogram appear on /metrics with the route pattern as the path label and that /metrics is not counted. Run go mod tidy, gofmt -l, go vet ./..., go test ./... -v -race and golangci-lint with the repository's config, and open a pull request.",
  "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

What comes back is a pull request that changes `go.mod`, `go.sum` and `app.go`, and adds `app_test.go`. The description lists the metric families the endpoint now serves (`http_requests_total`, `http_requests_status_class_total`, `http_request_duration_seconds`, `http_requests_in_progress`, `http_request_size_bytes`, `http_response_size_bytes`), shows a sample of the exposition output after a few requests, and explains the prefork caveat with the exact flag that triggers it.

The run's record keeps the `go vet`, `golangci-lint` and `go test -race` output, so a reviewer sees the new test pass and the linter stay clean without pulling the branch.

Before merging, a reviewer decides whether `/metrics` should be reachable from the public port at all or sit behind a network rule, whether `SkipStatusCodes` should drop 404s from the catch-all `/*` static route so bot traffic does not inflate the counters, and whether to keep prefork in production now that scrapes are per process.

## Run it on every Go service repository

A platform team that wants the same metrics on every Fiber service starts one run per repository from its own server: a `POST /o/{org}/sessions` each, with the 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 sandbox fetches its own toolchain and opens its own pull request.

Afterwards, an automation on the Project with a `github` trigger on `pull_request.opened` and the prompt "if this pull request adds a route, confirm /metrics still reports it under its route pattern and comment if the middleware was moved below a route" keeps the mount order honest. The trigger adds the pull request after the prompt; only work by an owner, member or collaborator fires it.

## Why run it on Rush?

Mounting a metrics middleware is five lines; knowing what it reports under prefork, whether the catch-all static route floods the labels, and whether the linter the repository already runs accepts the new file are things you learn by running the app and the tools. The agent does that in its own sandbox, with the exact Go version the module asks for, and the test it writes stays behind to catch a future change that mounts the middleware twice or below a route.

The run keeps every command's output, so the pull request can paste the exposition sample and the passing test instead of describing them. Several services can get the same change at once, each on its own machine, each with its own evidence.

## Frequently asked questions

### Why gofiber/contrib and not ansrivas/fiberprometheus?

Because the project is on Fiber v3. The contrib module under `v3/prometheus` is the one built against `fiber/v3` and is based on ansrivas/fiberprometheus; its `go.mod` requires the same v3.6.1 the project already uses, so `go get` adds no second Fiber version.

### Does the middleware count requests to /metrics?

No. Unless a `Next` function says otherwise, the middleware answers `MetricsPath` itself and does not instrument those requests, so scraping does not inflate the counters. The test asserts that no `path="/metrics"` series appears.

### What happens with -prod and prefork?

Each preforked child is its own process with its own registry, and a scrape reaches whichever child accepts the connection. The counters are correct per process but not for the whole service. Scrape each child separately, add a PID label, or run without prefork where you scrape; the pull request lays out the options and changes nothing about the flag.

### Will a 404 to a random path create a new label?

No. Every request to a path with no specific route hits the `/*` static route, so it is recorded under `path="/*"`. A truly unmatched request is only recorded if `TrackUnmatchedRequests` is on, which the agent leaves off.

## More Fiber jobs

- [Add Rate Limiting to a Fiber Login Endpoint](https://getrush.ai/run/add-rate-limiting/fiber/)
- [Add Request ID Logging to a Fiber App](https://getrush.ai/run/add-request-id-logging/fiber/)
- [Add Request Timeouts to a Fiber App](https://getrush.ai/run/add-request-timeouts/fiber/)

## The same job on other stacks

- [Add Prometheus Metrics to a Gin API from a Linear Issue](https://getrush.ai/run/add-prometheus-metrics/gin/)
- [Add Prometheus Metrics to a GORM Service from a Linear Issue](https://getrush.ai/run/add-prometheus-metrics/gorm/)
- [Add Prometheus Metrics to a Koa API](https://getrush.ai/run/add-prometheus-metrics/koa/)

## 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/)
