From ed0145838cd29eb887722ce3071a6a553c63f873 Mon Sep 17 00:00:00 2001 From: Brian Fox Date: Tue, 20 Dec 2022 14:26:09 +0100 Subject: [PATCH] fix(otelfiber): ensure route and size metrics are correctly populated Also adds the status code --- otelfiber/fiber.go | 39 +++++++++++++++++++++++++++------------ otelfiber/semconv.go | 1 - 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/otelfiber/fiber.go b/otelfiber/fiber.go index 1469d8b5..6a4ffaa5 100644 --- a/otelfiber/fiber.go +++ b/otelfiber/fiber.go @@ -11,6 +11,7 @@ import ( "github.com/gofiber/fiber/v2/utils" otelcontrib "go.opentelemetry.io/contrib" "go.opentelemetry.io/otel" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/metric/global" "go.opentelemetry.io/otel/metric/instrument" @@ -82,16 +83,13 @@ func Middleware(service string, opts ...Option) fiber.Handler { savedCtx, cancel := context.WithCancel(c.UserContext()) start := time.Now() - metricAttrs := httpServerMetricAttributesFromRequest(c, service) - httpServerActiveRequests.Add(savedCtx, 1, metricAttrs...) - defer func() { - httpServerDuration.Record(savedCtx, float64(time.Since(start).Microseconds())/1000, metricAttrs...) - httpServerRequestSize.Record(savedCtx, int64(len(c.Request().Body())), metricAttrs...) - httpServerResponseSize.Record(savedCtx, int64(len(c.Response().Body())), metricAttrs...) - httpServerActiveRequests.Add(savedCtx, -1, metricAttrs...) - c.SetUserContext(savedCtx) - cancel() - }() + + requestMetricsAttrs := httpServerMetricAttributesFromRequest(c, service) + httpServerActiveRequests.Add(savedCtx, 1, requestMetricsAttrs...) + + responseMetricAttrs := make([]attribute.KeyValue, len(requestMetricsAttrs)) + copy(responseMetricAttrs, requestMetricsAttrs) + reqHeader := make(http.Header) c.Request().Header.VisitAll(func(k, v []byte) { reqHeader.Add(string(k), string(v)) @@ -135,8 +133,25 @@ func Middleware(service string, opts ...Option) fiber.Handler { err := c.Next() span.SetName(cfg.SpanNameFormatter(c)) - // no need to copy c.Route().Path: route strings should be immutable across app lifecycle - span.SetAttributes(semconv.HTTPRouteKey.String(c.Route().Path)) + + routeAttr := semconv.HTTPRouteKey.String(c.Route().Path) // no need to copy c.Route().Path: route strings should be immutable across app lifecycle + + span.SetAttributes(routeAttr) + + responseMetricAttrs = append( + requestMetricsAttrs, + routeAttr) + requestSize := int64(len(c.Request().Body())) + responseSize := int64(len(c.Response().Body())) + + defer func() { + httpServerDuration.Record(savedCtx, float64(time.Since(start).Microseconds())/1000, responseMetricAttrs...) + httpServerRequestSize.Record(savedCtx, requestSize, responseMetricAttrs...) + httpServerResponseSize.Record(savedCtx, responseSize, responseMetricAttrs...) + httpServerActiveRequests.Add(savedCtx, -1, requestMetricsAttrs...) + c.SetUserContext(savedCtx) + cancel() + }() if err != nil { span.RecordError(err) diff --git a/otelfiber/semconv.go b/otelfiber/semconv.go index 7a9aae0b..a0ea09f6 100644 --- a/otelfiber/semconv.go +++ b/otelfiber/semconv.go @@ -18,6 +18,5 @@ func httpServerMetricAttributesFromRequest(c *fiber.Ctx, service string) []attri attrs = append(attrs, semconv.HTTPHostKey.String(utils.CopyString(c.Hostname()))) attrs = append(attrs, semconv.HTTPFlavorHTTP11) attrs = append(attrs, semconv.HTTPMethodKey.String(utils.CopyString(c.Method()))) - attrs = append(attrs, semconv.HTTPRouteKey.String(utils.CopyString(c.Route().Path))) return attrs }