Files
rose-ash/lib/host/router.sx
giles c2def0ea16 otel P3: auto-instrument handlers at the make-app seam
otel/instrument-routes wraps each flattened Dream route's handler in a timed
span named METHOD /route with {:http.method :http.route :http.status} attrs;
host/make-app applies it so every matched request becomes a trace. Refactored
with-span onto a shared otel/-timed core that takes a finalize fn for
result-derived attrs (the http.status only known post-handler).
2026-07-01 18:20:46 +00:00

31 lines
1.7 KiB
Plaintext

;; lib/host/router.sx — Host application assembly. A host app is a single Dream
;; router built from per-domain route groups, with a built-in health endpoint and
;; a JSON 404 fallback so the native OCaml HTTP server has one entry point:
;; request -> response. Each subsystem contributes a list of Dream routes (see
;; lib/host/feed.sx); host/make-app concatenates them under one router.
;; dr/flatten-routes (Dream) flattens the nested groups, so a group is just a list
;; of routes. Depends on lib/dream/router.sx + lib/host/handler.sx + the host
;; session middleware (lib/host/session.sx) and login routes (lib/host/auth.sx).
;; Liveness probe — GET /health -> 200 {"ok":true,"data":"healthy"}.
(define host/health-route
(dream-get "/health" (fn (req) (host/ok "healthy"))))
;; Build the host app from a list of route groups (each a list of Dream routes).
;; The health route + login routes are always mounted; Dream's router returns a
;; JSON 404 for unmatched paths, which host endpoints override per-domain as
;; needed. The WHOLE app is wrapped in the signed-session middleware so every
;; request carries a session and any handler can log a principal in/out — this is
;; the front door, so sessions are not optional.
;; Every route's handler is wrapped by otel/instrument-routes (lib/host/otel.sx)
;; so each matched request records a "METHOD /route" trace span — observability is
;; on by default at the front door. (Resolved at call time; otel loads before any
;; request is served.)
(define host/make-app
(fn (groups)
(let ((router (dream-router
(otel/instrument-routes
(cons host/health-route
(cons host/auth-routes groups))))))
((host/sessions) router))))