From e8d172925d2e1adf3ef3734ee62e47472359f99a Mon Sep 17 00:00:00 2001 From: nityanandagohain Date: Wed, 22 Jan 2025 15:57:04 +0530 Subject: [PATCH 1/3] fix: move log comment middleware --- ee/query-service/app/server.go | 4 +- pkg/http/middleware/log_enricher.go | 81 +++++++++++++++++++++++++++++ pkg/query-service/app/server.go | 67 +----------------------- 3 files changed, 84 insertions(+), 68 deletions(-) create mode 100644 pkg/http/middleware/log_enricher.go diff --git a/ee/query-service/app/server.go b/ee/query-service/app/server.go index 7a25da0892..4e99297482 100644 --- a/ee/query-service/app/server.go +++ b/ee/query-service/app/server.go @@ -319,7 +319,7 @@ func (s *Server) createPrivateServer(apiHandler *api.APIHandler) (*http.Server, r.Use(setTimeoutMiddleware) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(baseapp.LogCommentEnricher) + r.Use(middleware.NewLogCommentEnricher().Wrap) apiHandler.RegisterPrivateRoutes(r) @@ -362,7 +362,7 @@ func (s *Server) createPublicServer(apiHandler *api.APIHandler, web web.Web) (*h r.Use(setTimeoutMiddleware) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(baseapp.LogCommentEnricher) + r.Use(middleware.NewLogCommentEnricher().Wrap) apiHandler.RegisterRoutes(r, am) apiHandler.RegisterLogsRoutes(r, am) diff --git a/pkg/http/middleware/log_enricher.go b/pkg/http/middleware/log_enricher.go new file mode 100644 index 0000000000..212f1004e8 --- /dev/null +++ b/pkg/http/middleware/log_enricher.go @@ -0,0 +1,81 @@ +package middleware + +import ( + "context" + "net/http" + "net/url" + "strings" + + "go.signoz.io/signoz/pkg/query-service/auth" + "go.signoz.io/signoz/pkg/query-service/common" +) + +type LogCommentEnricher struct { +} + +func NewLogCommentEnricher() *LogCommentEnricher { + + return &LogCommentEnricher{} +} + +func (l *LogCommentEnricher) Wrap(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + referrer := r.Header.Get("Referer") + + var path, dashboardID, alertID, page, client, viewName, tab string + + if referrer != "" { + referrerURL, _ := url.Parse(referrer) + client = "browser" + path = referrerURL.Path + + if strings.Contains(path, "/dashboard") { + // Split the path into segments + pathSegments := strings.Split(referrerURL.Path, "/") + // The dashboard ID should be the segment after "/dashboard/" + // Loop through pathSegments to find "dashboard" and then take the next segment as the ID + for i, segment := range pathSegments { + if segment == "dashboard" && i < len(pathSegments)-1 { + // Return the next segment, which should be the dashboard ID + dashboardID = pathSegments[i+1] + } + } + page = "dashboards" + } else if strings.Contains(path, "/alerts") { + urlParams := referrerURL.Query() + alertID = urlParams.Get("ruleId") + page = "alerts" + } else if strings.Contains(path, "logs") && strings.Contains(path, "explorer") { + page = "logs-explorer" + viewName = referrerURL.Query().Get("viewName") + } else if strings.Contains(path, "/trace") || strings.Contains(path, "traces-explorer") { + page = "traces-explorer" + viewName = referrerURL.Query().Get("viewName") + } else if strings.Contains(path, "/services") { + page = "services" + tab = referrerURL.Query().Get("tab") + if tab == "" { + tab = "OVER_METRICS" + } + } + } else { + client = "api" + } + + email, _ := auth.GetEmailFromJwt(r.Context()) + + kvs := map[string]string{ + "path": path, + "dashboardID": dashboardID, + "alertID": alertID, + "source": page, + "client": client, + "viewName": viewName, + "servicesTab": tab, + "email": email, + } + + r = r.WithContext(context.WithValue(r.Context(), common.LogCommentKey, kvs)) + next.ServeHTTP(w, r) + }) +} diff --git a/pkg/query-service/app/server.go b/pkg/query-service/app/server.go index 9b265efcd9..50813239b6 100644 --- a/pkg/query-service/app/server.go +++ b/pkg/query-service/app/server.go @@ -11,10 +11,8 @@ import ( "net" "net/http" _ "net/http/pprof" // http profiler - "net/url" "os" "regexp" - "strings" "time" "github.com/gorilla/handlers" @@ -33,7 +31,6 @@ import ( "go.signoz.io/signoz/pkg/query-service/app/opamp" opAmpModel "go.signoz.io/signoz/pkg/query-service/app/opamp/model" "go.signoz.io/signoz/pkg/query-service/app/preferences" - "go.signoz.io/signoz/pkg/query-service/common" v3 "go.signoz.io/signoz/pkg/query-service/model/v3" "go.signoz.io/signoz/pkg/signoz" "go.signoz.io/signoz/pkg/web" @@ -291,7 +288,7 @@ func (s *Server) createPublicServer(api *APIHandler, web web.Web) (*http.Server, r.Use(setTimeoutMiddleware) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(LogCommentEnricher) + r.Use(middleware.NewLogCommentEnricher().Wrap) // add auth middleware getUserFromRequest := func(r *http.Request) (*model.UserPayload, error) { @@ -339,68 +336,6 @@ func (s *Server) createPublicServer(api *APIHandler, web web.Web) (*http.Server, }, nil } -func LogCommentEnricher(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - referrer := r.Header.Get("Referer") - - var path, dashboardID, alertID, page, client, viewName, tab string - - if referrer != "" { - referrerURL, _ := url.Parse(referrer) - client = "browser" - path = referrerURL.Path - - if strings.Contains(path, "/dashboard") { - // Split the path into segments - pathSegments := strings.Split(referrerURL.Path, "/") - // The dashboard ID should be the segment after "/dashboard/" - // Loop through pathSegments to find "dashboard" and then take the next segment as the ID - for i, segment := range pathSegments { - if segment == "dashboard" && i < len(pathSegments)-1 { - // Return the next segment, which should be the dashboard ID - dashboardID = pathSegments[i+1] - } - } - page = "dashboards" - } else if strings.Contains(path, "/alerts") { - urlParams := referrerURL.Query() - alertID = urlParams.Get("ruleId") - page = "alerts" - } else if strings.Contains(path, "logs") && strings.Contains(path, "explorer") { - page = "logs-explorer" - viewName = referrerURL.Query().Get("viewName") - } else if strings.Contains(path, "/trace") || strings.Contains(path, "traces-explorer") { - page = "traces-explorer" - viewName = referrerURL.Query().Get("viewName") - } else if strings.Contains(path, "/services") { - page = "services" - tab = referrerURL.Query().Get("tab") - if tab == "" { - tab = "OVER_METRICS" - } - } - } else { - client = "api" - } - - email, _ := auth.GetEmailFromJwt(r.Context()) - - kvs := map[string]string{ - "path": path, - "dashboardID": dashboardID, - "alertID": alertID, - "source": page, - "client": client, - "viewName": viewName, - "servicesTab": tab, - "email": email, - } - - r = r.WithContext(context.WithValue(r.Context(), common.LogCommentKey, kvs)) - next.ServeHTTP(w, r) - }) -} - // TODO(remove): Implemented at pkg/http/middleware/logging.go type loggingResponseWriter struct { http.ResponseWriter From 30994c90985d603604710a861878c5b6093fcb93 Mon Sep 17 00:00:00 2001 From: nityanandagohain Date: Wed, 22 Jan 2025 22:24:49 +0530 Subject: [PATCH 2/3] fix: move log comment to logger middleware --- ee/query-service/app/server.go | 2 - pkg/http/middleware/log_enricher.go | 81 ----------------------------- pkg/http/middleware/logging.go | 66 +++++++++++++++++++++++ pkg/query-service/app/server.go | 1 - 4 files changed, 66 insertions(+), 84 deletions(-) delete mode 100644 pkg/http/middleware/log_enricher.go diff --git a/ee/query-service/app/server.go b/ee/query-service/app/server.go index 4e99297482..0122c7afb6 100644 --- a/ee/query-service/app/server.go +++ b/ee/query-service/app/server.go @@ -319,7 +319,6 @@ func (s *Server) createPrivateServer(apiHandler *api.APIHandler) (*http.Server, r.Use(setTimeoutMiddleware) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(middleware.NewLogCommentEnricher().Wrap) apiHandler.RegisterPrivateRoutes(r) @@ -362,7 +361,6 @@ func (s *Server) createPublicServer(apiHandler *api.APIHandler, web web.Web) (*h r.Use(setTimeoutMiddleware) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(middleware.NewLogCommentEnricher().Wrap) apiHandler.RegisterRoutes(r, am) apiHandler.RegisterLogsRoutes(r, am) diff --git a/pkg/http/middleware/log_enricher.go b/pkg/http/middleware/log_enricher.go deleted file mode 100644 index 212f1004e8..0000000000 --- a/pkg/http/middleware/log_enricher.go +++ /dev/null @@ -1,81 +0,0 @@ -package middleware - -import ( - "context" - "net/http" - "net/url" - "strings" - - "go.signoz.io/signoz/pkg/query-service/auth" - "go.signoz.io/signoz/pkg/query-service/common" -) - -type LogCommentEnricher struct { -} - -func NewLogCommentEnricher() *LogCommentEnricher { - - return &LogCommentEnricher{} -} - -func (l *LogCommentEnricher) Wrap(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - referrer := r.Header.Get("Referer") - - var path, dashboardID, alertID, page, client, viewName, tab string - - if referrer != "" { - referrerURL, _ := url.Parse(referrer) - client = "browser" - path = referrerURL.Path - - if strings.Contains(path, "/dashboard") { - // Split the path into segments - pathSegments := strings.Split(referrerURL.Path, "/") - // The dashboard ID should be the segment after "/dashboard/" - // Loop through pathSegments to find "dashboard" and then take the next segment as the ID - for i, segment := range pathSegments { - if segment == "dashboard" && i < len(pathSegments)-1 { - // Return the next segment, which should be the dashboard ID - dashboardID = pathSegments[i+1] - } - } - page = "dashboards" - } else if strings.Contains(path, "/alerts") { - urlParams := referrerURL.Query() - alertID = urlParams.Get("ruleId") - page = "alerts" - } else if strings.Contains(path, "logs") && strings.Contains(path, "explorer") { - page = "logs-explorer" - viewName = referrerURL.Query().Get("viewName") - } else if strings.Contains(path, "/trace") || strings.Contains(path, "traces-explorer") { - page = "traces-explorer" - viewName = referrerURL.Query().Get("viewName") - } else if strings.Contains(path, "/services") { - page = "services" - tab = referrerURL.Query().Get("tab") - if tab == "" { - tab = "OVER_METRICS" - } - } - } else { - client = "api" - } - - email, _ := auth.GetEmailFromJwt(r.Context()) - - kvs := map[string]string{ - "path": path, - "dashboardID": dashboardID, - "alertID": alertID, - "source": page, - "client": client, - "viewName": viewName, - "servicesTab": tab, - "email": email, - } - - r = r.WithContext(context.WithValue(r.Context(), common.LogCommentKey, kvs)) - next.ServeHTTP(w, r) - }) -} diff --git a/pkg/http/middleware/logging.go b/pkg/http/middleware/logging.go index 50fb4cd06b..76538e2deb 100644 --- a/pkg/http/middleware/logging.go +++ b/pkg/http/middleware/logging.go @@ -2,12 +2,17 @@ package middleware import ( "bytes" + "context" "net" "net/http" + "net/url" + "strings" "time" "github.com/gorilla/mux" semconv "go.opentelemetry.io/otel/semconv/v1.26.0" + "go.signoz.io/signoz/pkg/query-service/auth" + "go.signoz.io/signoz/pkg/query-service/common" "go.uber.org/zap" ) @@ -47,6 +52,9 @@ func (middleware *Logging) Wrap(next http.Handler) http.Handler { zap.String(string(semconv.HTTPRouteKey), path), } + logCommentKVs := getLogCommentKVs(req) + req = req.WithContext(context.WithValue(req.Context(), common.LogCommentKey, logCommentKVs)) + badResponseBuffer := new(bytes.Buffer) writer := newBadResponseLoggingWriter(rw, badResponseBuffer) next.ServeHTTP(writer, req) @@ -69,3 +77,61 @@ func (middleware *Logging) Wrap(next http.Handler) http.Handler { } }) } + +func getLogCommentKVs(r *http.Request) map[string]string { + referrer := r.Header.Get("Referer") + + var path, dashboardID, alertID, page, client, viewName, tab string + + if referrer != "" { + referrerURL, _ := url.Parse(referrer) + client = "browser" + path = referrerURL.Path + + if strings.Contains(path, "/dashboard") { + // Split the path into segments + pathSegments := strings.Split(referrerURL.Path, "/") + // The dashboard ID should be the segment after "/dashboard/" + // Loop through pathSegments to find "dashboard" and then take the next segment as the ID + for i, segment := range pathSegments { + if segment == "dashboard" && i < len(pathSegments)-1 { + // Return the next segment, which should be the dashboard ID + dashboardID = pathSegments[i+1] + } + } + page = "dashboards" + } else if strings.Contains(path, "/alerts") { + urlParams := referrerURL.Query() + alertID = urlParams.Get("ruleId") + page = "alerts" + } else if strings.Contains(path, "logs") && strings.Contains(path, "explorer") { + page = "logs-explorer" + viewName = referrerURL.Query().Get("viewName") + } else if strings.Contains(path, "/trace") || strings.Contains(path, "traces-explorer") { + page = "traces-explorer" + viewName = referrerURL.Query().Get("viewName") + } else if strings.Contains(path, "/services") { + page = "services" + tab = referrerURL.Query().Get("tab") + if tab == "" { + tab = "OVER_METRICS" + } + } + } else { + client = "api" + } + + email, _ := auth.GetEmailFromJwt(r.Context()) + + kvs := map[string]string{ + "path": path, + "dashboardID": dashboardID, + "alertID": alertID, + "source": page, + "client": client, + "viewName": viewName, + "servicesTab": tab, + "email": email, + } + return kvs +} diff --git a/pkg/query-service/app/server.go b/pkg/query-service/app/server.go index 50813239b6..ba5832381f 100644 --- a/pkg/query-service/app/server.go +++ b/pkg/query-service/app/server.go @@ -288,7 +288,6 @@ func (s *Server) createPublicServer(api *APIHandler, web web.Web) (*http.Server, r.Use(setTimeoutMiddleware) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(middleware.NewLogCommentEnricher().Wrap) // add auth middleware getUserFromRequest := func(r *http.Request) (*model.UserPayload, error) { From 9aeeb0806cf0f5216ab911ce95930b16012b4db6 Mon Sep 17 00:00:00 2001 From: nityanandagohain Date: Thu, 23 Jan 2025 10:18:53 +0530 Subject: [PATCH 3/3] fix: make logcomment a method of logging struct --- pkg/http/middleware/logging.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/http/middleware/logging.go b/pkg/http/middleware/logging.go index 76538e2deb..278a170406 100644 --- a/pkg/http/middleware/logging.go +++ b/pkg/http/middleware/logging.go @@ -52,7 +52,7 @@ func (middleware *Logging) Wrap(next http.Handler) http.Handler { zap.String(string(semconv.HTTPRouteKey), path), } - logCommentKVs := getLogCommentKVs(req) + logCommentKVs := middleware.getLogCommentKVs(req) req = req.WithContext(context.WithValue(req.Context(), common.LogCommentKey, logCommentKVs)) badResponseBuffer := new(bytes.Buffer) @@ -78,7 +78,7 @@ func (middleware *Logging) Wrap(next http.Handler) http.Handler { }) } -func getLogCommentKVs(r *http.Request) map[string]string { +func (middleware *Logging) getLogCommentKVs(r *http.Request) map[string]string { referrer := r.Header.Get("Referer") var path, dashboardID, alertID, page, client, viewName, tab string