# Add Request ID Logging to an Echo App

Yes. An agent adds Echo's RequestID middleware so every response carries X-Request-ID and every access log line has it, tests it, and opens a PR.

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

## What the agent does

The agent reads the router before adding anything, because in Echo the request id and the access log are two middlewares that have to agree. In an app like xesina/golang-echo-realworld-example-app, `go.mod` requires `github.com/labstack/echo/v4` v4.1.16 on Go 1.18 with GORM 1.9.8 on SQLite, and `router/router.go` builds the instance: `echo.New()`, the logger level set to debug, `middleware.RemoveTrailingSlash()` as a pre-middleware, `middleware.Logger()` with its default format, a wide-open CORS config, and a validator. `main.go` mounts Swagger on `/swagger/*`, registers the handlers on the `/api` group through `handler.Register`, and starts on `127.0.0.1:8585`. Nothing in the repository logs from a handler; the only log output is the access line, and `utils/errors.go` shapes every error as `{"errors": {"body": ...}}` as the RealWorld spec requires.

The default log format matters here. Echo's `DefaultLoggerConfig.Format` at v4.1.16 already writes a JSON object per request with a `time`, an `id`, `remote_ip`, `method`, `uri`, `status`, `error`, `latency` and byte counts, and the `id` tag is filled from the request's `X-Request-ID` header, or from the response header when the request had none. Today the field is always empty, because nothing sets either header. The agent adds `e.Use(middleware.RequestID())` in `router.go` before the logger. That middleware keeps an incoming `X-Request-ID` and otherwise generates one with `random.String(32)` from gommon, and sets it on the response, so the access line gains an id without any change to the format string and the client gets the same id back to quote in a bug report.

The error path is left as the spec defines it. `utils.NewError` and friends keep their shape, so no client parsing `errors.body` breaks; the id travels in the response header, which is where a support engineer looks it up. For handlers that want it, the agent adds a small `utils.RequestID(c echo.Context) string` that reads the response header, and adds a `c.Logger().Errorf` line carrying it where `handler/article.go` returns a `500`, so those lines and the access line share the id. A `Generator` that produces UUIDs instead of the 32-character random string is possible through `middleware.RequestIDWithConfig`, but `go.mod` has no UUID module and the agent does not add one for this; the pull request notes the option.

Tests follow the handler package's own layout. `handler/handler_test.go` builds the instance with `router.New()` in `setup()` and the existing tests call handlers straight through `e.NewContext`, which skips middleware, so the agent adds `router/router_test.go` that registers a trivial route on `router.New()` and serves requests through `e.ServeHTTP`: one without a header, asserting a 32-character `X-Request-ID` on the response, and one with `X-Request-ID: abc-123`, asserting the same value comes back. A third case points `middleware.LoggerWithConfig` at a buffer with the default format and asserts the written line contains `"id":"abc-123"`. In the sandbox the agent runs `go build ./...` and `go test -v -race ./...`, as the `Makefile` and `.github/workflows/go.yml` do, starts `go run main.go`, shows `curl -i` on `/api/tags` with the header and the matching log line, and opens 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 Echo app. Read go.mod, main.go, router/router.go, handler/routes.go, handler/handler_test.go, handler/article.go, utils/errors.go, Makefile and .github/workflows/go.yml first. Add e.Use(middleware.RequestID()) in router/router.go before the Logger middleware and explain in the pull request why the default Logger format's id field is then filled, quoting the case for id in Echo's logger middleware. Do not change the error payload shape in utils/errors.go. Add utils.RequestID(c echo.Context) string that reads the X-Request-ID response header and log it with c.Logger().Errorf where handler/article.go returns a 500 so those lines carry the id. Add router/router_test.go that serves requests through e.ServeHTTP and asserts a 32-character X-Request-ID on a response when none was sent, the same value back when X-Request-ID abc-123 is sent, and that a Logger pointed at a buffer with the default format writes a line containing \"id\":\"abc-123\". Run go build ./... and go test -v -race ./..., start the app with go run main.go and curl -i /api/tags, then open a pull request that pastes the test output, the curl headers and the matching log line.",
  "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 `router/router.go`, `utils/utils.go` and `handler/article.go` and adds `router/router_test.go`. The description pastes `curl -i` for `/api/tags` with the `X-Request-ID` response header, the access log line whose `id` field holds the same value, and the quoted lines from Echo's logger middleware that explain where the field comes from.

