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

Files touched

Review this PR

C++ practice