Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move log comment middleware #6899

Merged
merged 4 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions ee/query-service/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(baseapp.LogCommentEnricher)

apiHandler.RegisterPrivateRoutes(r)

Expand Down Expand Up @@ -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(baseapp.LogCommentEnricher)

apiHandler.RegisterRoutes(r, am)
apiHandler.RegisterLogsRoutes(r, am)
Expand Down
66 changes: 66 additions & 0 deletions pkg/http/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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
}
66 changes: 0 additions & 66 deletions pkg/query-service/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ import (
"net"
"net/http"
_ "net/http/pprof" // http profiler
"net/url"
"os"
"regexp"
"strings"
"time"

"github.com/gorilla/handlers"
Expand All @@ -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"
Expand Down Expand Up @@ -291,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(LogCommentEnricher)

// add auth middleware
getUserFromRequest := func(r *http.Request) (*model.UserPayload, error) {
Expand Down Expand Up @@ -339,68 +335,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
Expand Down
Loading