internal/prometheus: add tenant to http and status metrics

This commit is contained in:
Sanne Raymaekers 2023-06-23 00:03:34 +02:00
parent 0f946e1c9e
commit 06038b2af6
3 changed files with 11 additions and 3 deletions

View file

@ -30,5 +30,5 @@ var (
Subsystem: ComposerSubsystem, Subsystem: ComposerSubsystem,
Help: "Duration of HTTP requests.", 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}, 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"})
) )

View file

@ -7,6 +7,8 @@ import (
"github.com/labstack/echo/v4" "github.com/labstack/echo/v4"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"github.com/osbuild/osbuild-composer/internal/auth"
) )
func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc { func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
@ -15,7 +17,10 @@ func MetricsMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
if strings.HasSuffix(ctx.Path(), "/compose") { if strings.HasSuffix(ctx.Path(), "/compose") {
ComposeRequests.Inc() 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() defer timer.ObserveDuration()
return next(ctx) return next(ctx)
} }
@ -35,6 +40,8 @@ func StatusMiddleware(subsystem string) func(next echo.HandlerFunc) echo.Handler
path := pathLabel(ctx.Path()) path := pathLabel(ctx.Path())
method := ctx.Request().Method method := ctx.Request().Method
status := ctx.Response().Status 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) httpErr := new(echo.HTTPError)
if errors.As(err, &httpErr) { if errors.As(err, &httpErr) {
@ -46,6 +53,7 @@ func StatusMiddleware(subsystem string) func(next echo.HandlerFunc) echo.Handler
path, path,
strconv.Itoa(status), strconv.Itoa(status),
subsystem, subsystem,
tenant,
).Inc() ).Inc()
return err return err

View file

@ -14,7 +14,7 @@ func StatusRequestsCounter(subsystem string) *prometheus.CounterVec {
Namespace: Namespace, Namespace: Namespace,
Subsystem: subsystem, Subsystem: subsystem,
Help: "total number of http requests", Help: "total number of http requests",
}, []string{"method", "path", "code", "subsystem"}) }, []string{"method", "path", "code", "subsystem", "tenant"})
err := prometheus.Register(counter) err := prometheus.Register(counter)
if err != nil { if err != nil {