[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.
This commit is contained in:
parent
3ed1ca68e5
commit
5f10885dde
|
@ -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))
|
||||
}
|
||||
id := gtscontext.RequestID(c.Request.Context())
|
||||
if id != "" {
|
||||
opts = append(opts, oteltrace.WithAttributes(attribute.String("requestID", id)))
|
||||
|
|
Loading…
Reference in New Issue