110 lines
3.8 KiB
Go
110 lines
3.8 KiB
Go
package prometheus
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
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{
|
|
Name: "lincodex_http_requests_total",
|
|
Help: "Total number of HTTP requests served by the API.",
|
|
}, []string{"method", "route", "status"})
|
|
latency := client.NewHistogramVec(client.HistogramOpts{
|
|
Name: "lincodex_http_request_duration_seconds",
|
|
Help: "HTTP request duration in seconds.",
|
|
Buckets: client.DefBuckets,
|
|
}, []string{"method", "route"})
|
|
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) {
|
|
if route == "" {
|
|
route = "unmatched"
|
|
}
|
|
switch method {
|
|
case http.MethodGet, http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete, http.MethodHead, http.MethodOptions:
|
|
default:
|
|
method = "OTHER"
|
|
}
|
|
metrics.requests.WithLabelValues(method, route, strconv.Itoa(status)).Inc()
|
|
metrics.latency.WithLabelValues(method, route).Observe(duration.Seconds())
|
|
}
|
|
|
|
func (metrics *Metrics) Handler() http.Handler {
|
|
return promhttp.HandlerFor(metrics.registry, promhttp.HandlerOpts{})
|
|
}
|