From 5f10885ddeda5e982244fd15432b42fdc6fbe4ae Mon Sep 17 00:00:00 2001 From: Daenney Date: Mon, 4 Sep 2023 17:15:14 +0200 Subject: [PATCH] [feature] Don't trace non-existing routes (#2172) c.FullPath() is the empty string if a request doesn't match any route on our mux. In those cases, there's no value in emitting a trace. The trace will be empty, containing no other information beyond the fact that we didn't match a route. Since Gin breaks off the processing early we don't need to trace this request as it won't do anything and consumes no further resources. The 404 will still be emitted by our logs and will be visible from a reverse proxy too. --- internal/tracing/tracing.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/internal/tracing/tracing.go b/internal/tracing/tracing.go index a4b664fb2..16e5a5eb5 100644 --- a/internal/tracing/tracing.go +++ b/internal/tracing/tracing.go @@ -117,6 +117,15 @@ func InstrumentGin() gin.HandlerFunc { ) propagator := otel.GetTextMapPropagator() return func(c *gin.Context) { + spanName := c.FullPath() + // Do not trace a request if it didn't match a route. This doesn't omit + // all 404s as a request matching /:user for a user that doesn't exist + // still matches the route + if spanName == "" { + c.Next() + return + } + c.Set(tracerKey, tracer) savedCtx := c.Request.Context() defer func() { @@ -127,13 +136,9 @@ func InstrumentGin() gin.HandlerFunc { oteltrace.WithAttributes(httpconv.ServerRequest(config.GetHost(), c.Request)...), oteltrace.WithSpanKind(oteltrace.SpanKindServer), } - spanName := c.FullPath() - if spanName == "" { - spanName = fmt.Sprintf("HTTP %s route not found", c.Request.Method) - } else { - rAttr := semconv.HTTPRoute(spanName) - opts = append(opts, oteltrace.WithAttributes(rAttr)) - } + + rAttr := semconv.HTTPRoute(spanName) + opts = append(opts, oteltrace.WithAttributes(rAttr)) id := gtscontext.RequestID(c.Request.Context()) if id != "" { opts = append(opts, oteltrace.WithAttributes(attribute.String("requestID", id)))