From 06038b2af68467bd059173639b253590c443ef03 Mon Sep 17 00:00:00 2001 From: Sanne Raymaekers Date: Fri, 23 Jun 2023 00:03:34 +0200 Subject: [PATCH] internal/prometheus: add tenant to http and status metrics --- internal/prometheus/http_metrics.go | 2 +- internal/prometheus/middleware.go | 10 +++++++++- internal/prometheus/status_metrics.go | 2 +- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/internal/prometheus/http_metrics.go b/internal/prometheus/http_metrics.go index 0b2d912e1..6545fdba1 100644 --- a/internal/prometheus/http_metrics.go +++ b/internal/prometheus/http_metrics.go @@ -30,5 +30,5 @@ var ( Subsystem: ComposerSubsystem, Help: "Duration of HTTP requests.", Buckets: []float64{.025, .05, .075, .1, .2, .5, .75, 1, 1.5, 2, 3, 4, 5, 6, 8, 10, 12, 14, 16, 20}, - }, []string{"path"}) + }, []string{"path", "tenant"}) ) diff --git a/internal/prometheus/middleware.go b/internal/prometheus/middleware.go index 3be932400..58eb216cf 100644 --- a/internal/prometheus/middleware.go +++ b/internal/prometheus/middleware.go @@ -7,6 +7,8 @@ import ( "github.com/labstack/echo/v4" "github.com/prometheus/client_golang/prometheus" + + "github.com/osbuild/osbuild-composer/internal/auth" ) func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc { @@ -15,7 +17,10 @@ func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc { if strings.HasSuffix(ctx.Path(), "/compose") { ComposeRequests.Inc() } - timer := prometheus.NewTimer(httpDuration.WithLabelValues(ctx.Path())) + + // leave tenant empty in case it's not in the context + tenant, _ := ctx.Get(auth.TenantCtxKey).(string) + timer := prometheus.NewTimer(httpDuration.WithLabelValues(ctx.Path(), tenant)) defer timer.ObserveDuration() return next(ctx) } @@ -35,6 +40,8 @@ func StatusMiddleware(subsystem string) func(next echo.HandlerFunc) echo.Handler path := pathLabel(ctx.Path()) method := ctx.Request().Method status := ctx.Response().Status + // leave tenant empty in case it's not in the context + tenant, _ := ctx.Get(auth.TenantCtxKey).(string) httpErr := new(echo.HTTPError) if errors.As(err, &httpErr) { @@ -46,6 +53,7 @@ func StatusMiddleware(subsystem string) func(next echo.HandlerFunc) echo.Handler path, strconv.Itoa(status), subsystem, + tenant, ).Inc() return err diff --git a/internal/prometheus/status_metrics.go b/internal/prometheus/status_metrics.go index b48b42b60..be5ed78c2 100644 --- a/internal/prometheus/status_metrics.go +++ b/internal/prometheus/status_metrics.go @@ -14,7 +14,7 @@ func StatusRequestsCounter(subsystem string) *prometheus.CounterVec { Namespace: Namespace, Subsystem: subsystem, Help: "total number of http requests", - }, []string{"method", "path", "code", "subsystem"}) + }, []string{"method", "path", "code", "subsystem", "tenant"}) err := prometheus.Register(counter) if err != nil {