Per-route request counts for the ops dashboard
C++ · C++ · intermediate · modification
The ops dashboard needs to see which endpoints get traffic, so this PR counts requests per route in process. `src/metrics/hit_counter.hpp` and `src/metrics/hit_counter.cpp` add `metrics::HitCounter`: a map from a route key to an atomic count, with `record(route)` and a `snapshot()` that returns the (route, count) pairs sorted by route. `src/http/router.hpp` and `src/http/router.cpp` build each route's key (`GET /api/orders/{id}`) once in `add`, and, when a counter is attached with `set_hit_counter`, record the matched route's key, or `(unmatched)`, before calling the handler. `src/app/main.cpp` creates the counter, attaches it to the router before the server starts, and registers `GET /internal/hits`, which prints the snapshot. `tests/hit_counter_test.cpp` adds unit tests for the counter and for the router's counting.
Order-tracking API in C++20, standard library only, built with CMake (unchanged: it builds every `src/**/*.cpp` into the server and every `tests/*_test.cpp` together with `tests/check_main.cpp` and the `src/` sources other than `src/app/main.cpp` into the test runner) by Apple clang 17 locally and GCC 13 in CI, with `-Wall -Wextra -Wconversion -Werror`. Threading: `http::Server` (`src/http/server.hpp`, existing, unchanged) accepts connections on `ServerOptions::listen` and runs `ServerOptions::workers` worker threads (8 in production), each parsing a request and calling `Router::dispatch` on the one shared `const Router`, so up to 8 dispatches run at the same time; `Server::run` returns only at shutdown. `main` registers every route (`app::register_routes`, existing, unchanged, about 30 routes) and attaches the counter before `Server` is constructed, and nothing changes the router afterwards. `http::path_matches` (`src/http/path_match.hpp`, existing, unchanged) matches a pattern segment by segment, a `{name}` segment matching any non-empty segment. `Request` and `Response` (`src/http/message.hpp`, unchanged) are plain structs; a default `Response` is 200 `text/plain` with an empty body. No route registered by `app::register_routes` matches `GET /internal/hits` (there is no catch-all route), so the new route is the one that answers it. The load balancer does not forward `/internal/*` paths from the internet; only the ops network reaches them. The process is restarted on every deploy, under live traffic. Tests use the repo's existing helper `tests/check.hpp` (unchanged): `TEST(name)` registers a test, `CHECK(expr)` and `CHECK_EQ(a, b)` record a failure without stopping, and `tests/check_main.cpp` runs every registered test and exits non-zero on any failure.
Requirements
- `GET /internal/hits` (new) answers 200 `text/plain` with one line per route, `<METHOD> <pattern> <count>` (for example `GET /api/orders/{id} 17`), sorted by the `<METHOD> <pattern>` key, where the count is the number of requests the router has dispatched to that route since the process started. Requests that match no route are counted on one `(unmatched) <count>` line. A route is listed once it has received its first request.
- A request is counted when the router picks its route, before the handler runs, so a request whose handler fails still counts, and `/internal/hits` counts its own requests like any other route.
- Counts are exact under the server's concurrency: every dispatched request is counted exactly once, whichever worker thread handles it and whatever else runs at the same time, including requests that arrive in the first moments after a restart. `/internal/hits` can be called while other requests are in flight; each count it prints is the route's count at some moment during that call.
- Unchanged: which route a request matches (first registered route whose method and pattern match), the handlers, and the 404 `not found` response for a request that matches nothing.
Files touched
- src/metrics/hit_counter.hpp
- src/metrics/hit_counter.cpp
- src/http/router.hpp
- src/http/router.cpp
- src/app/main.cpp
- tests/hit_counter_test.cpp