feat(outbox): relay committed outbox events to Kafka

This commit is contained in:
shira committed 2026-10-06 03:01:53 +03:00
1 parent 78f1651f6b
commit 6c3b34f73e
42 files changed
+3731 -45

No files matched your search

@@ -8,14 +8,31 @@ import (
client "github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"lincodex/backend/internal/port/outbound"
)
type Metrics struct {
registry *client.Registry
requests *client.CounterVec
latency *client.HistogramVec
// relayEvents counts outbox events by relay outcome: published or
// failed. A failed event stays pending and is counted again on every
// retry.
relayEvents *client.CounterVec
// relayPending and relayOldestAge describe the unpublished outbox
// events. Every backend instance reports the same database-wide values,
// so dashboards aggregate them with max, not sum.
relayPending client.Gauge
relayOldestAge client.Gauge
}
// Outbox relay outcome label values.
const (
relayOutcomePublished = "published"
relayOutcomeFailed = "failed"
)
func New() *Metrics {
registry := client.NewRegistry()
requests := client.NewCounterVec(client.CounterOpts{
@@ -27,8 +44,51 @@ func New() *Metrics {
Help: "HTTP request duration in seconds.",
Buckets: client.DefBuckets,
}, []string{"method", "route"})
registry.MustRegister(collectors.NewGoCollector(), collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}), requests, latency)
return &Metrics{registry: registry, requests: requests, latency: latency}
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.",
}, []string{"outcome"})
relayPending := client.NewGauge(client.GaugeOpts{
Name: "lincodex_outbox_relay_pending_events",
Help: "Unpublished outbox events at the last backlog read by the relay.",
})
relayOldestAge := client.NewGauge(client.GaugeOpts{
Name: "lincodex_outbox_relay_oldest_pending_age_seconds",
Help: "Age of the earliest unpublished outbox event at the last backlog read by the relay; 0 without pending events.",
})
registry.MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
requests, latency, relayEvents, relayPending, relayOldestAge,
)
// Expose both series from the start so rate queries see zero, not absence.
relayEvents.WithLabelValues(relayOutcomePublished)
relayEvents.WithLabelValues(relayOutcomeFailed)
return &Metrics{
registry: registry,
requests: requests,
latency: latency,
relayEvents: relayEvents,
relayPending: relayPending,
relayOldestAge: relayOldestAge,
}
}
// ObserveOutboxBacklog records the latest backlog read.
func (metrics *Metrics) ObserveOutboxBacklog(backlog outbound.OutboxBacklog) {
metrics.relayPending.Set(float64(backlog.Pending))
metrics.relayOldestAge.Set(backlog.OldestAge.Seconds())
}
// ObserveOutboxRelayPass adds the events one relay pass published and those
// it claimed but could not publish.
func (metrics *Metrics) ObserveOutboxRelayPass(published, failed int) {
if published > 0 {
metrics.relayEvents.WithLabelValues(relayOutcomePublished).Add(float64(published))
}
if failed > 0 {
metrics.relayEvents.WithLabelValues(relayOutcomeFailed).Add(float64(failed))
}
}
func (metrics *Metrics) ObserveRequest(method, route string, status int, duration time.Duration) {
@@ -5,6 +5,8 @@ import (
"strings"
"testing"
"time"
"lincodex/backend/internal/port/outbound"
)
func TestMetricsHandlerExposesBoundedRequestLabels(t *testing.T) {
@@ -21,3 +23,38 @@ func TestMetricsHandlerExposesBoundedRequestLabels(t *testing.T) {
t.Fatalf("metrics output is missing request duration:\n%s", body)
}
}
func TestMetricsHandlerExposesOutboxRelayOutcomes(t *testing.T) {
metrics := New()
metrics.ObserveOutboxRelayPass(3, 0)
metrics.ObserveOutboxRelayPass(1, 2)
response := httptest.NewRecorder()
metrics.Handler().ServeHTTP(response, httptest.NewRequest("GET", "/metrics", nil))
body := response.Body.String()
for _, want := range []string{
`lincodex_outbox_relay_events_total{outcome="published"} 4`,
`lincodex_outbox_relay_events_total{outcome="failed"} 2`,
} {
if !strings.Contains(body, want) {
t.Fatalf("metrics output is missing %s:\n%s", want, body)
}
}
}
func TestMetricsHandlerExposesTheOutboxBacklog(t *testing.T) {
metrics := New()
metrics.ObserveOutboxBacklog(outbound.OutboxBacklog{Pending: 42, OldestAge: 90 * time.Second})
response := httptest.NewRecorder()
metrics.Handler().ServeHTTP(response, httptest.NewRequest("GET", "/metrics", nil))
body := response.Body.String()
for _, want := range []string{
"lincodex_outbox_relay_pending_events 42",
"lincodex_outbox_relay_oldest_pending_age_seconds 90",
} {
if !strings.Contains(body, want) {
t.Fatalf("metrics output is missing %s:\n%s", want, body)
}
}
}