diff --git a/cmd/baker/main.go b/cmd/baker/main.go index 07aa66b..b425bc6 100644 --- a/cmd/baker/main.go +++ b/cmd/baker/main.go @@ -49,6 +49,7 @@ https://ella.to/baker slog.SetLogLoggerLevel(parseLogLevel(logLevel)) metricsHandler := metrics.SetupHandler() + metrics.SetInfo(Version, GitCommit) dockerGetter, err := httpclient.NewClient( httpclient.WithUnixSock("/var/run/docker.sock", "http://localhost"), diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index bd05870..3a44f8b 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -9,18 +9,27 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -var processedRequests = prometheus.NewCounterVec( +var websocketRequestCount = prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: "baker", - Name: "baker_pattern_requests_total", + Name: "websocket_request_count", + Help: "How many WebSocket requests processed, partitioned by status code, method and HTTP path.", + }, + []string{"domain", "path", "method", "code"}, +) + +var httpRequestCount = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: "baker", + Name: "http_request_count", Help: "How many HTTP requests processed, partitioned by status code, method and HTTP path (with patterns).", }, []string{"domain", "path", "method", "code"}, ) -var requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ +var httpRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ Namespace: "baker", - Name: "baker_pattern_request_duration_seconds", + Name: "http_request_duration_seconds", Help: "How long it took to process the request, partitioned by status code, method and HTTP path (with patterns).", Buckets: []float64{.1, .3, 1, 1.5, 2, 5, 10}, }, @@ -28,9 +37,9 @@ var requestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ ) var infoGuage = prometheus.NewGaugeVec(prometheus.GaugeOpts{ - Namespace: "bus", + Namespace: "baker", Name: "info", - Help: "Information about the bus version and commit hash", + Help: "Information about the baker version and commit hash", }, []string{"version", "commit"}) func SetInfo(version, commit string) { @@ -40,8 +49,8 @@ func SetInfo(version, commit string) { }).Set(1) } -func HttpProcessedRequest(domain string, method string, path string, code int) { - processedRequests.With(prometheus.Labels{ +func HttpRequestCount(domain string, method string, path string, code int) { + httpRequestCount.With(prometheus.Labels{ "domain": domain, "method": method, "path": path, @@ -50,7 +59,7 @@ func HttpProcessedRequest(domain string, method string, path string, code int) { } func HttpRequestDuration(domain string, method string, path string, code int, duration float64) { - requestDuration.With(prometheus.Labels{ + httpRequestDuration.With(prometheus.Labels{ "domain": domain, "method": method, "path": path, @@ -58,14 +67,24 @@ func HttpRequestDuration(domain string, method string, path string, code int, du }).Observe(duration) } +func WebsocketRequest(domain string, method string, path string, code int) { + websocketRequestCount.With(prometheus.Labels{ + "domain": domain, + "method": method, + "path": path, + "code": strconv.FormatInt(int64(code), 10), + }).Inc() +} + func SetupHandler() http.Handler { req := prometheus.NewRegistry() req.MustRegister( collectors.NewGoCollector(), infoGuage, - processedRequests, - requestDuration, + httpRequestCount, + httpRequestDuration, + websocketRequestCount, ) // Create a custom http serve mux diff --git a/server.go b/server.go index fc13ab9..783765a 100644 --- a/server.go +++ b/server.go @@ -217,10 +217,6 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { tw := &trackResponseWriter{w: w} start := time.Now() - defer func() { - metrics.HttpProcessedRequest(domain, path, method, tw.statusCode) - metrics.HttpRequestDuration(domain, path, method, tw.statusCode, float64(time.Since(start))) - }() var container *Container endpoint := &Endpoint{ @@ -236,8 +232,15 @@ func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { } if isWebSocketRequest(r) { + defer func() { + metrics.WebsocketRequest(domain, path, method, tw.statusCode) + }() s.handleWebSocket(tw, r, container) } else { + defer func() { + metrics.HttpRequestCount(domain, path, method, tw.statusCode) + metrics.HttpRequestDuration(domain, path, method, tw.statusCode, float64(time.Since(start))) + }() s.handleHTTP(tw, r, container, endpoint) } }