feat(observability): add product metrics for API, outbox and delivery

This commit is contained in:
shira committed 2026-10-06 19:48:15 +03:00
1 parent 560724e2aa
commit 94488e2b57
51 files changed
+3655 -68

No files matched your search

@@ -51,10 +51,12 @@ func NewRouter(
router := chi.NewRouter()
// Request ID, request logging, and metrics wrap every route, so both
// recovery middlewares below run inside them: a recovered panic carries the
// request ID and its 500 is logged and counted.
// request ID and its 500 is logged and counted. The request log names the
// same route template as the metrics, never the raw path or query.
resolveRoute := operationRoute(apiServer)
router.Use(kitmiddleware.RequestID())
router.Use(kitmiddleware.Logger(logger, nil, kitmiddleware.WithSkipPaths(healthPath, readinessPath)))
router.Use(httpmiddleware.CollectMetrics(collector))
router.Use(httpmiddleware.RequestLog(logger, resolveRoute, healthPath, readinessPath))
router.Use(httpmiddleware.CollectMetrics(collector, resolveRoute))
router.Group(func(operational chi.Router) {
operational.Use(kitmiddleware.Recoverer(logger))
operational.Get(healthPath, health.Healthz)
@@ -65,7 +67,7 @@ func NewRouter(
// server aborts the response; the kit Recoverer would swallow that panic.
router.Group(func(public chi.Router) {
public.Use(problems.Recover)
public.Handle(publicPrefix+"/*", apiServer)
public.Handle(apiWildcard, apiServer)
// Unknown /api paths outside the versioned prefix are also problems,
// so every error under /api has the contract's format.
public.Handle(apiRoot, http.HandlerFunc(problems.NotFound))
@@ -73,3 +75,23 @@ func NewRouter(
})
return router, nil
}
// apiWildcard is the chi pattern that mounts the generated server.
const apiWildcard = publicPrefix + "/*"
// operationRoute names a request under the versioned prefix by the OpenAPI
// path template of its operation, such as /api/v1/me/invitations/{invitation_id},
// so the metrics tell operations apart without a path parameter in a label.
// A path or method that matches no operation keeps the wildcard pattern.
func operationRoute(server *apiadapter.Server) httpmiddleware.RouteResolver {
return func(r *http.Request, pattern string) string {
if pattern != apiWildcard {
return pattern
}
operation, ok := server.FindPath(r.Method, r.URL)
if !ok {
return pattern
}
return operation.PathPattern()
}
}