diff --git a/ee/query-service/app/server.go b/ee/query-service/app/server.go index acc5132dc2..55a325c8f8 100644 --- a/ee/query-service/app/server.go +++ b/ee/query-service/app/server.go @@ -324,7 +324,6 @@ func (s *Server) createPrivateServer(apiHandler *api.APIHandler) (*http.Server, ).Wrap) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(baseapp.LogCommentEnricher) apiHandler.RegisterPrivateRoutes(r) @@ -371,7 +370,6 @@ func (s *Server) createPublicServer(apiHandler *api.APIHandler, web web.Web) (*h ).Wrap) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(baseapp.LogCommentEnricher) apiHandler.RegisterRoutes(r, am) apiHandler.RegisterLogsRoutes(r, am) diff --git a/pkg/http/middleware/logging.go b/pkg/http/middleware/logging.go index 50fb4cd06b..278a170406 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 := middleware.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 (middleware *Logging) 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 f03e96a874..bef2ae5ee3 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" @@ -300,7 +297,6 @@ func (s *Server) createPublicServer(api *APIHandler, web web.Web) (*http.Server, ).Wrap) r.Use(s.analyticsMiddleware) r.Use(middleware.NewLogging(zap.L()).Wrap) - r.Use(LogCommentEnricher) // add auth middleware getUserFromRequest := func(r *http.Request) (*model.UserPayload, error) { @@ -348,68 +344,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