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

@@ -15,7 +15,11 @@ import (
type Metrics struct {
registry *client.Registry
requests *client.CounterVec
latency *client.HistogramVec
// latency carries the status class, so a fast stream of rejected
// requests does not hide slow successful ones.
latency *client.HistogramVec
// problems counts error responses by the problem code they carry.
problems *client.CounterVec
// relayEvents counts outbox events by relay outcome: published or
// failed. A failed event stays pending and is counted again on every
// retry.
@@ -40,6 +44,9 @@ type Metrics struct {
scheduledJobsDue client.Gauge
scheduledJobsOldestDueAge client.Gauge
scheduledJobsDead client.Gauge
mail mailMetrics
assessment assessmentMetrics
}
// Outbox relay outcome label values.
@@ -65,9 +72,13 @@ func New() *Metrics {
}, []string{"method", "route", "status"})
latency := client.NewHistogramVec(client.HistogramOpts{
Name: "lincodex_http_request_duration_seconds",
Help: "HTTP request duration in seconds.",
Help: "HTTP request duration in seconds, by method, route template, and status class.",
Buckets: client.DefBuckets,
}, []string{"method", "route"})
}, []string{"method", "route", "status_class"})
problems := client.NewCounterVec(client.CounterOpts{
Name: "lincodex_http_problems_total",
Help: "Public API error responses by route template and the code of the RFC 9457 problem they carry.",
}, []string{"route", "code"})
relayEvents := client.NewCounterVec(client.CounterOpts{
Name: "lincodex_outbox_relay_events_total",
Help: "Outbox events handled by the relay, by outcome; failed events stay pending and are retried.",
@@ -104,13 +115,17 @@ func New() *Metrics {
Name: "lincodex_scheduled_jobs_dead",
Help: "Dead scheduled jobs, which need an operator, at the last backlog read.",
})
mail := newMailMetrics()
assessment := newAssessmentMetrics()
registry.MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
requests, latency, relayEvents, relayPending, relayOldestAge,
requests, latency, problems, relayEvents, relayPending, relayOldestAge,
consumerRecords, consumerFailures,
scheduledJobs, scheduledJobsDue, scheduledJobsOldestDueAge, scheduledJobsDead,
)
registry.MustRegister(mail.collectors()...)
registry.MustRegister(assessment.collectors()...)
// Expose both series from the start so rate queries see zero, not absence.
relayEvents.WithLabelValues(relayOutcomePublished)
relayEvents.WithLabelValues(relayOutcomeFailed)
@@ -118,6 +133,7 @@ func New() *Metrics {
registry: registry,
requests: requests,
latency: latency,
problems: problems,
relayEvents: relayEvents,
relayPending: relayPending,
relayOldestAge: relayOldestAge,
@@ -129,6 +145,9 @@ func New() *Metrics {
scheduledJobsDue: scheduledJobsDue,
scheduledJobsOldestDueAge: scheduledJobsOldestDueAge,
scheduledJobsDead: scheduledJobsDead,
mail: mail,
assessment: assessment,
}
}
@@ -201,7 +220,36 @@ func (metrics *Metrics) ObserveRequest(method, route string, status int, duratio
method = "OTHER"
}
metrics.requests.WithLabelValues(method, route, strconv.Itoa(status)).Inc()
metrics.latency.WithLabelValues(method, route).Observe(duration.Seconds())
metrics.latency.WithLabelValues(method, route, statusClass(status)).Observe(duration.Seconds())
}
// problemCodes mirror the problem codes of the public contract, which this
// adapter does not import; a test in the HTTP adapter checks that every
// contract code is listed.
var problemCodes = []string{
"validation_failed", "verification_token_invalid", "verification_token_expired",
"unauthenticated", "invalid_credentials", "forbidden", "email_not_verified",
"csrf_check_failed", "not_found", "method_not_allowed", "idempotency_in_progress",
"state_conflict", "version_conflict", "payload_too_large", "unsupported_media_type",
"idempotency_key_reused", "precondition_required", "rate_limited", "internal",
"not_implemented",
}
// ObserveProblem counts one error response. A code outside the contract
// catalogue is counted as "other"; an empty route is the unmatched one.
func (metrics *Metrics) ObserveProblem(route, code string) {
if route == "" {
route = "unmatched"
}
metrics.problems.WithLabelValues(route, boundedLabel(code, problemCodes)).Inc()
}
// statusClass maps a status code to 1xx-5xx, or "other" outside them.
func statusClass(status int) string {
if status < 100 || status > 599 {
return "other"
}
return strconv.Itoa(status/100) + "xx"
}
func (metrics *Metrics) Handler() http.Handler {