What the agent does
The agent begins with what the application logs about SQL today, which is decided by one missing field. In a GORM app like realworld-apps/golang-gin-realworld-example-app, Init in common/database.go opens the database with &gorm.Config{TranslateError: true} and sets no Logger. GORM 1.25.12, the version in go.mod, then uses logger.Default: it writes to standard output through the standard library's log package, in colour, at the Warn level, with a slow threshold of 200 milliseconds and IgnoreRecordNotFoundError off. The test database in TestDBInit is different on purpose, logger.Default.LogMode(logger.Info), which prints every statement. So production already reports slow statements, as coloured multi-line text that a log collector cannot parse, at a threshold nobody chose.
Reading the logger's Trace method next to the models shows two problems a threshold does not fix. First, any statement that returns an error is printed at this level, and gorm.ErrRecordNotFound counts. isFavoriteBy in articles/models.go and isFollowing in users/models.go both call First and treat an empty result as the answer no, so viewing an article you have not favorited, or a profile you do not follow, writes an error line. Second, the default logger prints SQL with the values substituted in. A failed or slow INSERT into user_models would carry the email and the bcrypt hash from the password column into the logs. The agent sets IgnoreRecordNotFoundError and ParameterizedQueries to true, which keeps placeholders in the logged SQL.
For the format it writes a small type in common that implements logger.Interface, the five methods LogMode, Info, Warn, Error and Trace, on top of log/slog, which the module's go 1.21 line makes available without a dependency. Trace emits one JSON line per reported statement with the SQL, rows affected, elapsed milliseconds, the calling file and line, and a boolean for slow. The threshold and level come from two new environment variables documented in .env.example beside PORT, GIN_MODE and DB_PATH, with the current 200 milliseconds as the default so behaviour does not shift silently. TestDBInit keeps its Info mode. The agent also reports a limit it does not paper over: the model functions call common.GetDB() without WithContext, so Trace receives a background context and a slow statement cannot be tied to the request that issued it. Threading the request context through is a separate, wider change.
The test has to produce a slow query on demand, and SQLite on a local file is fast. So the new test in common builds the logger over a bytes.Buffer and opens a database twice: once with a threshold of one nanosecond, where every statement qualifies, asserting a JSON line with slow true and no literal values in the SQL; once with a threshold of an hour, asserting silence. A third case runs First on an empty table and asserts nothing is logged. The SQLite driver needs cgo, as the dependency table in readme.md notes, so the agent checks for a C compiler in the sandbox and installs gcc from system packages if it is missing, then runs go test ./..., scripts/gofmt.sh and the linter configured in .golangci.yml.
Start it with one API call
POST https://api.phoenix.so/o/{org}/sessions with this body:
{
"agent": "codex",
"input": "Add slow query logging to this GORM application. Read go.mod, common/database.go, every function that calls the database in articles and users, .env.example, the CI workflow and the lint config first. Read the logger package of the installed GORM version in the module cache and state what the application logs about SQL today: destination, format, level, slow threshold, and whether query values and record-not-found errors appear. Replace the default logger for the main database with one that writes one structured JSON line per reported statement using log/slog, with the SQL, rows affected, elapsed time, calling file and line, and whether it was slow. Read the slow threshold and the level from environment variables, default to today's values, and document them in .env.example and the readme. Query parameter values must not appear in logged SQL, and a record-not-found result must not be logged as an error. Leave the test database's logging as it is. Do not add a logging dependency and do not change any query. Add tests that prove a statement over the threshold is logged as slow, one under it is not, values are absent, and a not-found lookup is silent; make the slow case deterministic by setting the threshold, not by sleeping. If slow statements cannot be tied to the request that issued them with the code as it is, say why and what change would allow it, without making that change. Make sure a C compiler is available for the SQLite driver, run go test ./..., scripts/gofmt.sh and the linter, and open a pull request with sample log lines from before and after.",
"resources": [
{
"url": "https://github.com/your-org/your-app"
}
]
}- 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.
- 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.
- 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
A pull request with a new logger file and its test in common, a few changed lines in common/database.go, and two documented variables in .env.example and readme.md. The description shows the same three situations before and after, as captured from the running server: a slow statement, a failed insert, and a lookup that finds nothing. Before, the first two are coloured text with values inline and the third is an error line; after, the first two are single JSON lines with placeholders and the third is absent.
It states the defaults it kept, 200 milliseconds and the warning level, and why they were kept instead of tuned: the right threshold comes from production latency, which the agent cannot see. A final section describes the missing request context, lists the functions that would need a context parameter, and says that this pull request does not attempt it.
The reviewer checks that the JSON keys match what their log pipeline already indexes, and that nothing in the sample lines contains a value. If the team wants every statement in some environment, the level variable set to info does that without a code change.
Run it on every service that hides its slow queries
This is a job a developer hands off the day a slow endpoint cannot be explained from the logs. They send this body to POST /o/{org}/sessions from a terminal or an editor's MCP client and carry on; the pull request is on the session when the run completes. Sending a request_id, a UUID generated once for the job, means a retried call returns the same run and does not start another.
For a group of Go services, a script sends the same body once per repository, each with its own UUID as request_id. Each run reads its own repository's GORM version and logger, so the pull requests differ where the services do, and they can be merged in any order.
The use case this job follows, with its automation and trigger: Hand off a long task.
Why run it on Rush?
A logging change is judged by its output, and the output only exists when the code runs. In its own sandbox the agent starts the server, issues the requests that produce a slow statement, a failure and an empty lookup, and captures what is printed before and after. Those lines go into the pull request, and the commands that produced them stay in the run's transcript at GET /o/{org}/sessions/{id}/messages.
It is also a small change that tends to sit in a backlog because nothing is broken until an incident needs the data. Handing it off costs one request. What comes back has tests that pin the threshold and the redaction, so a later edit to the logger cannot quietly bring values back into the logs.
Frequently asked questions
Why not just set LogMode to Info in production?
Because that prints every statement, with values, on every request. It multiplies log volume and puts user data in the logs. The slow threshold exists so that only statements worth reading are reported.
Can it log through zap or zerolog instead of slog?
Yes, if the repository already uses one; say so in the prompt. GORM only needs the five methods of its logger interface, so the adapter is the same size for any backend. The agent does not add a logging library to a project that has none.
Does the threshold include time spent waiting for a connection?
The logger measures from the moment GORM starts the operation to the moment it finishes, so time waiting for a pooled connection is inside the number. A statement can therefore be reported as slow when the database was idle and the pool was exhausted. The description says this next to the sample lines.
Will this find the query that is actually slow in production?
It makes that query visible once it runs there. The log line carries the SQL shape and the file and line that issued it, which is enough to open an issue for an index or a rewrite. Choosing the index is a separate job with its own evidence.
More GORM jobs
The same job on other stacks
- Add Slow Query Logging to a Mongoose App (Mongoose)
- Add Slow Query Logging to a Prisma App (Prisma)
- Add Slow Query Logging to a Sequelize App (Sequelize)