# Add Request ID Logging to a Fiber App

Yes. An agent adds Fiber's requestid middleware to your app, puts the id in every access log line and response header, writes a test, and opens a PR.

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

## What the agent does

The agent starts from the middleware chain, because in Fiber the order it is built in decides what the logger can print. In a project like gofiber/boilerplate, `app.go` parses a `-port` flag and a `-prod` flag that turns on prefork, calls `database.Connect()`, creates the app with `fiber.New(fiber.Config{})`, adds `recover.New()` and `logger.New()` with the default format, mounts `GET` and `POST /api/v1/users` on `handlers.UserList` and `handlers.UserCreate`, serves `./static/public` and ends with `handlers.NotFound`. `go.mod` is on `gofiber/fiber/v3` v3.6.1 and Go 1.26. The `database` package is an in-memory slice behind a mutex, `models.User` has one `Name` field, and there is no test file in the module.

Fiber v3 ships `middleware/requestid`, which reads an incoming `X-Request-ID`, keeps it when it is printable ASCII, otherwise generates one with `utils.SecureToken`, stores it on the context and sets it on the response. Its `New()` also registers the `${requestid}` tag for the logger, but only loggers built afterwards see it, so the agent inserts `app.Use(requestid.New())` between `recover.New()` and `logger.New()` and gives the logger a format of `${time} ${pid} ${requestid} ${status} ${latency} ${method} ${path} ${error}\n`. `${pid}` matters here because `-prod` enables prefork and each child process writes its own lines; `${error}` prints the error a handler returned rather than `-` once one does.

Handlers that want the id use `requestid.FromContext(c)`. `handlers.NotFound` serves a static `404.html` and is left alone; instead the agent adds a `request_id` field to the JSON that `UserCreate` and `UserList` return, so a client that reports a problem can quote the same value the log holds. Everything that reads the id copies it with `strings.Clone` before keeping it past the handler, as the middleware's documentation says to do when `Immutable` is off, which it is in this `fiber.Config{}`.

To test any of this the chain has to be reachable from a test, and today it is built inside `main()`. The agent lifts the construction into `func newApp(stream io.Writer) *fiber.App` so `main()` calls it with `os.Stdout` and a test calls it with a buffer. `app_test.go` then uses `app.Test` to send a request with no header and assert the response carries an `X-Request-ID`, to send one with `X-Request-ID: abc123` and assert it comes back unchanged, and to check that the buffer's one log line contains that same id. It runs `go test ./... -v -race` as `.github/workflows/test.yml` does, `golangci-lint run --tests=false` with the v2 config in `.golangci.yml`, and `go run app.go` with two `curl -i` calls before opening the pull request.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Add request id logging to this Fiber v3 app. Read go.mod, app.go, handlers/handlers.go, database/database.go, models/user.go, Makefile, Dockerfile, .golangci.yml and .github/workflows/test.yml first. Add the requestid middleware from github.com/gofiber/fiber/v3/middleware/requestid before the logger middleware, since the logger only sees the ${requestid} tag if requestid.New() ran first, and set a logger format that prints the time, pid, request id, status, latency, method, path and error. Return the id as request_id in the JSON of the users handlers using requestid.FromContext, cloning the string before keeping it. Move the app construction out of main into a function that takes the log writer, so a test can build the app with a buffer. Write app_test.go using app.Test: a request without X-Request-ID gets one back, a request with X-Request-ID keeps it, and the log line contains it. Run go test ./... -v -race and golangci-lint with the repo config, start the app and show two curl -i calls, then 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 `app.go` to add the middleware, the logger format and the `newApp` function, changes `handlers/handlers.go` to include `request_id` in the two JSON responses, and adds `app_test.go`. The description shows two `curl -i` exchanges, one with a generated id and one with a supplied id echoed back, and the matching log lines with the same values, so a reviewer sees the header and the log agree without running anything.

The run's record holds the `go test -race` output, the `golangci-lint` run against the `modernize` linter the repository enables, and the server session, so every claim in the description points at a command that ran.

Before merging, a reviewer decides whether to trust incoming `X-Request-ID` values from the internet or only from a proxy, which is a `requestid.Config` with a `Generator` and a `Next` function away, and whether the `404.html` page should carry the id in a comment. The `Dockerfile` and the `-prod` prefork path are unchanged.

## Run it on every Go service that logs without ids

A team with several Fiber services starts one run per repository from its own server: one `POST /o/{org}/sessions` each, 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 instead of a second pull request. Each run reads that service's own middleware order and puts `requestid.New()` where its logger will see it.

After that, an automation on the Project with a `github` trigger on `pull_request.opened` and `pull_request.synchronize` and the prompt "if this pull request adds a middleware before the logger or changes the logger format, check that ${requestid} still prints and comment with a sample log line" guards the order on every change. The trigger puts the pull request after the prompt; only work by an owner, member or collaborator fires it, never a fork's.

## Why run it on Rush?

The failure mode here is silent: put `requestid.New()` after `logger.New()` and the app runs, the header is set, and every log line prints an empty id. The agent builds the chain, sends real requests and reads the log it produced, so the pull request includes a line with the id in it rather than a middleware list that looks right.

The sandbox keeps the test output, the lint run and the curl exchanges with the run, and the pull request cites them. A team can start the same job on every Go service it owns in one afternoon, each on its own machine with its own record, and review ten pull requests that each show their own log line.

## Frequently asked questions

### Why does the order of requestid and logger matter?

A logger instance resolves its format tags when `logger.New()` builds it. `requestid.New()` is what registers the `${requestid}` tag, so a logger created before it renders the tag as empty text for the life of the process. The middleware docs for Fiber v3.6.1 say this in so many words; the agent follows them and the test would catch a reorder.

### Why clone the id before keeping it?

With `Immutable` off, which is the default in `fiber.Config{}`, a request id taken from the incoming header points into the request's buffer, and that buffer is reused once the handler returns. `strings.Clone` copies the bytes so a goroutine or a stored value does not read a later request's data. For a generated id the copy is harmless.

### Why change main at all to add a test?

Because `app.go` builds the whole app inside `main()`, which a test cannot call. A `newApp` function that takes the log writer is the smallest change that lets `app.Test` drive the real middleware chain and lets the test read the log line from a buffer instead of stdout. `main()` still does the same thing with the same flags.

### Does prefork change anything?

Each forked child has its own logger and its own generator, so ids are still unique and the `${pid}` tag in the new format shows which child served a request. The test runs without prefork; the agent starts the server once with `-prod` as well to show the format holds across children.

## More Fiber jobs

- [Add Request Timeouts to a Fiber App](https://getrush.ai/run/add-request-timeouts/fiber/)
- [Add Response Caching to a Fiber App](https://getrush.ai/run/add-response-caching/fiber/)
- [Add Security Headers to a Fiber App](https://getrush.ai/run/add-security-headers/fiber/)

## The same job on other stacks

- [Add Request IDs and Structured Logging to a Gin API from a Linear Issue](https://getrush.ai/run/add-request-id-logging/gin/)
- [Add Request ID Logging to a Hono Worker](https://getrush.ai/run/add-request-id-logging/hono/)
- [Add Request ID Logging to a Koa App](https://getrush.ai/run/add-request-id-logging/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/)
