Browse code

api: Add method and path to trace operation string

Currently, all traces coming from the API have an empty operation
string, which make them indistinguishable from each other without looking
at the logs of the root span, and prevent proper filtering on Jaeger UI.

With this change, traces get the route pattern as the operation string.

Signed-off-by: Albin Kerouanton <albinker@gmail.com>

Albin Kerouanton authored on 2023/09/23 23:03:39
Showing 1 changed files
... ...
@@ -30,7 +30,7 @@ func (s *Server) UseMiddleware(m middleware.Middleware) {
30 30
 	s.middlewares = append(s.middlewares, m)
31 31
 }
32 32
 
33
-func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
33
+func (s *Server) makeHTTPHandler(handler httputils.APIFunc, operation string) http.HandlerFunc {
34 34
 	return otelhttp.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
35 35
 		// Define the context that we'll pass around to share info
36 36
 		// like the docker-request-id.
... ...
@@ -59,7 +59,7 @@ func (s *Server) makeHTTPHandler(handler httputils.APIFunc) http.HandlerFunc {
59 59
 			}
60 60
 			makeErrorHandler(err)(w, r)
61 61
 		}
62
-	}), "").ServeHTTP
62
+	}), operation).ServeHTTP
63 63
 }
64 64
 
65 65
 type pageNotFoundError struct{}
... ...
@@ -77,7 +77,7 @@ func (s *Server) CreateMux(routers ...router.Router) *mux.Router {
77 77
 	log.G(context.TODO()).Debug("Registering routers")
78 78
 	for _, apiRouter := range routers {
79 79
 		for _, r := range apiRouter.Routes() {
80
-			f := s.makeHTTPHandler(r.Handler())
80
+			f := s.makeHTTPHandler(r.Handler(), r.Method()+" "+r.Path())
81 81
 
82 82
 			log.G(context.TODO()).Debugf("Registering %s, %s", r.Method(), r.Path())
83 83
 			m.Path(versionMatcher + r.Path()).Methods(r.Method()).Handler(f)
... ...
@@ -87,7 +87,7 @@ func (s *Server) CreateMux(routers ...router.Router) *mux.Router {
87 87
 
88 88
 	debugRouter := debug.NewRouter()
89 89
 	for _, r := range debugRouter.Routes() {
90
-		f := s.makeHTTPHandler(r.Handler())
90
+		f := s.makeHTTPHandler(r.Handler(), r.Method()+" "+r.Path())
91 91
 		m.Path("/debug" + r.Path()).Handler(f)
92 92
 	}
93 93