mirror of
				https://github.com/superseriousbusiness/gotosocial.git
				synced 2025-10-31 14:22:25 -05:00 
			
		
		
		
	[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
					
				
			
		
					 1 changed files with 12 additions and 7 deletions
				
			
		|  | @ -117,6 +117,15 @@ func InstrumentGin() gin.HandlerFunc { | ||||||
| 	) | 	) | ||||||
| 	propagator := otel.GetTextMapPropagator() | 	propagator := otel.GetTextMapPropagator() | ||||||
| 	return func(c *gin.Context) { | 	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) | 		c.Set(tracerKey, tracer) | ||||||
| 		savedCtx := c.Request.Context() | 		savedCtx := c.Request.Context() | ||||||
| 		defer func() { | 		defer func() { | ||||||
|  | @ -127,13 +136,9 @@ func InstrumentGin() gin.HandlerFunc { | ||||||
| 			oteltrace.WithAttributes(httpconv.ServerRequest(config.GetHost(), c.Request)...), | 			oteltrace.WithAttributes(httpconv.ServerRequest(config.GetHost(), c.Request)...), | ||||||
| 			oteltrace.WithSpanKind(oteltrace.SpanKindServer), | 			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) | 		rAttr := semconv.HTTPRoute(spanName) | ||||||
| 		opts = append(opts, oteltrace.WithAttributes(rAttr)) | 		opts = append(opts, oteltrace.WithAttributes(rAttr)) | ||||||
| 		} |  | ||||||
| 		id := gtscontext.RequestID(c.Request.Context()) | 		id := gtscontext.RequestID(c.Request.Context()) | ||||||
| 		if id != "" { | 		if id != "" { | ||||||
| 			opts = append(opts, oteltrace.WithAttributes(attribute.String("requestID", id))) | 			opts = append(opts, oteltrace.WithAttributes(attribute.String("requestID", id))) | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue