fix: logging middleware add excluded routes (#6917)

* fix: logging middleware add excluded routes

* fix: consistant name and update example

* fix: consistant name
This commit is contained in:
Nityananda Gohain 2025-01-24 14:43:28 +05:30 committed by GitHub
parent af6b54aeb4
commit b544a54c40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 40 additions and 8 deletions

View File

@ -77,4 +77,7 @@ apiserver:
max: 600s
excluded_routes:
- /api/v1/logs/tail
- /api/v3/logs/livetail
- /api/v3/logs/livetail
logging:
excluded_routes:
- /api/v1/health

View File

@ -330,7 +330,7 @@ func (s *Server) createPrivateServer(apiHandler *api.APIHandler) (*http.Server,
s.serverOptions.Config.APIServer.Timeout.Max,
).Wrap)
r.Use(middleware.NewAnalytics(zap.L()).Wrap)
r.Use(middleware.NewLogging(zap.L()).Wrap)
r.Use(middleware.NewLogging(zap.L(), s.serverOptions.Config.APIServer.Logging.ExcludedRoutes).Wrap)
apiHandler.RegisterPrivateRoutes(r)
@ -376,7 +376,7 @@ func (s *Server) createPublicServer(apiHandler *api.APIHandler, web web.Web) (*h
s.serverOptions.Config.APIServer.Timeout.Max,
).Wrap)
r.Use(middleware.NewAnalytics(zap.L()).Wrap)
r.Use(middleware.NewLogging(zap.L()).Wrap)
r.Use(middleware.NewLogging(zap.L(), s.serverOptions.Config.APIServer.Logging.ExcludedRoutes).Wrap)
apiHandler.RegisterRoutes(r, am)
apiHandler.RegisterLogsRoutes(r, am)

View File

@ -9,6 +9,7 @@ import (
// Config holds the configuration for config.
type Config struct {
Timeout Timeout `mapstructure:"timeout"`
Logging Logging `mapstructure:"logging"`
}
type Timeout struct {
@ -20,6 +21,11 @@ type Timeout struct {
ExcludedRoutes []string `mapstructure:"excluded_routes"`
}
type Logging struct {
// The list of routes that are excluded from the logging
ExcludedRoutes []string `mapstructure:"excluded_routes"`
}
func NewConfigFactory() factory.ConfigFactory {
return factory.NewConfigFactory(factory.MustNewName("apiserver"), newConfig)
}
@ -34,6 +40,11 @@ func newConfig() factory.Config {
"/api/v3/logs/livetail",
},
},
Logging: Logging{
ExcludedRoutes: []string{
"/api/v1/health",
},
},
}
}

View File

@ -16,6 +16,7 @@ func TestNewWithEnvProvider(t *testing.T) {
t.Setenv("SIGNOZ_APISERVER_TIMEOUT_DEFAULT", "70s")
t.Setenv("SIGNOZ_APISERVER_TIMEOUT_MAX", "700s")
t.Setenv("SIGNOZ_APISERVER_TIMEOUT_EXCLUDED__ROUTES", "/excluded1,/excluded2")
t.Setenv("SIGNOZ_APISERVER_LOGGING_EXCLUDED__ROUTES", "/api/v1/health1")
conf, err := config.New(
context.Background(),
@ -45,6 +46,11 @@ func TestNewWithEnvProvider(t *testing.T) {
"/excluded2",
},
},
Logging: Logging{
ExcludedRoutes: []string{
"/api/v1/health1",
},
},
}
assert.Equal(t, expected, actual)

View File

@ -21,16 +21,23 @@ const (
)
type Logging struct {
logger *zap.Logger
logger *zap.Logger
excludedRoutes map[string]struct{}
}
func NewLogging(logger *zap.Logger) *Logging {
func NewLogging(logger *zap.Logger, excludedRoutes []string) *Logging {
if logger == nil {
panic("cannot build logging, logger is empty")
}
excludedRoutesMap := make(map[string]struct{})
for _, route := range excludedRoutes {
excludedRoutesMap[route] = struct{}{}
}
return &Logging{
logger: logger.Named(pkgname),
logger: logger.Named(pkgname),
excludedRoutes: excludedRoutesMap,
}
}
@ -59,6 +66,11 @@ func (middleware *Logging) Wrap(next http.Handler) http.Handler {
writer := newBadResponseLoggingWriter(rw, badResponseBuffer)
next.ServeHTTP(writer, req)
// if the path is in the excludedRoutes map, don't log
if _, ok := middleware.excludedRoutes[path]; ok {
return
}
statusCode, err := writer.StatusCode(), writer.WriteError()
fields = append(fields,
zap.Int(string(semconv.HTTPResponseStatusCodeKey), statusCode),

View File

@ -275,7 +275,7 @@ func (s *Server) createPrivateServer(api *APIHandler) (*http.Server, error) {
s.serverOptions.Config.APIServer.Timeout.Max,
).Wrap)
r.Use(middleware.NewAnalytics(zap.L()).Wrap)
r.Use(middleware.NewLogging(zap.L()).Wrap)
r.Use(middleware.NewLogging(zap.L(), s.serverOptions.Config.APIServer.Logging.ExcludedRoutes).Wrap)
api.RegisterPrivateRoutes(r)
@ -305,7 +305,7 @@ func (s *Server) createPublicServer(api *APIHandler, web web.Web) (*http.Server,
s.serverOptions.Config.APIServer.Timeout.Max,
).Wrap)
r.Use(middleware.NewAnalytics(zap.L()).Wrap)
r.Use(middleware.NewLogging(zap.L()).Wrap)
r.Use(middleware.NewLogging(zap.L(), s.serverOptions.Config.APIServer.Logging.ExcludedRoutes).Wrap)
// add auth middleware
getUserFromRequest := func(r *http.Request) (*model.UserPayload, error) {