-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
feat: add query-log-path
configuration
#25710
base: master-1.x
Are you sure you want to change the base?
Conversation
This PR adds the ability for an admin to define 'query-log-path' as a configuration item field. A new configuration option is created ```toml [data] query-log-path = "/var/influx/query.log" ``` This will enable query logging. Logged queries example: ``` {"level":"info","ts":1735248393.084461,"msg":"Executing query","query":"SHOW DATABASES"} {"level":"info","ts":1735248395.092188,"msg":"Executing query","query":"SHOW DATABASES"} {"level":"info","ts":1735248398.58039,"msg":"Executing query","query":"SELECT * FROM stress.autogen.m0 LIMIT 20"} ```
a440193
to
410241e
Compare
} | ||
|
||
// Test to ensure that watcher creates new file on file rename | ||
func TestQueryExecutor_WriteQueryToLog_WatcherRemoveFile(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It appears that CI doesn't like these tests where on my local machine they pass no problem. I may need to adjust how I'm doing this 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did not look at the tests
@@ -66,6 +66,7 @@ | |||
# Whether queries should be logged before execution. Very useful for troubleshooting, but will | |||
# log any sensitive data contained within a query. | |||
# query-log-enabled = true | |||
# query-log-path = "/var/log/influxdb/query.log" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the default be the empty string?
query/executor.go
Outdated
func initQueryLogWriter(log *zap.Logger, e *Executor, path string) (*os.File, error) { | ||
logFile, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0644) | ||
if err != nil { | ||
e.Logger.Error("failed to open log file", zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fmt.Errorf
to put the file path into the error. It can help to pinpoint the problem
encoderConfig := zap.NewProductionEncoderConfig() | ||
|
||
fileCore := zapcore.NewCore( | ||
zapcore.NewJSONEncoder(encoderConfig), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we respect the configuration setting for log format?
query/executor.go
Outdated
|
||
file, err = initQueryLogWriter(log, e, path) | ||
if err != nil { | ||
e.Logger.Error("failed to open log file", zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this error logged in initQueryLogWriter
?
query/executor.go
Outdated
closeQueryLogWriter(file.Fd(), e) | ||
file, err = initQueryLogWriter(log, e, path) | ||
if err != nil { | ||
e.Logger.Error("failed to create log file watcher", zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Already logged?
query/executor.go
Outdated
} | ||
|
||
func (e *Executor) WithLogWriter(log *zap.Logger, path string) { | ||
var file *os.File |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the closeQueryLogWriter
be in a defer
call? There's only one open at a time.
query/executor.go
Outdated
defer watcher.Close() | ||
|
||
for { | ||
select { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need an exit case here; a context
that is cancelled on influxd
shutdown, or some similar mechanism that closes and cleans up the watcher and the log.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Otherwise we may not get the Sync() and Close() on the file, I believe.
query/executor.go
Outdated
return logFile, nil | ||
} | ||
|
||
func closeQueryLogWriter(fd uintptr, e *Executor) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wouldn't operate on the fd
here. Stick with the higher level libraries, like os.File.Sync
and os.File.Close
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I might call our utility function SyncDir on the containing directory to ensure the log files are saved in case of bad crashes.
query/executor.go
Outdated
|
||
err = watcher.Add(file.Name()) | ||
if err != nil { | ||
e.Logger.Error("failed to watch log file", zap.Error(err)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add file name to error message?
This PR adds the ability for an admin to define 'query-log-path' as a configuration item field.
A new configuration option is created
This will enable query logging.
Logged queries example: