diff options
author | 2023-09-04 17:15:14 +0200 | |
---|---|---|
committer | 2023-09-04 17:15:14 +0200 | |
commit | 5f10885ddeda5e982244fd15432b42fdc6fbe4ae (patch) | |
tree | 151a42db3567b6714d0aa493af8ff48f42af8ee4 /internal/tracing/tracing.go | |
parent | [feature] Store admin actions in the db, prevent conflicting actions (#2167) (diff) | |
download | gotosocial-5f10885ddeda5e982244fd15432b42fdc6fbe4ae.tar.xz |
[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.
Diffstat (limited to 'internal/tracing/tracing.go')
-rw-r--r-- | internal/tracing/tracing.go | 19 |
1 files 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))) |