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

Added option to write HTTP request logs to separate file #9449

Merged
merged 2 commits into from
Feb 15, 2018
Merged
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
8 changes: 8 additions & 0 deletions etc/config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,14 @@
# Determines whether HTTP request logging is enabled.
# log-enabled = true

# When HTTP request logging is enabled, this option specifies the path where
# log entries should be written. If unspecified, the default is to write to stderr, which
# intermingles HTTP logs with internal InfluxDB logging.
#
# If influxd is unable to access the specified path, it will log an error and fall back to writing
# the request log to stderr.
# access-log-path = ""

# Determines whether detailed write logging is enabled.
# write-tracing = false

Expand Down
2 changes: 2 additions & 0 deletions services/httpd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Config struct {
UnixSocketEnabled bool `toml:"unix-socket-enabled"`
BindSocket string `toml:"bind-socket"`
MaxBodySize int `toml:"max-body-size"`
AccessLogPath string `toml:"access-log-path"`
}

// NewConfig returns a new Config with default settings.
Expand Down Expand Up @@ -67,5 +68,6 @@ func (c Config) Diagnostics() (*diagnostics.Diagnostics, error) {
"https-enabled": c.HTTPSEnabled,
"max-row-limit": c.MaxRowLimit,
"max-connection-limit": c.MaxConnectionLimit,
"access-log-path": c.AccessLogPath,
}), nil
}
26 changes: 26 additions & 0 deletions services/httpd/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type Handler struct {
Config *Config
Logger *zap.Logger
CLFLogger *log.Logger
accessLog *os.File
stats *Statistics

requestTracker *RequestTracker
Expand Down Expand Up @@ -180,6 +181,31 @@ func NewHandler(c Config) *Handler {
return h
}

func (h *Handler) Open() {
if h.Config.LogEnabled {
path := "stderr"

if h.Config.AccessLogPath != "" {
f, err := os.OpenFile(h.Config.AccessLogPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err != nil {
h.Logger.Error("unable to open access log, falling back to stderr", zap.Error(err), zap.String("path", h.Config.AccessLogPath))
return
}
h.CLFLogger = log.New(f, "", 0) // [httpd] prefix stripped when logging to a file
h.accessLog = f
path = h.Config.AccessLogPath
}
h.Logger.Info("opened HTTP access log", zap.String("path", path))
}
}

func (h *Handler) Close() {
if h.accessLog != nil {
h.accessLog.Close()
h.accessLog = nil
}
}

// Statistics maintains statistics for the httpd service.
type Statistics struct {
Requests int64
Expand Down
4 changes: 4 additions & 0 deletions services/httpd/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ func (s *Service) Open() error {
s.Logger.Info("Starting HTTP service")
s.Logger.Info(fmt.Sprint("Authentication enabled:", s.Handler.Config.AuthEnabled))

s.Handler.Open()

// Open listener.
if s.https {
cert, err := tls.LoadX509KeyPair(s.cert, s.key)
Expand Down Expand Up @@ -163,6 +165,8 @@ func (s *Service) Open() error {

// Close closes the underlying listener.
func (s *Service) Close() error {
s.Handler.Close()

if s.ln != nil {
if err := s.ln.Close(); err != nil {
return err
Expand Down