The run's record holds `go build`, the race-enabled test run with the three new cases beside the existing handler tests, and the server start. The Swagger route, the JWT middleware, the CORS config and the error payloads are untouched; the only wiring change is one line in `router.go`.

Before merging, a reviewer decides whether to trust an incoming `X-Request-ID` from any client or only from the proxy in front of the app, since the middleware keeps whatever it is given, and whether the Dockerfile, which still builds on `golang:1.12.1` while `go.mod` says 1.18, should be brought up to date in the same change or a separate one.

## Run it on every new Go service

A team with several Echo 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 rather than a second pull request. Each run reads that service's own `router.go`, so a service with a custom logger format gets the `${id}` tag added to it and a service already behind a proxy that sets the header gets only the passthrough.

Afterwards, 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 RequestID in router.go, or a log call in a handler that does not include utils.RequestID, comment with the line" keeps later changes from logging lines nobody can correlate. The trigger adds 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 job is small and the mistakes are specific: the logger's `id` tag reads the request header first, so a middleware order that sets the id after logging leaves the field empty on the first line, and a test that calls handlers through `e.NewContext`, as every test in this repository does, never exercises middleware at all. The agent reads Echo's middleware source at the pinned version, places the line where the log line sees it, and writes the one kind of test the repository lacks.

In its own sandbox the agent runs the race-enabled test suite and a real server with a real request, and the run keeps the headers and the log line for the pull request. For a team with many Go services, each gets its own run and its own pull request with the id wired into that service's logger.

## Frequently asked questions

### Will the error responses change?

No. `utils/errors.go` keeps producing `errors.body` as the RealWorld spec defines, because clients parse it. The id lives in the `X-Request-ID` response header on every response, including errors, and the access log line carries the same value.

### Why a 32-character random string and not a UUID?

That is what Echo's `RequestID` middleware generates at v4.1.16, through gommon's `random.String(32)`, and it needs no new module. A `Generator` function in `middleware.RequestIDWithConfig` can return a UUID if the team already depends on a UUID package; the pull request names the option without adding the dependency.

### Should the app trust an X-Request-ID a client sends?

The middleware keeps an incoming header as it is, which is right behind a proxy that sets one and risky on an app exposed directly, since a client can send any string into the logs. The pull request raises it; a `Skipper` or a `Generator` that ignores the incoming value for untrusted sources is a small follow-up.

### Does the existing handler test suite still pass?

Yes. Those tests build the instance with `router.New()` and call handlers through `e.NewContext`, which runs no middleware, so adding `RequestID` changes nothing they observe. The new tests in `router/router_test.go` go through `e.ServeHTTP` so the middleware chain is what is under test.

## More Echo jobs

- [Add Request Timeouts to an Echo App](https://getrush.ai/run/add-request-timeouts/echo/)
- [Add Security Headers to an Echo API](https://getrush.ai/run/add-security-headers/echo/)
- [Add Structured Logging to an Echo API](https://getrush.ai/run/add-structured-logging/echo/)

## The same job on other stacks

- [Add Request ID Logging to a FastAPI App](https://getrush.ai/run/add-request-id-logging/fastapi/)
- [Add Request IDs and Structured Logging to a Fastify App](https://getrush.ai/run/add-request-id-logging/fastify/)
- [Add Request ID Logging to a Fiber App](https://getrush.ai/run/add-request-id-logging/fiber/)

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