Files
gohoarder/pkg/metrics/metrics.go
T
lukaszraczylo 6f604367da fix(websocket): start the event loop and throttle drop warnings
The app built the websocket server but never called Start, so nothing read the 256-slot broadcast channel. After the first 256 events every event was dropped with a warning, and dashboard clients blocked forever on the unbuffered register channel, leaking a goroutine per connection and never receiving events. Run now starts the loop with the other workers. Dropped events are counted in gohoarder_websocket_events_dropped_total and warned about at most once a minute.
2026-11-23 12:56:32 +00:00

174 lines
4.7 KiB
Go

// Package metrics exposes Prometheus instrumentation for cache, storage,
// scanner, and HTTP request flows.
package metrics
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
// HTTP metrics
HTTPRequestsTotal = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "gohoarder_http_requests_total",
Help: "Total number of HTTP requests",
},
[]string{"handler", "method", "status"},
)
HTTPRequestDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "gohoarder_http_request_duration_seconds",
Help: "HTTP request duration in seconds",
Buckets: prometheus.DefBuckets,
},
[]string{"handler", "method"},
)
// Cache metrics
CacheRequests = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "gohoarder_cache_requests_total",
Help: "Total number of cache requests",
},
[]string{"status", "handler"}, // hit, miss, error
)
CacheSizeBytes = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "gohoarder_cache_size_bytes",
Help: "Current cache size in bytes",
},
[]string{"backend"},
)
CacheItemsTotal = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "gohoarder_cache_items_total",
Help: "Total number of cached items",
},
[]string{"handler"},
)
CacheEvictions = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "gohoarder_cache_evictions_total",
Help: "Total number of cache evictions",
},
[]string{"reason"}, // ttl, lru, manual
)
// WebSocketEventsDropped counts realtime events dropped because the broadcast channel was full.
WebSocketEventsDropped = promauto.NewCounter(
prometheus.CounterOpts{
Name: "gohoarder_websocket_events_dropped_total",
Help: "Realtime dashboard events dropped because the broadcast channel was full",
},
)
// Storage metrics
StorageOperations = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "gohoarder_storage_operations_total",
Help: "Total number of storage operations",
},
[]string{"backend", "operation", "status"}, // get, put, delete
)
StorageQuotaBytes = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "gohoarder_storage_quota_bytes",
Help: "Storage quota in bytes per project",
},
[]string{"project"},
)
// Upstream metrics
UpstreamRequests = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "gohoarder_upstream_requests_total",
Help: "Total number of upstream requests",
},
[]string{"registry", "status"},
)
UpstreamDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Name: "gohoarder_upstream_duration_seconds",
Help: "Upstream request duration in seconds",
Buckets: prometheus.DefBuckets,
},
[]string{"registry"},
)
// Security metrics
SecurityScans = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "gohoarder_security_scans_total",
Help: "Total number of security scans",
},
[]string{"scanner", "result"}, // clean, blocked, error
)
VulnerabilitiesFound = promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "gohoarder_vulnerabilities_found_total",
Help: "Total number of vulnerabilities found",
},
[]string{"severity"}, // low, medium, high, critical
)
// Circuit breaker metrics
CircuitBreakerState = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "gohoarder_circuit_breaker_state",
Help: "Circuit breaker state (0=closed, 1=open, 2=half-open)",
},
[]string{"name"},
)
)
// Handler returns the Prometheus HTTP handler
func Handler() http.Handler {
return promhttp.Handler()
}
// RecordCacheHit records a cache hit
func RecordCacheHit(handler string) {
CacheRequests.WithLabelValues("hit", handler).Inc()
}
// RecordCacheMiss records a cache miss
func RecordCacheMiss(handler string) {
CacheRequests.WithLabelValues("miss", handler).Inc()
}
// UpdateCacheSize updates the cache size metric
func UpdateCacheSize(backend string, bytes int64) {
CacheSizeBytes.WithLabelValues(backend).Set(float64(bytes))
}
// RecordCacheEviction records a cache eviction
func RecordCacheEviction(reason string) {
CacheEvictions.WithLabelValues(reason).Inc()
}
// RecordStorageOperation records a storage operation
func RecordStorageOperation(backend, operation, status string) {
StorageOperations.WithLabelValues(backend, operation, status).Inc()
}
// RecordUpstreamRequest records an upstream request
func RecordUpstreamRequest(registry, status string) {
UpstreamRequests.WithLabelValues(registry, status).Inc()
}
// UpdateCircuitBreakerState updates the circuit breaker state
func UpdateCircuitBreakerState(name string, state int) {
CircuitBreakerState.WithLabelValues(name).Set(float64(state))
}