Skip to content

Commit

Permalink
fix(otelfiber): ensure route and size metrics are correctly populated
Browse files Browse the repository at this point in the history
Also adds the status code
  • Loading branch information
onematchfox committed Dec 20, 2022
1 parent b9cfd37 commit ed01458
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
39 changes: 27 additions & 12 deletions otelfiber/fiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion otelfiber/semconv.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit ed01458

Please sign in to comment.