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

Teach thruster to optionally not log requests #50

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
9 changes: 6 additions & 3 deletions internal/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const (
defaultHttpReadTimeout = 30 * time.Second
defaultHttpWriteTimeout = 30 * time.Second

defaultLogLevel = slog.LevelInfo
defaultLogLevel = slog.LevelInfo
defaultLogRequests = true
)

type Config struct {
Expand Down Expand Up @@ -61,7 +62,8 @@ type Config struct {

ForwardHeaders bool

LogLevel slog.Level
LogLevel slog.Level
LogRequests bool
}

func NewConfig() (*Config, error) {
Expand Down Expand Up @@ -97,7 +99,8 @@ func NewConfig() (*Config, error) {
HttpReadTimeout: getEnvDuration("HTTP_READ_TIMEOUT", defaultHttpReadTimeout),
HttpWriteTimeout: getEnvDuration("HTTP_WRITE_TIMEOUT", defaultHttpWriteTimeout),

LogLevel: logLevel,
LogLevel: logLevel,
LogRequests: getEnvBool("LOG_REQUESTS", defaultLogRequests),
}

config.ForwardHeaders = getEnvBool("FORWARD_HEADERS", !config.HasTLS())
Expand Down
4 changes: 4 additions & 0 deletions internal/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func TestConfig_override_defaults_with_env_vars(t *testing.T) {
usingEnvVar(t, "X_SENDFILE_ENABLED", "0")
usingEnvVar(t, "DEBUG", "1")
usingEnvVar(t, "ACME_DIRECTORY", "https://acme-staging-v02.api.letsencrypt.org/directory")
usingEnvVar(t, "LOG_REQUESTS", "false")

c, err := NewConfig()
require.NoError(t, err)
Expand All @@ -125,6 +126,7 @@ func TestConfig_override_defaults_with_env_vars(t *testing.T) {
assert.Equal(t, false, c.XSendfileEnabled)
assert.Equal(t, slog.LevelDebug, c.LogLevel)
assert.Equal(t, "https://acme-staging-v02.api.letsencrypt.org/directory", c.ACMEDirectoryURL)
assert.Equal(t, false, c.LogRequests)
}

func TestConfig_override_defaults_with_env_vars_using_prefix(t *testing.T) {
Expand All @@ -134,6 +136,7 @@ func TestConfig_override_defaults_with_env_vars_using_prefix(t *testing.T) {
usingEnvVar(t, "THRUSTER_HTTP_READ_TIMEOUT", "5")
usingEnvVar(t, "THRUSTER_X_SENDFILE_ENABLED", "0")
usingEnvVar(t, "THRUSTER_DEBUG", "1")
usingEnvVar(t, "THRUSTER_LOG_REQUESTS", "0")

c, err := NewConfig()
require.NoError(t, err)
Expand All @@ -143,6 +146,7 @@ func TestConfig_override_defaults_with_env_vars_using_prefix(t *testing.T) {
assert.Equal(t, 5*time.Second, c.HttpReadTimeout)
assert.Equal(t, false, c.XSendfileEnabled)
assert.Equal(t, slog.LevelDebug, c.LogLevel)
assert.Equal(t, false, c.LogRequests)
}

func TestConfig_prefixed_variables_take_precedence_over_non_prefixed(t *testing.T) {
Expand Down
5 changes: 4 additions & 1 deletion internal/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type HandlerOptions struct {
targetUrl *url.URL
xSendfileEnabled bool
forwardHeaders bool
logRequests bool
}

func NewHandler(options HandlerOptions) http.Handler {
Expand All @@ -28,7 +29,9 @@ func NewHandler(options HandlerOptions) http.Handler {
handler = http.MaxBytesHandler(handler, int64(options.maxRequestBody))
}

handler = NewLoggingMiddleware(slog.Default(), handler)
if options.logRequests {
handler = NewLoggingMiddleware(slog.Default(), handler)
}

return handler
}
1 change: 1 addition & 0 deletions internal/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,5 +251,6 @@ func handlerOptions(targetUrl string) HandlerOptions {
maxCacheableResponseBody: 1024,
badGatewayPage: "",
forwardHeaders: true,
logRequests: true,
}
}
1 change: 1 addition & 0 deletions internal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (s *Service) Run() int {
maxRequestBody: s.config.MaxRequestBody,
badGatewayPage: s.config.BadGatewayPage,
forwardHeaders: s.config.ForwardHeaders,
logRequests: s.config.LogRequests,
}

handler := NewHandler(handlerOptions)
Expand Down