Parcourir la source

ops: prod nginx logs request timing and the served hostname

A consumer's latency question was unanswerable retrospectively: the built-in
`combined` format records no time at all, so "was it slow yesterday" had no
evidence in either direction. Now `combined` plus three APPENDED fields -
host=$host, rt=$request_time, urt=$upstream_response_time - so anything
parsing the combined prefix keeps working byte-for-byte.

host= matters because prod serves two hostnames with different surfaces and
a line otherwise cannot be attributed to either. The gap between rt and urt
is usually the answer, since it is the part a consumer cannot see from their
side: a slow client or a large body rather than a slow backend.

Applied via /etc/nginx/conf.d/ and the two API server blocks, never
nginx.conf, which is an nginx-common conffile. A second access_log at the
http level would have double-logged rather than replaced the default, which
is why it is per server block. The copy committed here is the file installed
on prod.

The qdrant site file has three identical server_name lines, so the edit was
placed by line number and then asserted by reading back the surrounding
lines rather than trusting the match.

Verified by effect: nginx -t, reload, 200 on /healthz on both hostnames, and
the new fields present in the log. Worth knowing for next time - a reload
does not re-format existing keepalive connections, so an external client with
persistent connections kept being logged in the OLD format for about five
seconds while fresh requests carried the new fields. That reads exactly like
a config that did not take; the cut-over was visible at a single second.

Pre-approved by the dependent consumer for whenever a reload was in scope.
Site files backed up to /var/backups/nginx-20261016-timing first.
fszontagh il y a 1 mois
Parent
commit
ecc2f3ebb0
2 fichiers modifiés avec 29 ajouts et 0 suppressions
  1. 6 0
      CLAUDE.md
  2. 23 0
      packaging/nginx-log-format-timing.conf

+ 6 - 0
CLAUDE.md

@@ -246,6 +246,12 @@ docker run -d --name svapi-dev -p 8080:8080 -e SMARTBOTIC_VECTORAPI_KEY=dev svap
   **MOVING A CONSUMER TO THE NEW NAME: THE BASE IS THE ROOT, AND THE OLD VALUE INVITES THE ONE WRONG GUESS.** `https://global-api-01.smartbotics.ai` with NO `/rag` and no trailing slash; `https://global-api-01.smartbotics.ai/rag/...` is a 404, measured. Everything else carries over and was verified rather than asserted when a consumer asked before their go-live: both names proxy to the same `127.0.0.1:8080`, and `GET /api/v1/projects/toloajto/collections` returns 200 on both with **byte-for-byte identical bodies**. CORS preflight is identical (204, same `Access-Control-Allow-*`). `proxy_read_timeout 180s` and `client_max_body_size 10m` match, the new host taking them from the shared snippet so the two cannot drift.
   **Keys and origin allow-lists need NO re-provisioning, and the reason is the part worth stating:** a key and its allow-list live in the DATABASE, not in the hostname, and the allow-list is checked against the **Origin header the BROWSER sends**, which is the consumer's PAGE origin and does not change when they point at a different API host. A consumer will reasonably assume otherwise and ask whether their keys must be re-issued.
   **The one way this switch bites: a wrong base returns HTML, not JSON.** Outside `/api/` the new host answers nginx's own branded 404 page, so a base string that still contains `/rag` gives the caller `text/html` and a JSON parse failure rather than our `{"error":{...}}` envelope. On the old host everything under `/rag/` reaches vectorapi, so a bad path there still returns JSON. The failure therefore does not name its own cause, and it lands at CONFIG time - which is why the advice to a consumer is to test one real write against the new base before flipping prod, and never to flip it the day before a go-live.
