All jobs

Add Slow Query Logging to a Sequelize App

Yes. An agent turns Sequelize's benchmark timing into a slow-query log with a threshold from the environment, redacts bound values, tests it and opens a PR.

Run this on RushOpens the console with this prompt ready to send, in a Project that has your repository.

What the agent does

The agent reads how the connection is built before it touches logging. In an app laid out like sequelize/express-example, express-main-example/sequelize/index.js constructs new Sequelize({ dialect: 'sqlite', storage: 'sqlite-example-database/example-db.sqlite', logQueryParameters: true, benchmark: true }), defines the user, instrument and orchestra models from models/*.model.js, and applies orchestra.hasMany(instrument) in extra-setup.js. package.json depends on sequelize ^6.3.3, express ^4.17.1, body-parser ^1.19.0 and sqlite3 ^5.0.0, declares node >=10, has only start and setup-example-db scripts, and .npmrc sets package-lock=false, so there is no lockfile and no test runner. express/app.js mounts one REST route set per model, and routes/orchestras.js runs findAll({ include: models.instrument }) when includeInstruments is in the query, which is the one join in the app.

Two options already do most of the work and the agent explains them in the pull request. Sequelize's constructor documents benchmark as passing the query's execution time in milliseconds as the second argument to the logging function, and logging defaults to console.log, which prints only its first argument. So the app measures every query today and throws the number away. The agent adds sequelize/logging.js exporting a function (sql, timingMs) that reads a threshold from SLOW_QUERY_MS and writes one JSON line to console.warn with level, msg, ms and sql when the timing is at or above it, and nothing otherwise. LOG_ALL_QUERIES=1 keeps the old print-everything behaviour for local debugging. index.js passes the function as logging.

The third option is the one that matters for privacy. logQueryParameters: true puts bound values into the SQL string that reaches the logging function, so a slow INSERT on users would log the username it inserted. The agent keeps the option on outside production, where the values are the point, and turns it off when NODE_ENV is production, documenting the trade in a README section: a production slow-query line shows the statement shape with ? placeholders, which is enough to find the missing index without copying rows into logs. The threshold, the flag and the environment switch all live in sequelize/index.js next to the options they govern.

With no test framework in the repository the agent writes test/logging.test.js against Node's built-in assert and adds a test script that runs it with node, so nothing new is installed. The test calls the logging function with a timing under the threshold and one over it while capturing console.warn, checks that exactly one line came out and that it parses as JSON with the expected fields, and checks that LOG_ALL_QUERIES logs both. For the real path it runs npm run setup-example-db in the sandbox with SLOW_QUERY_MS=0, which makes every statement of sqlite-example-database/setup.js a slow one, and pastes a few of the resulting lines into the pull request so a reviewer sees the exact format.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add slow query logging to this Sequelize and Express app. Read package.json, .npmrc, index.js, sequelize/index.js, sequelize/extra-setup.js, sequelize/models/*.js, express/app.js, express/routes/*.js, sqlite-example-database/setup.js and the readme first. The Sequelize constructor already sets benchmark: true, which passes the elapsed milliseconds as the second argument of the logging function. Add sequelize/logging.js exporting a logging function that reads a threshold from SLOW_QUERY_MS, writes one JSON line to console.warn with level, msg, ms and sql when a query is at or above it, stays silent otherwise, and logs everything when LOG_ALL_QUERIES=1. Pass it as the logging option. Keep logQueryParameters on outside production and off when NODE_ENV is production, and explain why in a README section with the two variables. Add test/logging.test.js using Node's assert and a test script that runs it with node. Run the test and run setup-example-db with SLOW_QUERY_MS=0 to show the format, then open a pull request that quotes both outputs.",
  "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 adds sequelize/logging.js and test/logging.test.js, and changes sequelize/index.js (the logging option and the environment-dependent logQueryParameters), package.json (the test script) and readme.md. Its description quotes the test output and a handful of JSON lines from the seeded setup run, each with its ms and the statement that produced it.

A reviewer checks the threshold default against what their database actually does (SQLite on a local file answers in well under a millisecond, so the useful value for the real deployment is higher than anything the sandbox shows), and whether console.warn is the right sink or whether the app's process manager expects a logger. The function takes the writer as an optional argument so swapping in a logger is a one-line change.

No query changes. The routes, the models and the seed script run exactly as before; the only difference is that a slow statement now leaves a line behind.

Run it on every Grafana alert on p95 latency

Grafana contact points can send a custom Authorization header, so an alert rule on the app's p95 latency can call a Rush webhook trigger directly with Authorization: Bearer <its secret>. The trigger adds the alert body after the automation's prompt and fills in no placeholder, and an automation's runs are read-only, so that run reads the recent slow-query lines the alert points at and comments with the statement that dominates them and the index it would add.

A handler on your own server receives the same Grafana webhook and sends this body to POST /o/{org}/sessions with the alert's route name appended to the prompt and a request_id that is a UUID v5 of the alert's fingerprint, so a repeated notification returns the run it already started instead of opening a second pull request. That run can push, and the key that started it gets run.completed at its webhook_url with the pull request's URL.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Logging is the kind of change that looks finished when the code compiles and turns out wrong at the first slow night: the line is missing a field, the threshold is in seconds instead of milliseconds, or the parameters went to a log aggregator nobody meant them to reach. The agent runs the real seed script against the real options in its sandbox and keeps the lines it produced in the run's record, so a reviewer reads actual output rather than a description of it.

Most teams have the same new Sequelize(...) call in several services with benchmark either off or on with its timing discarded. One run per repository applies the same threshold variable and the same JSON shape to each, so the lines from every service parse the same way in whatever reads them.

Frequently asked questions

Does this slow the queries down?

No more than benchmark: true already does, and that option is already on in this app. The logging function returns at once when the timing is under the threshold; the JSON line is built only for the slow ones.

Why JSON on console.warn instead of a logging library?

The repository has four dependencies and no logger, and adding one for a single line type is more change than the job needs. One JSON line per slow query is what log collectors parse, and the writer is a parameter so a pino or winston instance can replace console.warn later without touching the function.

Why turn logQueryParameters off in production?

With it on, Sequelize puts the bound values into the SQL string the logging function receives, so a slow write would log the row it wrote. Off, the line shows the statement with placeholders, which is what you need to find a missing index, and nothing personal reaches the log.

Is the timing the whole request?

No. It is the time Sequelize measured around the database call. Building model instances from the rows, and anything Express does before or after, is not included, so a route can be slow while every query under it is fast.

Does it work with Postgres or MySQL instead of SQLite?

Yes. The options are on the Sequelize constructor and the logging function sees the same two arguments for every dialect. Only the threshold value should differ, since a networked database adds a round trip the local file does not.

More Sequelize jobs

The same job on other stacks

Keep reading