94 lines
3.0 KiB
Go
94 lines
3.0 KiB
Go
package metrics
|
|
|
|
import (
|
|
"expvar"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
var (
|
|
Registry = prometheus.NewRegistry()
|
|
|
|
HTTPRequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Name: "http_requests_total",
|
|
Help: "Total number of HTTP requests by method, normalized path, and status.",
|
|
}, []string{"method", "path", "status"})
|
|
|
|
HTTPRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
Name: "http_request_duration_seconds",
|
|
Help: "HTTP request duration by method, normalized path, and status.",
|
|
Buckets: []float64{0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
|
|
}, []string{"method", "path", "status"})
|
|
|
|
DBQueryDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
Name: "db_query_duration_seconds",
|
|
Help: "Database operation duration by driver, operation, and status.",
|
|
Buckets: []float64{0.001, 0.003, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5},
|
|
}, []string{"driver", "operation", "status"})
|
|
|
|
RealtimeFanoutEventsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
Name: "realtime_fanout_events_total",
|
|
Help: "Total number of realtime fanout events by stage, kind, target type, and status.",
|
|
}, []string{"stage", "kind", "target_type", "status"})
|
|
|
|
RealtimeFanoutDeliveryDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
|
Name: "realtime_fanout_delivery_duration_seconds",
|
|
Help: "Realtime fanout delivery age measured from message timestamp to local dispatch.",
|
|
Buckets: []float64{0.001, 0.003, 0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10},
|
|
}, []string{"kind", "target_type"})
|
|
|
|
WSConnectionsActive = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
|
|
Name: "ws_connections_active",
|
|
Help: "Current active WebSocket connections.",
|
|
}, func() float64 {
|
|
return float64(expvarIntValue("LiveSessions"))
|
|
})
|
|
)
|
|
|
|
func init() {
|
|
Registry.MustRegister(WSConnectionsActive, HTTPRequestsTotal, HTTPRequestDuration, DBQueryDuration,
|
|
RealtimeFanoutEventsTotal, RealtimeFanoutDeliveryDuration)
|
|
}
|
|
|
|
func ObserveDB(driverName, operation string, started time.Time, err error) {
|
|
status := "ok"
|
|
if err != nil {
|
|
status = "error"
|
|
}
|
|
DBQueryDuration.WithLabelValues(driverName, operation, status).Observe(time.Since(started).Seconds())
|
|
}
|
|
|
|
func IncRealtimeFanout(stage, kind, targetType, status string) {
|
|
RealtimeFanoutEventsTotal.WithLabelValues(normalizeLabel(stage), normalizeLabel(kind),
|
|
normalizeLabel(targetType), normalizeLabel(status)).Inc()
|
|
}
|
|
|
|
func ObserveRealtimeFanoutDelivery(kind, targetType string, timestamp time.Time) {
|
|
if timestamp.IsZero() {
|
|
return
|
|
}
|
|
RealtimeFanoutDeliveryDuration.WithLabelValues(normalizeLabel(kind), normalizeLabel(targetType)).
|
|
Observe(time.Since(timestamp).Seconds())
|
|
}
|
|
|
|
func normalizeLabel(value string) string {
|
|
if value == "" {
|
|
return "unknown"
|
|
}
|
|
return value
|
|
}
|
|
|
|
func expvarIntValue(name string) int64 {
|
|
value := expvar.Get(name)
|
|
if value == nil {
|
|
return 0
|
|
}
|
|
n, err := strconv.ParseInt(value.String(), 10, 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|