| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-06 12:14:37 +01:00
										 |  |  | //go:build !nootel | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-06 12:14:37 +01:00
										 |  |  | package observability | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"context" | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 	"net" | 
					
						
							|  |  |  | 	"net/http" | 
					
						
							|  |  |  | 	"strconv" | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-29 09:23:20 +02:00
										 |  |  | 	"codeberg.org/gruf/go-kv/v2" | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"go.opentelemetry.io/contrib/exporters/autoexport" | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 	"go.opentelemetry.io/otel" | 
					
						
							|  |  |  | 	"go.opentelemetry.io/otel/attribute" | 
					
						
							|  |  |  | 	"go.opentelemetry.io/otel/propagation" | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 	sdktrace "go.opentelemetry.io/otel/sdk/trace" | 
					
						
							| 
									
										
										
										
											2024-03-11 15:34:34 +01:00
										 |  |  | 	semconv "go.opentelemetry.io/otel/semconv/v1.24.0" | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 	"go.opentelemetry.io/otel/trace" | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/config" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtscontext" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/log" | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	tracerKey  = "gotosocial-server-tracer" | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	tracerName = "code.superseriousbusiness.org/gotosocial/internal/observability" | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | func InitializeTracing(ctx context.Context) error { | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 	if !config.GetTracingEnabled() { | 
					
						
							|  |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 	r, err := Resource() | 
					
						
							| 
									
										
										
										
											2024-03-04 05:10:15 -05:00
										 |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		// this can happen if semconv versioning is out-of-sync | 
					
						
							|  |  |  | 		return fmt.Errorf("building tracing resource: %w", err) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 	se, err := autoexport.NewSpanExporter(ctx) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	tp := sdktrace.NewTracerProvider( | 
					
						
							|  |  |  | 		sdktrace.WithResource(r), | 
					
						
							|  |  |  | 		sdktrace.WithBatcher(se), | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 	) | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 	otel.SetTracerProvider(tp) | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 	otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator( | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 		propagation.TraceContext{}, | 
					
						
							|  |  |  | 		propagation.Baggage{}, | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 	)) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 	log.Hook(func(ctx context.Context, kvs []kv.Field) []kv.Field { | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 		span := trace.SpanFromContext(ctx) | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 		if span != nil && span.SpanContext().HasTraceID() { | 
					
						
							|  |  |  | 			return append(kvs, kv.Field{K: "traceID", V: span.SpanContext().TraceID().String()}) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return kvs | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // InstrumentGin is a middleware injecting tracing information based on the | 
					
						
							|  |  |  | // otelgin implementation found at | 
					
						
							|  |  |  | // https://github.com/open-telemetry/opentelemetry-go-contrib/blob/main/instrumentation/github.com/gin-gonic/gin/otelgin/gintrace.go | 
					
						
							| 
									
										
										
										
											2025-02-06 12:14:37 +01:00
										 |  |  | func TracingMiddleware() gin.HandlerFunc { | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 	provider := otel.GetTracerProvider() | 
					
						
							|  |  |  | 	tracer := provider.Tracer( | 
					
						
							|  |  |  | 		tracerName, | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 		trace.WithInstrumentationVersion(config.GetSoftwareVersion()), | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 	) | 
					
						
							|  |  |  | 	propagator := otel.GetTextMapPropagator() | 
					
						
							|  |  |  | 	return func(c *gin.Context) { | 
					
						
							| 
									
										
										
										
											2023-09-04 17:15:14 +02:00
										 |  |  | 		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 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 		c.Set(tracerKey, tracer) | 
					
						
							|  |  |  | 		savedCtx := c.Request.Context() | 
					
						
							|  |  |  | 		defer func() { | 
					
						
							|  |  |  | 			c.Request = c.Request.WithContext(savedCtx) | 
					
						
							|  |  |  | 		}() | 
					
						
							|  |  |  | 		ctx := propagator.Extract(savedCtx, propagation.HeaderCarrier(c.Request.Header)) | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 		opts := []trace.SpanStartOption{ | 
					
						
							|  |  |  | 			trace.WithAttributes(ServerRequestAttributes(c.Request)...), | 
					
						
							|  |  |  | 			trace.WithSpanKind(trace.SpanKindServer), | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-09-04 17:15:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		rAttr := semconv.HTTPRoute(spanName) | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 		opts = append(opts, trace.WithAttributes(rAttr)) | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 		id := gtscontext.RequestID(c.Request.Context()) | 
					
						
							|  |  |  | 		if id != "" { | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 			opts = append(opts, trace.WithAttributes(attribute.String("requestID", id))) | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		ctx, span := tracer.Start(ctx, spanName, opts...) | 
					
						
							|  |  |  | 		defer span.End() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// pass the span through the request context | 
					
						
							|  |  |  | 		c.Request = c.Request.WithContext(ctx) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// serve the request to the next middleware | 
					
						
							|  |  |  | 		c.Next() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		status := c.Writer.Status() | 
					
						
							|  |  |  | 		if status > 0 { | 
					
						
							| 
									
										
										
										
											2024-03-11 15:34:34 +01:00
										 |  |  | 			span.SetAttributes(semconv.HTTPResponseStatusCode(status)) | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if len(c.Errors) > 0 { | 
					
						
							|  |  |  | 			span.SetAttributes(attribute.String("gin.errors", c.Errors.String())) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func InjectRequestID() gin.HandlerFunc { | 
					
						
							|  |  |  | 	return func(c *gin.Context) { | 
					
						
							|  |  |  | 		id := gtscontext.RequestID(c.Request.Context()) | 
					
						
							|  |  |  | 		if id != "" { | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 			span := trace.SpanFromContext(c.Request.Context()) | 
					
						
							| 
									
										
										
										
											2023-05-09 19:19:48 +02:00
										 |  |  | 			span.SetAttributes(attribute.String("requestID", id)) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-05-05 16:22:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | func ServerRequestAttributes(req *http.Request) []attribute.KeyValue { | 
					
						
							|  |  |  | 	attrs := make([]attribute.KeyValue, 0, 8) | 
					
						
							|  |  |  | 	attrs = append(attrs, method(req.Method)) | 
					
						
							|  |  |  | 	attrs = append(attrs, semconv.URLFull(req.URL.RequestURI())) | 
					
						
							|  |  |  | 	attrs = append(attrs, semconv.URLScheme(req.URL.Scheme)) | 
					
						
							|  |  |  | 	attrs = append(attrs, semconv.UserAgentOriginal(req.UserAgent())) | 
					
						
							|  |  |  | 	attrs = append(attrs, semconv.NetworkProtocolName("http")) | 
					
						
							|  |  |  | 	attrs = append(attrs, semconv.NetworkProtocolVersion(fmt.Sprintf("%d:%d", req.ProtoMajor, req.ProtoMinor))) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if ip, port, err := net.SplitHostPort(req.RemoteAddr); err == nil { | 
					
						
							|  |  |  | 		iport, _ := strconv.Atoi(port) | 
					
						
							|  |  |  | 		attrs = append(attrs, | 
					
						
							|  |  |  | 			semconv.NetworkPeerAddress(ip), | 
					
						
							|  |  |  | 			semconv.NetworkPeerPort(iport), | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 	} else if req.RemoteAddr != "" { | 
					
						
							|  |  |  | 		attrs = append(attrs, | 
					
						
							|  |  |  | 			semconv.NetworkPeerAddress(req.RemoteAddr), | 
					
						
							|  |  |  | 		) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return attrs | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func method(m string) attribute.KeyValue { | 
					
						
							|  |  |  | 	var methodLookup = map[string]attribute.KeyValue{ | 
					
						
							|  |  |  | 		http.MethodConnect: semconv.HTTPRequestMethodConnect, | 
					
						
							|  |  |  | 		http.MethodDelete:  semconv.HTTPRequestMethodDelete, | 
					
						
							|  |  |  | 		http.MethodGet:     semconv.HTTPRequestMethodGet, | 
					
						
							|  |  |  | 		http.MethodHead:    semconv.HTTPRequestMethodHead, | 
					
						
							|  |  |  | 		http.MethodOptions: semconv.HTTPRequestMethodOptions, | 
					
						
							|  |  |  | 		http.MethodPatch:   semconv.HTTPRequestMethodPatch, | 
					
						
							|  |  |  | 		http.MethodPost:    semconv.HTTPRequestMethodPost, | 
					
						
							|  |  |  | 		http.MethodPut:     semconv.HTTPRequestMethodPut, | 
					
						
							|  |  |  | 		http.MethodTrace:   semconv.HTTPRequestMethodTrace, | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if kv, ok := methodLookup[m]; ok { | 
					
						
							|  |  |  | 		return kv | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return semconv.HTTPRequestMethodGet | 
					
						
							|  |  |  | } |