+  **THE ACCESS LOG NOW CARRIES TIMING, AND IT IS `combined` PLUS THREE APPENDED FIELDS RATHER THAN A NEW SHAPE.** Done 2026-10-16 (task #84), after a consumer's latency question turned out to be unanswerable retrospectively: the built-in `combined` format records no time at all, so "was it slow yesterday" had no evidence in either direction. `/etc/nginx/conf.d/log-format-timing.conf` defines `log_format timing` as combined plus `host=$host rt=$request_time urt=$upstream_response_time`, and the two API server blocks set `access_log ... timing`. Appended, never reshaped, so anything parsing the combined prefix keeps working - the same additive discipline as `details.unsupported`.
+  **`host=` is the field that is easy to leave out and then need**: prod serves two hostnames with different surfaces, so without it a line cannot be attributed to either. And **the GAP between `rt` and `urt` is usually the answer**, because it is the part a consumer cannot see from their own side: a slow client or a large body rather than a slow backend. `urt` is `-` when nothing was proxied, which is correct rather than zero.
+  **It went in `conf.d` and in the SITE files, never in `nginx.conf`, which is an nginx-common CONFFILE** - `dpkg -S` first, per the trap above. `conf.d` is included at line 60 and `sites-enabled` at 61, so a format defined there is visible to the site files. A second `access_log` at the http level would have DOUBLE-logged every request rather than replacing the default, which is why it is set per server block.
+  **The qdrant site file has three identical `server_name` lines**, so the edit was placed by LINE NUMBER and then asserted by printing the surrounding lines - the scripted-edit-landed-in-the-wrong-handler trap, in config form.
+  **And a reload does NOT re-format existing keepalive connections, which looks exactly like a config that did not take.** Measured: immediately after the reload, requests from an external `node` client with persistent connections were still logged in the OLD format while fresh requests carried the new fields, because old workers keep serving established connections. The cut-over was visible at a single second and resolved in about five. So verify a log-format change on a NEW connection, and do not read a few old-format lines after a reload as failure.
+
   **Two traps met while doing it.** The half-written config was a BYTE COPY of the qdrant one with a single `server_name` changed, so it still carried full duplicate `qdrant-1` blocks on 443 and 80 and pointed at qdrant's port 6333 and qdrant's certificate - enabling that would have put conflicting server blocks on the live site and served a cert that covers neither name. And **the first request after `systemctl reload nginx` hit the old worker set and answered 404**, which read exactly like a broken location block; every path passed on a retry seconds later. Do not diagnose an nginx location from a request issued in the same breath as the reload.
 - **Prod's database binds `localhost`, not `0.0.0.0`** (`storage.bind_address`), which is why it is only reachable from vectorapi on the same box - correct, and worth knowing before wondering why a port-mapped copy refuses connections.
 - **`policy-rc.d` DOES NOT HOLD THE DATABASE PACKAGE. It blocks `invoke-rc.d`, and that postinst calls `systemctl` DIRECTLY.** Measured the hard way on 2026-10-12: a `policy-rc.d` returning 101 was in place, the pinned install ran at 18:21:12, and the service restarted anyway - the 2.17.2 first-boot migration ran to completion before the install command had even returned. **A hold point three consumers had been promised simply did not exist**, and that cost TWO separate things, not one. One consumer lost the chance to say "wait, a call is live" that they had explicitly been offered. **And because the install itself triggered the migration, the outage happened at 18:21 rather than in the 18:30 slot all three had approved** - it ran NINE MINUTES EARLY, outside the approved window, which is a governance failure distinct from the missing check. A consumer put that second point in the record; my own account had blurred it into the first, which understated it.

+ 23 - 0
packaging/nginx-log-format-timing.conf

@@ -0,0 +1,23 @@
+# Access-log format carrying request timing and the served hostname.
+#
+# Added 2026-10-16 because prod's logs could not answer a consumer's latency
+# question retrospectively: the built-in `combined` format records no time at
+# all, so "was it slow yesterday" had no evidence either way and the question
+# was unanswerable rather than merely unanswered.
+#
+# It is `combined` plus three fields APPENDED, never reshaped, so anything
+# already parsing the combined prefix keeps working byte-for-byte:
+#   host=  which hostname served it - prod has two serving different surfaces
+#          (the customer-facing name at the root, and the original /rag mount),
+#          and without this a line cannot be attributed to either
+#   rt=    $request_time, the whole request as nginx saw it, including the time
+#          the client spent sending and receiving
+#   urt=   $upstream_response_time, what vectorapi took. The GAP between rt and
+#          urt is the part a consumer cannot see from their side and is usually
+#          the answer: a slow client or a large body, not a slow backend.
+#
+# urt is "-" when nothing was proxied (a redirect, a branded error page), which
+# is correct rather than zero - no upstream was asked.
+log_format timing '$remote_addr - $remote_user [$time_local] "$request" '
+                  '$status $body_bytes_sent "$http_referer" "$http_user_agent" '
+                  'host=$host rt=$request_time urt=$upstream_response_time';